static_url usage

55 views
Skip to first unread message

davide moro

unread,
Aug 15, 2014, 6:39:51 PM8/15/14
to pylons...@googlegroups.com
Hi there,

say hello to a new pyramid user :). This is my very first practical experience with Pyramid.

I've registered a static view

    >>> config.add_static_view('images', 'p_started:webapp/app/images', cache_max_age=3601)

and all works fine visiting the url http://localhost:6543/images/pyramid.png using the browser, so the registration it's ok.
You might ask why am I registering a so strange static url? I'm trying to integrate pyramid with an external automated system for assets management (image optimization, css/js merge and minification). So depending on the .ini settings my app will register webapp/app/images or webapp/dist/images (to be used in production, with image optimized). Same stuff with css and js. I succeded in configuring the same thing with Plone using ++resources++.

The only problem I that request.static_url uses the path and not the name registered in add_static_view, causing a value error.

Is there a way to generate a static url using the name "images" registered in add_static_view?
I mean:
    >>> ${request.static_url('p_started:images/pyramid.png')}
instead of:
    >>> ${request.static_url('p_started:webapp/app/images/pyramid.png')}
I want to use the name because the path will change depending on the deployment settings.

Thank you in advance for your support.

Best regards,

davide

Karl O. Pinc

unread,
Aug 16, 2014, 12:59:47 AM8/16/14
to pylons...@googlegroups.com
Hello,

On 08/15/2014 05:39:51 PM, davide moro wrote:

> I've registered a static view
>
> >>> config.add_static_view('images',
> 'p_started:webapp/app/images',
> cache_max_age=3601)


> So depending on
> the
> .ini settings my app will register webapp/app/images or
> webapp/dist/images
> (to be used in production, with image optimized)

> The only problem I that request.static_url uses the path and not the
> name
> registered in add_static_view, causing a value error.
>
> Is there a way to generate a static url using the name "images"
> registered
> in add_static_view?
> I mean:
> >>> ${request.static_url('p_started:images/pyramid.png')}
> instead of:
> >>> ${request.static_url('p_started:webapp/app/images/
> pyramid.png')}
> I want to use the name because the path will change depending on the
> deployment settings.

I don't know how to do what you want, and am not particularly
experienced with pyramid. But if the path changes based
on deployment settings why not use the deployment settings
value everywhere and make that relationship explicit?

In the .ini:

minify=app

Then in the code:

config.add_static_view('images',
, 'p_started:webapp/'
+ config.registry.settings['minifiy']
+ '/images'
, cache_max_age=3601)


And:


${request.static_url('p_started:images/'
+ request.registry.settings['minifiy']
+ '/images')

There's probably a better way to abstract this, but you get the
idea.

See:

http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/
startup.html#deployment-settings


http://docs.pylonsproject.org/projects/pyramid/en/latest/api/
registry.html#pyramid.registry.Registry.settings

Regards,

Karl <k...@meme.com>
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein

davide moro

unread,
Aug 16, 2014, 4:23:18 PM8/16/14
to pylons...@googlegroups.com
Hi Karl!

Thank you for your tips, I hoped to avoid this workaround.

Anyway for me it sounds like a weird behaviour of static_url. I'm not sure but probably its parameter should be bound to the name registered in add_static_view because:
* if you want to construct a url you should know the exact path associated to the name (webapp/app/images)
* if you change the path of your assets you'll have to change all your templates code
* when you construct by hand an url of a static resource you just digit http://localhost:6543/images/pyramid.png and not webapp/app/etc

Or not? What do you think about that?

Thank you!

davide

Michael Merickel

unread,
Aug 16, 2014, 8:06:12 PM8/16/14
to pylons-devel
You'll want to avoid changing the path in your usage of static_url.
Fortunately, Pyramid supports overriding static assets, so you can
make a dummy path and override that with your dev or production "real"
asset path.

config.add_static_view('images', 'p_started:webapp/images')
config.override_asset(to_override='p_started:webapp/images/',
override_with='p_started:webapp/dist/images')

request.static_url('p_started:webapp/images/pyramid.png')
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to pylons-devel...@googlegroups.com.
> To post to this group, send email to pylons...@googlegroups.com.
> Visit this group at http://groups.google.com/group/pylons-devel.
> For more options, visit https://groups.google.com/d/optout.

davide moro

unread,
Aug 17, 2014, 5:55:14 PM8/17/14
to pylons...@googlegroups.com
Hi Michael!


2014-08-17 2:05 GMT+02:00 Michael Merickel <mmer...@gmail.com>:
You'll want to avoid changing the path in your usage of static_url.
Fortunately, Pyramid supports overriding static assets, so you can
make a dummy path and override that with your dev or production "real"
asset path.

config.add_static_view('images', 'p_started:webapp/images')
config.override_asset(to_override='p_started:webapp/images/',
override_with='p_started:webapp/dist/images')

request.static_url('p_started:webapp/images/pyramid.png')


Thank you, it works :)

davide moro

unread,
Sep 16, 2014, 7:55:05 AM9/16/14
to pylons...@googlegroups.com
Hi,

thanks to your tips I've just published my first write up about Pylons/Pyramid:
* http://davidemoro.blogspot.com/2014/09/pyramid-starter-seed-yeomam-part-1.html

Thank you for your support,

davide

Steve Piercy

unread,
Sep 16, 2014, 5:16:20 PM9/16/14
to pylons...@googlegroups.com
Davide,

Would you like to add this to the Pyramid Cookbook?
http://docs.pylonsproject.org/projects/pyramid-cookbook/en/latest/

I am not sure under which category it would be placed. We could
start a new category, maybe "Developer Workflow Automation"?
The default would be "Miscellaneous", but that wouldn't do
justice to something I see becoming more commonplace in a
developer's workflow.

Please let me know.

--steve


On 9/16/14 at 1:55 PM, david...@gmail.com (davide moro) pronounced:
------------------------
Steve Piercy, Soquel, CA

davide moro

unread,
Sep 16, 2014, 5:29:17 PM9/16/14
to pylons...@googlegroups.com
Hi Steve,

sure, it would be great!

I'm going to add another important feature to pyramid_starter_seed, so probably it is better waiting for the whole picture and then freeze it.
I think I will be able to finish by the end of this week (I'm in best effort mode, you know, I'm working on this project at night time :)

Bye,

davide


--
You received this message because you are subscribed to the Google Groups "pylons-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-devel+unsubscribe@googlegroups.com.

davide moro

unread,
Sep 18, 2014, 8:13:45 AM9/18/14
to pylons...@googlegroups.com
Hi,

a couple of people reported problems related with the prerequisites installation (nodejs).

Here you can find a write up that should helps if you are in trouble with the pyramid_starter_seed template installation:
* http://davidemoro.blogspot.com/2014/09/pyramid-starter-seed-yeoman-part-2.html

Bye,

davide

Reply all
Reply to author
Forward
0 new messages