Minimatic for Pyramid?

424 views
Skip to first unread message

Mark Huang

unread,
Jun 12, 2012, 2:31:05 AM6/12/12
to pylons-...@googlegroups.com
Hi Pyramid experts,

I am about to launch a web application soon and I realized I had some problems with js and css assets (minimizing and which one to use for production and development).  I used to use Minimatic when I was developing using Pylons.  I came to learn that Webhelpers is not supported in Pyramid and hence, Minimatic, being an extension of webhelpers, doesn't seem to work on Pyramid.

What do you guys use for this sort of job.  The key feature I need is for Pyramid to switch resources when I switch from development to production and vice versa.  

Regards,
Mark Huang

John Anderson

unread,
Jun 12, 2012, 2:34:40 AM6/12/12
to pylons-...@googlegroups.com

Zhenghao Huang

unread,
Jun 12, 2012, 2:59:48 AM6/12/12
to pylons-...@googlegroups.com
Hi,

Thank you for your speedy response, I was quite shocked!

I read through the documentation.  I am using Mako templates and they don't seem to have something for that.  Do you have any real examples of it's use in the templates.  My mind is thinking along the lines of Minimatic's way:

Inside base.mako:
${h.javascript_link('/static/js/base.js', dict(file='/static/js/cache.js', minify='minify'))}

Regards,
Mark Huang

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To post to this group, send email to pylons-...@googlegroups.com.
To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.

jerry

unread,
Jun 12, 2012, 12:05:07 PM6/12/12
to pylons-discuss
From a quick skim of the documentation, webassets doesn't seem to be
as flexible or versatile as Fanstatic: Resource publishing for
Pythonistas [ http://www.fanstatic.org/en/latest/index.html ].

Any thoughts?

Jerry

On Jun 12, 2:34 pm, John Anderson <son...@gmail.com> wrote:

Mike Orr

unread,
Jun 12, 2012, 12:38:53 PM6/12/12
to pylons-...@googlegroups.com
On Mon, Jun 11, 2012 at 11:31 PM, Mark Huang <zheng...@gmail.com> wrote:
> I am about to launch a web application soon and I realized I had some
> problems with js and css assets (minimizing and which one to use for
> production and development).  I used to use Minimatic when I was developing
> using Pylons.  I came to learn that Webhelpers is not supported in Pyramid
> and hence, Minimatic, being an extension of webhelpers, doesn't seem to work
> on Pyramid.

Most of WebHelpers works on Pyramid. Only a few helpers that have
Pylons dependencies don't (they're in webhelpers.pylonslib).
webhelpers.paginate and webhelpers.mimehelper have optional Pylons
features that can be bypassed.

The minification helper was dropped due to a licensing issue (its
license wasn't considered free by Fedora). It's available separately
as http://pypi.python.org/pypi/jsmin/2.0.2 . All other Javascript
helpers were dropped after WebHelpers 0.6.4. I can't imagine what else
Minimatic may be using that's not compatible with Pyramid.

WebHelpers does need some maintenance. The main problem is its
documentation link is broken. That's because it doesn't seem to have
gotten into the Pyramid docs when pylonshq.com was retired. I use the
docs at a temporary URL,
http://sluggo.scrapping.cc/python/WebHelpers/, but they're also in the
source code. This will be resolved when I have time to work on it
again.

--
Mike Orr <slugg...@gmail.com>

Randall Leeds

unread,
Jun 12, 2012, 4:42:50 PM6/12/12
to pylons-...@googlegroups.com
On Tue, Jun 12, 2012 at 12:05 PM, jerry <jerry...@gmail.com> wrote:
> From a quick skim of the documentation, webassets doesn't seem to be
> as flexible or versatile as Fanstatic: Resource publishing for
> Pythonistas [ http://www.fanstatic.org/en/latest/index.html ].
>
> Any thoughts?

I've found it to be very flexible and usable. I started off using
fanstatic for my latest project and switched to webassets. The code
seemed cleaner and simpler and the built-in filters did what I needed.
There's a compilers-and-minifiers branch of fanstatic that hadn't been
merged last I checked and the google group is very quiet. I don't
think the authors have abandoned the project and I think they've done
good work, but I decided to go with webassets.

>
> Jerry
>
> On Jun 12, 2:34 pm, John Anderson <son...@gmail.com> wrote:
>> On Tue, Jun 12, 2012 at 1:31 AM, Mark Huang <zhengha...@gmail.com> wrote:
>> > Hi Pyramid experts,
>>
>> > I am about to launch a web application soon and I realized I had some
>> > problems with js and css assets (minimizing and which one to use for
>> > production and development).  I used to use Minimatic when I was developing
>> > using Pylons.  I came to learn that Webhelpers is not supported in Pyramid
>> > and hence, Minimatic, being an extension of webhelpers, doesn't seem to
>> > work on Pyramid.
>>
>> > What do you guys use for this sort of job.  The key feature I need is for
>> > Pyramid to switch resources when I switch from development to production
>> > and vice versa.
>>
>> > Regards,
>> > Mark Huang
>>
>> I usehttp://webassets.readthedocs.org/en/latest/index.html  withhttps://github.com/sontek/pyramid_webassets
>

Randall Leeds

unread,
Jun 13, 2012, 2:25:27 AM6/13/12
to pylons-...@googlegroups.com
On Tue, Jun 12, 2012 at 2:59 AM, Zhenghao Huang <zheng...@gmail.com> wrote:
> Hi,
>
> Thank you for your speedy response, I was quite shocked!
>
> I read through the documentation.  I am using Mako templates and they don't
> seem to have something for that.  Do you have any real examples of it's use
> in the templates.  My mind is thinking along the lines of Minimatic's way:
>
> Inside base.mako:
> ${h.javascript_link('/static/js/base.js', dict(file='/static/js/cache.js',
> minify='minify'))}

The way I treat this is to make these decisions in a separate file and
name my assets in an asset environment.
I use pyramid_webassets and add the webassets environment to my
template globals.

@subscriber(BeforeRender)
def add_global(event):
environment = event['request'].registry.queryUtility(IWebAssetsEnvironment)
event['environment'] = environment

Where IWebAssetsEnvironment comes from pyramid_webassets.

Then in my template I have things like:

<link rel="stylesheet"
tal:attributes="href href"
tal:repeat="href environment['css'].urls()" />

which is a chameleon template. Here I get a single link tag if
environment['css'] is concatenated (+minified) and multiple if not.
Running with webassets in debug mode is all it takes to flip the
switch, and I do that in my paste-style .ini.

Robert Forkel

unread,
Jun 13, 2012, 3:00:37 AM6/13/12
to pylons-...@googlegroups.com
I just started using webassets with mako templates for an existing
project. All assets are included in a site template (site.mako).
Assets come from various sources, e.g. the deform package. So what I
do (which may be crazy): Have my build script parse site.mako to
assemble the bundles (resolving resource specs of the form
'deform:static/...' to absolute paths). This way, using a new asset
does only require adding it to site.mako.
So far I have not run into a show-stopper with this.

regards
robert

Kai Groner

unread,
Jun 13, 2012, 9:17:07 AM6/13/12
to pylons-...@googlegroups.com
I created patch for pyramid_webassets that adds support for asset specs.  I'm hoping maybe it will be in a future release.



Kai

Domen Kožar

unread,
Jun 13, 2012, 9:18:25 AM6/13/12
to pylons-...@googlegroups.com

Mark Huang

unread,
Jun 13, 2012, 1:18:32 PM6/13/12
to pylons-...@googlegroups.com
Hi Randall,

I really like your solution and I think I'll try it out. Thanks for the code snippet, although I use Mako and I'm not too sure when environment["urls"] resolves to many urls when debug=True, whether mako has the ability to turn that into multiple link tags....

Does environment["urls"] return a list?

Regards,
Mark Huang

Mark Huang

unread,
Jun 13, 2012, 1:27:13 PM6/13/12
to pylons-...@googlegroups.com
Hi everyone,

Thanks to everyone that contributed to this thread, I really appreciate it. I have one more question that's been bothering me though:

Managing assets in you python code, be it writing a bundle/library OR calling a jquery.need() while writing your pyramid view, this just feels a bit weird. I was actually hoping for a solution much like minimattic where by the minification/combination of assets are specified in your templates. Maybe it's just me, but don't you guys think it's more natural ?

And what if I had base templates that always use jquery and jqueryui, must I specify that I need to use it in every single vie/controller ( I come from a pylons world ) that I write?

Regards
Mark Huang

Randall Leeds

unread,
Jun 13, 2012, 1:59:24 PM6/13/12
to pylons-...@googlegroups.com
On Wed, Jun 13, 2012 at 10:27 AM, Mark Huang <zheng...@gmail.com> wrote:
> Hi everyone,
>
> Thanks to everyone that contributed to this thread, I really appreciate it. I have one more question that's been bothering me though:
>
> Managing assets in you python code, be it writing a bundle/library OR calling a jquery.need() while writing your pyramid view, this just feels a bit weird. I was actually hoping for a solution much like minimattic where by the minification/combination of assets are specified in your templates.  Maybe it's just me, but don't you guys think it's more natural ?

I prefer not setting minification in my templates because I want it to
be a runtime option (to be flipped off during development for easier
debugging). The debug mode of webassets makes that easy. Fanstatic has
something similar as well.

Instead, I set up most of my assets using the yaml support in webassets:
https://github.com/hypothesis/h/blob/bff441621535674adcd2834a64927c6665de3e67/h/resources.yaml

Combining that with my resources.py, I end up with the webassets
environment in my template globals, as I said before:
https://github.com/hypothesis/h/blob/bff441621535674adcd2834a64927c6665de3e67/h/resources.py#L38

If you try webassets, you also need to turn off the cache and manifest
depending on the assets that are being bundled. This may only be
necessary if you're using things that need to be forced debug=False,
such as SASS/SCSS or CoffeeScript which cannot be served directly to
the browser.
https://github.com/hypothesis/h/blob/bff441621535674adcd2834a64927c6665de3e67/development.ini#L27

Note that I haven't actually added the minifiers to the end of my
filter chains, but you can see by the output names where I plan to.

>
> And what if I had base templates that always use jquery and jqueryui,  must I specify that I need to use it in every single vie/controller ( I come from a pylons world ) that I write?

This is part of why I switched to webassets. At first I thought
.need() was neat, but then I realized that it's more often my
templates than my view functions that need to specify the assets.
Also, it bothered me that some injector middleware would need to read
the body on the way out looking for the </head> or </body> and
injecting the assets. That seems like extra work when I could just
write it in to my template.

To answer your previous question, the mako syntax would be something like:

% for href in environment['css_bundle'].urls():
<link rel="stylesheet" href="${href}" />
% endfor

Also, it should go without saying these are just my preferences. There
are also other projects like https://github.com/tav/assetgen and
probably others.

-Randall

Mark Huang

unread,
Jun 13, 2012, 4:46:37 PM6/13/12
to pylons-...@googlegroups.com
Thanks Randall.  Your explanation really helped!  

Mark Huang

unread,
Jul 5, 2012, 2:40:52 AM7/5/12
to pylons-...@googlegroups.com
Hi Randall,

Just a quick question.  I began to realize I need the expire feature.  How would I add a %(versions)s if I were to use the YAML file to store my bundles?  

Randall Leeds

unread,
Jul 7, 2012, 12:51:14 PM7/7/12
to pylons-...@googlegroups.com

On Jul 4, 2012 11:40 PM, "Mark Huang" <zheng...@gmail.com> wrote:
>
> Hi Randall,
>
> Just a quick question.  I began to realize I need the expire feature.  How would I add a %(versions)s if I were to use the YAML file to store my bundles?  

I believe that replacement variable is best places in the 'base_dir' option.

> --
> You received this message because you are subscribed to the Google Groups "pylons-discuss" group.

> To view this discussion on the web visit https://groups.google.com/d/msg/pylons-discuss/-/9drQg_ClGFMJ.

Reply all
Reply to author
Forward
0 new messages