Un web framework en Python
LinuxCabal, 9 de Abril de 2011, FLISOL
Patricio Páez
Un framework para desarrollar aplicaciones web
Velocidad, flexibilidad y escalabilidad son prioridades
No es un full-stack framework como Django
No es un microframework como Flask o Bottle
--> Documentation --> Pyramid Documentation --> For help getting Pyramid setup --> Installing Pyramid
www.linuxcabal.org/PythonCabal/pyramid
Esta presentación y archivos de trabajo
Crear un ambiente virtual:
virtualenv --no-site-dependencies pyramid-tutorial cd pyramid-tutorial source bin/activate
Paquete python-virtualenv en distribuciones de GNU/Linux.
Instalar Pyramid y sus dependencias:
easy_install pyramid
Validar:
python >>> import pyramid
http://linuxcabal.org/PythonCabal/pyramid
Descargar archivos a la carpeta pyramid-tutorial:
wget http://linuxcabal.org/PythonCabal/pyramid/holamundo.py wget http://linuxcabal.org/PythonCabal/pyramid/template.tar
Descomprimir:
tar xf template.tar
# -*- coding: utf8 -*- from pyramid.config import Configurator from pyramid.response import Response from paste.httpserver import serve def hello_world(request): return Response( u'¡Hola mundo!') if __name__ == '__main__': config = Configurator() config.add_route('main', '/', view=hello_world) app = config.make_wsgi_app() serve(app)
Iniciar el servidor:
python holamundo.py serving on http://127.0.0.1:8080
Visitar con este cliente HTTP:
wget -q -S -O - http://localhost:8080 wget -q -S -O - http://localhost:8080/algo
HTTP/1.0 200 OK Status Server: PasteWSGIServer/0.5 Python/2.6.6 Encabezado Date: Sat, 09 Apr 2011 13:10:42 GMT " Content-Type: text/html; charset=UTF-8 " Content-Length: 13 " ¡Hola mundo! Cuerpo
HTTP/1.0 404 Not Found Status Server: PasteWSGIServer/0.5 Python/2.6.6 Encabezado Date: Sat, 09 Apr 2011 13:29:59 GMT " Content-Length: 206 " Content-Type: text/html " <html> Cuerpo <title>404 Not Found</title> " <body> " <h1>404 Not Found</h1> " <code>/algo</code> " </body> " </html>
Reiniciar el servidor para incluir cada modificación
Recomendable usar control de versiones, git
def hello_world(request): print print request print print Response(u'¡Hola mundo!') return Response(u'¡Hola mundo!')
def goodbye_world(request): return Response('Goodbye world!') config.add_route('bye', '/goodbye', view=goodbye_world) config.add_route('bye2', '/goodbye/', view=goodbye_world)
Probar:
wget -q -S -O - http://localhost:8080/goodbye wget -q -S -O - http://localhost:8080/goodbye/
def goodbye_name(request): resp = u'Goodbye {name}!'.format(**request.matchdict) return Response(resp) config.add_route('bye_name', '/goodbye/{name}', view=goodbye_name)
Probar:
wget -q -S -O - http://localhost:8080/goodbye/Juan Pérez
def hello_world(request): return dict( project = u'¡Hola mundo!' ) config.add_static_view('static', 'holamundo:static') config.add_route('main', '/', view=hello_world, view_renderer='holamundo:mytemplate.pt')
Visitar con navegador:
firefox http://localhost:8080
http://docs.pylonsproject.org/projects/pyramid/1.0/
IRC: #pyramid en freenode.net (http://webchat.freenode.net/)