Pyramid web framework

Post on 25-Jan-2017

90 views 0 download

transcript

PYRAMID WEB FRAMEWORKby Nathan Van Gheem

ABOUT ME

• @vangheezy

• Wildcard Corp

• Plone core developer

GOALS• Convince you Pyramid is a superior

framework

• Niche market is complex web applications

• If you are building serious applications that don’t fit standard Django, Flask paradigms well, Pyramid is for you

Disclaimer: ’Merica

• Micro framework• Not opinionated• Extensive use of thread locals• Any database backend• ./bin/pip install flask

•Opinionated•Full stack•RDBMS only•Large community• ./bin/pip install django

•Runs on Python 2, 3•Not opinionated but provides many tools•Robust•Modular/extendable/overridable•Roots in Plone/Zope community, lessons learned from Zope mistakes• ./bin/pip install pyramid

FLASK BASICS

DJANGO BASICS

Credits: http://softwaremaniacs.org/blog/2011/01/07/django-micro-framework/en/

PYRAMID BASICS

CONFIGURATION• Main WSGI function to create application

• Uses a Configuration object

• Register components for various aspects

• Components can be overridden and/or extended

• Settings are pulled in from configuration file

CONFIGURATION CONTINUED

• Uses ini files for entry point into application configuration

CONFIGURATION CONTINUED

• Everything is overridable/extendable

URL MAPPING• Flask couples route with associated

callable

• Django and Pyramid de-couple route and callable

• Django: to override route, provide different list of url patterns

• Pyramid: to override route, register new route in configurator instance

TRAVERSAL• URL mapping based on object tree• Dynamic URL structure

•http://localhost:6543/foo/another/level - 200 OK•http://localhost:6543/donald/trump - 404 NOT FOUND

TRAVERSAL CONTINUED• Objects can be any

object type

• Finding leaf node in object tree is done by __getitem__

• Can match routes with interfaces, use inheritance, etc

• ZODB traversal tutorial: http://docs.pylonsproject.org/projects/pyramid/en/latest/tutorials/wiki/index.html

VIEWS• Class based views support

• Use renderers to define how output data is processed

• Or, manually create response objects

JSON RESPONSES• Provide custom JSON object

serialization

RENDERERS• JSON is simple example

• Ships with Chameleon template support

• Support for custom renderers

CUSTOM RENDERERS

TEMPLATING• Support for many templating languages

• Chameleon: pyramid_chameleon

• Jinja2: pyramid_jinja2

• Genshi: pyramid_genshi

• Mako: pyramid_mako

• Mustache: pyramid_pystache

• others…

STATIC ASSETS

• Static assets/files are defined with the configurator instance

• Maps: http://localhost/static/logo.png• To: /var/www/static/logo.png

DATA STORAGE• Not provided in core Pyramid, not

opinionated

• Add-on support for many backends

• SQLAlchemy: pyramid_sqlalchemy

• ZODB: pyramid_zodbconn

• MongoDB: pyramid_mongodb

ALCHEMY EXAMPLE

• Define model like you would any Alchemy project

TRANSACTIONS• pyramid_tm

• Great support for making transactional aware safe code

• Works with SQLAlchemy and ZODB

• Django defaults to auto commit mode(unsafe)

DJANGO TRANSACTIONSDefault Django results in potential inconsistent application states

Set: ATOMIC_REQUESTS = TrueOR:

FLASK TRANSACTIONS• Need to manually handle in code with

SQLAlchemy machinery

ADD-ONS

• Django: INSTALLED_APPS setting

• Flask: wrap app object to use add-on

• Pyramid: config.include(‘mypackage’)

EVENTS/HOOKS• Pyramid has many event you can hook into with your

own subscribers

• Ability to create your own events

TWEENS• Tweens give you the ability to provide application request/response

wrapper functionality

• Allows you to customize the request before it is routed and the response after it has been completed.

COMMAND LINE TOOLING• bin/pcreate: create projects from scaffolds

• bin/pdistreport: show python package version information

• bin/prequest: submit fake request to application

• bin/proutes: print all URL dispatch routes registered

• bin/pserve: server WSGI application from ini file

• bin/pshell: provide shell for interacting with application

• bin/ptweens: print all tweens used by application

• bin/pviews: print views that might match for a given URL

SCAFFOLDING• Support for building base projects

• Default included scaffolds

• starter

• alchemy

• zodb

SECURITY• Built-in support for authentication and

authorization policies

• AuthTKT support

• Can extend default authorization policies

EXAMPLE LOGIN/LOGOUT

PROTECTING VIEWS• If mapping URLs to resources, you can

protect them for a particular resource

SPECIFYING ACL• Specified on resources

PYRAMID

• Extensible

• Robust but not opinionated

• Great for large, complex projects

• Many integrations and features

QUESTIONS?

• Nathan Van Gheem

• @vangheezy

• vangheem@gmail.com