Pyramid web framework

38
PYRAMID WEB FRAMEWORK by Nathan Van Gheem

Transcript of Pyramid web framework

Page 1: Pyramid web framework

PYRAMID WEB FRAMEWORKby Nathan Van Gheem

Page 2: Pyramid web framework

ABOUT ME

• @vangheezy

• Wildcard Corp

• Plone core developer

Page 3: Pyramid web framework

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

Page 4: Pyramid web framework

Disclaimer: ’Merica

Page 5: Pyramid web framework

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

Page 6: Pyramid web framework

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

Page 7: Pyramid web framework

•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

Page 8: Pyramid web framework

FLASK BASICS

Page 9: Pyramid web framework

DJANGO BASICS

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

Page 10: Pyramid web framework

PYRAMID BASICS

Page 11: Pyramid web framework

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

Page 12: Pyramid web framework

CONFIGURATION CONTINUED

• Uses ini files for entry point into application configuration

Page 13: Pyramid web framework

CONFIGURATION CONTINUED

• Everything is overridable/extendable

Page 14: Pyramid web framework

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

Page 15: Pyramid web framework

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

Page 16: Pyramid web framework

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

Page 17: Pyramid web framework

VIEWS• Class based views support

• Use renderers to define how output data is processed

• Or, manually create response objects

Page 18: Pyramid web framework

JSON RESPONSES• Provide custom JSON object

serialization

Page 19: Pyramid web framework

RENDERERS• JSON is simple example

• Ships with Chameleon template support

• Support for custom renderers

Page 20: Pyramid web framework

CUSTOM RENDERERS

Page 21: Pyramid web framework

TEMPLATING• Support for many templating languages

• Chameleon: pyramid_chameleon

• Jinja2: pyramid_jinja2

• Genshi: pyramid_genshi

• Mako: pyramid_mako

• Mustache: pyramid_pystache

• others…

Page 22: Pyramid web framework

STATIC ASSETS

• Static assets/files are defined with the configurator instance

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

Page 23: Pyramid web framework

DATA STORAGE• Not provided in core Pyramid, not

opinionated

• Add-on support for many backends

• SQLAlchemy: pyramid_sqlalchemy

• ZODB: pyramid_zodbconn

• MongoDB: pyramid_mongodb

Page 24: Pyramid web framework

ALCHEMY EXAMPLE

• Define model like you would any Alchemy project

Page 25: Pyramid web framework

TRANSACTIONS• pyramid_tm

• Great support for making transactional aware safe code

• Works with SQLAlchemy and ZODB

• Django defaults to auto commit mode(unsafe)

Page 26: Pyramid web framework

DJANGO TRANSACTIONSDefault Django results in potential inconsistent application states

Set: ATOMIC_REQUESTS = TrueOR:

Page 27: Pyramid web framework

FLASK TRANSACTIONS• Need to manually handle in code with

SQLAlchemy machinery

Page 28: Pyramid web framework

ADD-ONS

• Django: INSTALLED_APPS setting

• Flask: wrap app object to use add-on

• Pyramid: config.include(‘mypackage’)

Page 29: Pyramid web framework

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

own subscribers

• Ability to create your own events

Page 30: Pyramid web framework

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.

Page 31: Pyramid web framework

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

Page 32: Pyramid web framework

SCAFFOLDING• Support for building base projects

• Default included scaffolds

• starter

• alchemy

• zodb

Page 33: Pyramid web framework

SECURITY• Built-in support for authentication and

authorization policies

• AuthTKT support

• Can extend default authorization policies

Page 34: Pyramid web framework

EXAMPLE LOGIN/LOGOUT

Page 35: Pyramid web framework

PROTECTING VIEWS• If mapping URLs to resources, you can

protect them for a particular resource

Page 36: Pyramid web framework

SPECIFYING ACL• Specified on resources

Page 37: Pyramid web framework

PYRAMID

• Extensible

• Robust but not opinionated

• Great for large, complex projects

• Many integrations and features

Page 38: Pyramid web framework

QUESTIONS?

• Nathan Van Gheem

• @vangheezy

[email protected]