Python and Pylons and Jinja2

From Wildsong
Revision as of 19:42, 16 December 2013 by Brian Wilson (talk | contribs)
Jump to navigationJump to search

oh my!

and maybe maps with Leaflet too.

Pylons Project Pyramid is for writing web applications in Python.

"Jinja2 is a modern and designer friendly templating language for Python, modelled after Django’s templates."

By "templating" they mean embedding python (or code) inside HTML files, like PHP.

"bootstrap" is a "front end framework". It is a bunch of javascript and css files that help you build a site with a consistent look.

Pyramid helps you write WSGI applications, so you are writing in python and it generates the HTML.

Pyramid

First install it and check to see which version it is

sudo pip install pyramid
python
>>> import pkg_resources
>>> pkg_resources.get_distribution("pyramid").version
'1.5a3'

Then create the minimal greeting aka "Hello, world" application.

Standalone

This is straight out of the manual. Pyramid runs its own web server so you save this to a file such as pyramid-hello.py.

from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
def hello_world(request):
    return Response('Hello %(name)s!' % request.matchdict)
if __name__ == '__main__':
    config = Configurator()
    config.add_route('hello', '/hello/{name}')
    config.add_view(hello_world, route_name='hello')
    app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 8080, app)
    server.serve_forever()

Next you run it, for example "python pyramid-hello.py" On the command line, you see NOTHING. You connect to your computer at port 8080 with a URL like this "http://ip address/hello/brian" and the response comes back and you get console output in the shell where you started python.

That's great, you can run it and test it using a Python IDE such as Komodo. To deploy you will want to run it as a WSGI based application.

Apache

http://docs.pylonsproject.org/projects/pyramid/en/1.0-branch/tutorials/modwsgi/index.html

Now, press on. Read the manual.