Introducción a Pyramid

Un web framework en Python

LinuxCabal, 9 de Abril de 2011, FLISOL

Patricio Páez

Pyramid

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

Pyramid

Pyramid

Pyramid

Después de esta sesión

pylonsproject.org

--> Documentation --> Pyramid Documentation --> For help getting Pyramid setup --> Installing Pyramid

www.linuxcabal.org/PythonCabal/pyramid

Esta presentación y archivos de trabajo

Agenda

Instalación

Paquete python-virtualenv en distribuciones de GNU/Linux.

Instalación

Instalación

Hola mundo

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

holamundo.py 0.1

# -*- 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)

Ejecutar y visitar

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

Respuesta

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

Respuesta

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>

Variaciones a un tema

Reiniciar el servidor para incluir cada modificación

Recomendable usar control de versiones, git

holamundo.py 0.2

def hello_world(request):

    print
    print request
    print
    print Response(u'¡Hola mundo!')

    return Response(u'¡Hola mundo!')

holamundo.py 0.3

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/

holamundo.py 0.4

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

holamundo.py 0.5

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

Tarea

Conceptos

Conceptos

Wiki

SQLAlchemy + URL Dispatch Wiki Tutorial

Disponible en la documentación en línea de Pyramid

Preguntas?

http://docs.pylonsproject.org/projects/pyramid/1.0/

IRC: #pyramid en freenode.net (http://webchat.freenode.net/)