Download - Pyramid web framework
![Page 1: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/1.jpg)
PYRAMID WEB FRAMEWORKby Nathan Van Gheem
![Page 2: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/2.jpg)
ABOUT ME
• @vangheezy
• Wildcard Corp
• Plone core developer
![Page 3: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/4.jpg)
Disclaimer: ’Merica
![Page 5: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/5.jpg)
• Micro framework• Not opinionated• Extensive use of thread locals• Any database backend• ./bin/pip install flask
![Page 6: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/6.jpg)
•Opinionated•Full stack•RDBMS only•Large community• ./bin/pip install django
![Page 7: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/7.jpg)
•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](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/8.jpg)
FLASK BASICS
![Page 9: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/9.jpg)
DJANGO BASICS
Credits: http://softwaremaniacs.org/blog/2011/01/07/django-micro-framework/en/
![Page 10: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/10.jpg)
PYRAMID BASICS
![Page 11: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/12.jpg)
CONFIGURATION CONTINUED
• Uses ini files for entry point into application configuration
![Page 13: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/13.jpg)
CONFIGURATION CONTINUED
• Everything is overridable/extendable
![Page 14: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/17.jpg)
VIEWS• Class based views support
• Use renderers to define how output data is processed
• Or, manually create response objects
![Page 18: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/18.jpg)
JSON RESPONSES• Provide custom JSON object
serialization
![Page 19: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/19.jpg)
RENDERERS• JSON is simple example
• Ships with Chameleon template support
• Support for custom renderers
![Page 20: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/20.jpg)
CUSTOM RENDERERS
![Page 21: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/24.jpg)
ALCHEMY EXAMPLE
• Define model like you would any Alchemy project
![Page 25: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/25.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/26.jpg)
DJANGO TRANSACTIONSDefault Django results in potential inconsistent application states
Set: ATOMIC_REQUESTS = TrueOR:
![Page 27: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/27.jpg)
FLASK TRANSACTIONS• Need to manually handle in code with
SQLAlchemy machinery
![Page 28: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/28.jpg)
ADD-ONS
• Django: INSTALLED_APPS setting
• Flask: wrap app object to use add-on
• Pyramid: config.include(‘mypackage’)
![Page 29: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/29.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/30.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/31.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/32.jpg)
SCAFFOLDING• Support for building base projects
• Default included scaffolds
• starter
• alchemy
• zodb
![Page 33: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/33.jpg)
SECURITY• Built-in support for authentication and
authorization policies
• AuthTKT support
• Can extend default authorization policies
![Page 34: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/34.jpg)
EXAMPLE LOGIN/LOGOUT
![Page 35: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/35.jpg)
PROTECTING VIEWS• If mapping URLs to resources, you can
protect them for a particular resource
![Page 36: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/36.jpg)
SPECIFYING ACL• Specified on resources
![Page 37: Pyramid web framework](https://reader036.vdocuments.site/reader036/viewer/2022062412/5888a5881a28ab264b8b69bf/html5/thumbnails/37.jpg)
PYRAMID
• Extensible
• Robust but not opinionated
• Great for large, complex projects
• Many integrations and features