- what are the benefits of the Yeoman workflow applied to Pyramid, a traditional web framework (part 1)
- how to install pyramid_starter_seed, a Pyramid+Yeoman flavoured starter template (part 2)
- how it works under the hood
- how to manage things with grunt
- how to create and share other templates based on pyramid_starter_seed
How it works under the hood (narrative)
pyramid_starter_seed registers only one route (home -> /) and static assets views.The home route is associated to a view callable with a webapp/%s/index.html renderer.
from pyramid.view import view_configViews can be associated to routes imperatively or through a scan.
@view_config(route_name='home', renderer='webapp/%s/index.html')
def my_view(request):
return {'project': 'pyramid_starter_seed'}
Wait, there is no .html renderer handled by default in Pyramid! The pyramid_starter_seed will register a .html renderer that will replace the string token with app or dist depending on the production settings and it calls the original .pt renderer.
You can choose between production vs development mode running your pyramid app with the appropriate .ini file provided by pyramid_starter_seed.
Here you can see the relevant parts of the production.ini:
[app:main]
use = egg:pyramid_starter_seed
PRODUCTION = true
minify = dist
...and development.ini:
[app:main]As you can imagine the PRODUCTION configuration tells the application to switch between production or development. The minify configuration tells the .html renderer how to construct templates paths.
use = egg:pyramid_starter_seed
PRODUCTION = false
minify = app
...
Let's see how looks like the index.html template. When you write css or javascript files you want to keep things separated on different modules when you are in development mode, but in production mode you might want a unique concatenated and minified/uglified resource. Here you can see how you can do that for the Bootstrap javascripts modules you might want to enable:
<!doctype html>So in development mode you will have two separate javascript files: alert.js and dropdown.js. When you are in production mode it will served a unique concatenated and uglyfied scripts/plugins.js. At first time it might seem a bit complicated or a task with a too verbose setup, but it is a simple and very powerful mechanism.
<html class="no-js"
lang="${request.locale_name}"
tal:define="minify python:request.registry.settings['minify'];
production python:request.registry.settings.get('PRODUCTION', 'false') == 'true'">
...
<tal:production tal:condition="production">
<script src="${request.static_url('pyramid_starter_seed:webapp/%s/scripts/plugins.js' % minify)}"></script>
</tal:production>
<tal:not_production tal:condition="not:production">
<script src="${request.static_url('pyramid_starter_seed:webapp/%s/bower_components/bootstrap/js/alert.js' % minify)}"></script>
<script src="${request.static_url('pyramid_starter_seed:webapp/%s/bower_components/bootstrap/js/dropdown.js' % minify)}"></script>
</tal:not_production>
<!-- build:js scripts/plugins.js -->
<tal:comment replace="nothing">
<!-- DO NOT REMOVE this block (minifier) -->
<script src="./bower_components/bootstrap/js/alert.js"></script>
<script src="./bower_components/bootstrap/js/dropdown.js"></script>
</tal:comment>
<!-- endbuild -->
...
</html>
For example: do you want to add another javascript file to the plugins.js bundle? Just add two lines and you are ok!
It works without any other configuration thanks to the start and end comments blocks build:js and endbuild that groups assets groups.
Simple, isn't it? Same thing for css files.
And if you have to include images is even more simple with:
<img class="logo img-responsive" src="${request.static_url('pyramid_starter_seed:webapp/%s/images/pyramid.png' % minify)}" alt="pyramid web framework">
How to manage things with grunt
The concatenation, minification/uglyfication and image optimization (with other tasks specified in the grunt's pipeline) it is automatically performed just running the following command:$ grunt buildThis is just one of the possible implementations. Feel free to contribute and improve pyramid_starter_seed.
How to clone pyramid_starter_seed
Fetch pyramid_starter_seed, personalize it and then clone it!Pyramid starter seed can be fetched, personalized and released with another name. So other developer can bootstrap, build, release and distribute their own starter templates without having to write a new package template generator. For example you could create a more opinionated starter seed based on SQLAlchemy, ZODB nosql or powered by a javascript framework like AngularJS and so on.
The clone method should speed up the process of creation of new more evoluted packages based on Pyramid, also people that are not keen on writing their own reusable scaffold templates.
So if you want to release your own customized template based on pyramid_starter_seed you'll have to call a console script named pyramid_starter_seed_clone with the following syntax (obviously you'll have to call this command outside the root directory of pyramid_starter_seed):
$ YOUR_VIRTUALENV_PYTHON_PATH/bin/pyramid_starter_seed_clone new_templateand you'll get as a result a perfect renamed clone new_template:
A new starter template cloned from pyramid_starter_seed |
In effect the clone console script it might not work in some corner cases just in case you choose a new package name that contains reserved words or the name of a dependency of your plugin, but it should be quite easy to fix by hand or improving the console script. Anyway this mechanism has been tested several years: I have built this script years ago because I was fed up with ugly package names chose by me or other colleagues of mine and allowed me to save a lot of time.
If you want to disable the console script on your new template (for example: new_template_clone) drop from setup.py the console script configuration.
So it sounds like a viral extension mechanism (I hope).
End of story?
In the next future I'd like to create a new Pyramid starter seed for single page web apps based on SQLAlchemy and powered by AngularJS. So if you similar plans... together is better: if you want to share your thoughts, improvements, feedback in general, or if you are going to create your own template based on pyramid_starter_seed please contact me (Twitter, Google+, Linkedin)!
Anyway I hope you'll save time with pyramid_starter_seed.
Links
- pyramid_starter_seed (github repo): https://github.com/davidemoro/pyramid_starter_seed
- part 1 (benefits of yeoman fully integrated with a web framework like Pyramid): http://davidemoro.blogspot.com/2014/09/pyramid-starter-seed-yeomam-part-1.html
- part 2 (how to install pyramid_starter_seed ant its prerequisites, this blog post) - http://davidemoro.blogspot.com/2014/09/pyramid-starter-seed-yeoman-part-2.html
- part 3 (how to manage things with grunt, personalize pyramid_starter_seed and create your own starter template - this blog post) - http://davidemoro.blogspot.com/2014/09/pyramid-starter-seed-yeoman-part-3.html
No comments:
Post a Comment
Note: only a member of this blog may post a comment.