from wsgiref.simple_server import make_serverfrom pyramid.config import Configuratorfrom pyramid.response import Responsedef hello_world(request): return Response('Hello World!')if __name__ == '__main__': with Configurator() as config: config.add_route('hello', '/') config.add_view(hello_world, route_name='hello') app = config.make_wsgi_app() server = make_server('0.0.0.0', 6543, app) server.serve_forever()
Pyramid makes it easy to write web applications. You canstart small with this "hello world" minimal request/response web app. This may take you far, especially while learning. As your application grows, Pyramid offers many features that make writing complex software take less effort.
Pyramid works in all supported versions of Python. Ourinstallation instructions will help you get Pyramid up and running.
Pyramid'squick tutorial will take you step by step through writing a single file application, forms, database integration, and authentication.
Developers may dive in to Pyramid'snarrative documentation, or browse the extensiveAPI reference. Pyramid has a rich pool of helpfulresources from which to draw.Extending Pyramid is a curated and filterable list of add-ons, packages, and applications built to work with Pyramid.
Megaframeworks make decisions for you. But if you don't fit their viewpoint, you end up fighting their decisions.Microframeworks force no decisions, making it easy to start. But as your application grows, you're on your own.
In both cases, the focus is on thestart: either too much or too little. Either way, finishing and staying finished is hard. You need afinishing-focused framework with an architectural design that scales down to getting started, then up as your application grows.
Pyramid was made for just this. It's a Goldilocks Solution: not too small, not too big, just right.
Pyramid The StartSmall, FinishBig, StayFinished Framework.
Getting started quickly and simply is a key attraction of lightweight frameworks. Equally, you get to choose what approaches to use for templating, database, security, and more, or use a convenient starting point with a scaffold. Pyramid excels at scaling down to the first hour of learning, while avoiding the pitfalls of framework magic.
Ambitious projects aspire to grow big without losing their dignity. Pyramid is uniquely equipped to scale with you. Its configuration, extension, and add-on system gives the skeleton to support your ambitions, and its architecture ensures that you don't lose those performance and quality features you started with.
Pyramid's simple first hour helps you get started and its extensability helps you finish your ambitions. There's life after shipping. Pyramid helps keep your application finished by understanding the full life cycle of a professional web application.
Full-stack frameworks provide built-in value by telling you what to do. But doing something different, or using something better, leads to the dreaded "fighting the framework". Pyramid starts from a very small base, providing many high-quality choices.
The Pyramid team has been doing ambitious Python web frameworks since 1995. We have built small systems and huge systems. From this, we delight in helping others who appreciate quality and dream big.
To demonstrate these features,install Pyramid, click to expand and copy the code sample into a file, run the application withenv/bin/python demo.py
, and usecurl
or a browser to requesthttp://0.0.0.0:6543.
from wsgiref.simple_server import make_serverfrom pyramid.config import Configuratorfrom pyramid.response import Responsefrom pyramid.view import view_config@view_config( route_name='home')def home(request): return Response('Welcome!')if __name__ == '__main__': with Configurator() as config: config.add_route('home', '/') config.scan() app = config.make_wsgi_app() server = make_server('0.0.0.0', 6543, app) server.serve_forever()
Pyramid is written in Python. All the features you know and love in the Python language, such as function decorators, are available to Pyramid developers. Here we show the function namedhome
that returns a response. The function has a decorator@view_config
which has a route assigned to it also namedhome
.
from wsgiref.simple_server import make_serverfrom pyramid.config import Configuratorfrom pyramid.response import Responsefrom pyramid.view import view_config@view_config( route_name='home', request_method='POST')def home(request): return Response('Welcome!')if __name__ == '__main__': with Configurator() as config: config.add_route('home', '/') config.scan() app = config.make_wsgi_app() server = make_server('0.0.0.0', 6543, app) server.serve_forever()
A test which returnsTrue
orFalse
, and which narrows the set of circumstances in which views or routes may be called. Here we use predicates to limit matching of a view callable to a route name ofhome
and to thePOST
HTTP request method.
from wsgiref.simple_server import make_serverfrom pyramid.config import Configuratorfrom pyramid.view import view_config@view_config( route_name='home', renderer='json')def home(request): return {"a": 1, "b": 2}if __name__ == '__main__': with Configurator() as config: config.add_route('home', '/') config.scan() app = config.make_wsgi_app() server = make_server('0.0.0.0', 6543, app) server.serve_forever()
Convert non-response return values that are later consumed by renderers. Using a renderer can make writing views that require templating or other serialization, likeJSON
, less tedious.
"""1. Run `env/bin/pip install pyramid_jinja2`2. Copy this template and put it in `templates/home.jinja2`:<!DOCTYPE html><html lang="en"><head> <title>{{ greet }}, {{ name }}</title></head><body><h1>{{ greet }}, {{ name }}</h1></body></html>"""from wsgiref.simple_server import make_serverfrom pyramid.config import Configuratorfrom pyramid.view import view_config@view_config( route_name='home', renderer='templates/home.jinja2')def home(request): return {"greet": 'Welcome', "name": 'Akhenaten'}if __name__ == '__main__': with Configurator() as config: config.include('pyramid_jinja2') config.add_route('home', '/') config.scan() app = config.make_wsgi_app() server = make_server('0.0.0.0', 6543, app) server.serve_forever()
Allows specifying the location of assets in a package. Here the asset is specified as a Jinja2 templatehome.jinja2
, located in a subdirectory namedtemplates
. Within a packagemyapp
, a colon delimits the package name from the location of the asset relative to the package, for examplerenderer='myapp:templates/home.jinja2'
.
from wsgiref.simple_server import make_serverfrom pyramid.config import Configuratorfrom pyramid.events import NewRequestfrom pyramid.events import NewResponsefrom pyramid.events import subscriberfrom pyramid.response import Responsefrom pyramid.view import view_config@view_config( route_name='home',)def home(request): return Response('Welcome!')@subscriber(NewRequest, NewResponse)def mysubscriber(event): print(event)if __name__ == '__main__': with Configurator() as config: config.add_route('home', '/') config.scan() app = config.make_wsgi_app() server = make_server('0.0.0.0', 6543, app) server.serve_forever()
An event is an object broadcast at interesting points during the lifetime of an application. A subscriber to an event allows you to run some code, such as resizing an uploaded image, sending email, or sending a message to a remote system. Here the decorated subscriber will be called for more than one event type, specifically for every new request and response objects.