Configurator() config.add_route('hello', '/hello/{name}') config.add_view(hello_world, route_name='hello') Create Instance of Pyramid Configuration (no silly global settings) Add a route, name it 'hello' and give it a path {name} part of the path will be available in request. matchdict dictionary Map the 'hello_world' view to 'hello' route
request object specific to your context. from wsgiref.simple_server import make_server from pyramid.config import Configurator from pyramid.view import view_config @view_config(route_name='hello', renderer='string') def hello_world( request ): return 'Hello %s!' % \ request.matchdict['name'] from flask import Flask from flask import \ request In Pyramid, the request object is passed into the view.
app = config.make_wsgi_app() app = Flask(__name__) Configuration and WSGI In Flask, the app serves both as a basic configurator as well as the WSGI app. In Pyramid, the Configurator is explicit, extensible and handles only configuration. It does not really have anything to do with WSGI, but it has a handy method that creates a WSGI app IF you need it.
if __name__ == '__main__': config = Configurator() config.add_route('hello', '/hello/{name}') config.scan() @app.route('/') def hello_world(name): return \ 'Hello %s!' % name if __name__ == '__main__': app.run() Routing and View Configuration In Flask, app.route does A LOT: registers a route, maps to the decorated view, names the route after decorated view In Pyramid all those actions are separate and explicit
if __name__ == '__main__': config = Configurator() config.add_route('hello', /hello/{name}') config.scan() @app.route('/') def hello_world(name): return \ 'Hello %s!' % name if __name__ == '__main__': app.run() Routing and View Configuration ... In Pyramid config.scan() adds the "view_config" decorated view to the registry. You always have to specify route order manually since some routes may conflict.
== '__main__': config = Configurator() config.add_route('hello', '/hello/{name}') config.add_view(hello_world, route_name='hello') @app.route('/') def hello_world(name): return \ 'Hello %s!' % name if __name__ == '__main__': app.run() Routing and View Configuration 3 In Pyramid the more explicit way to register views and routes is "config.add_view". In that case you do not need "config.scan" In Flask, app.route is a shortcut for 'add_url_rule' and certain properties set on the view callable.
app.run() Running the WSGI Server In Flask, app.run() run a built Werkzeug WSGI server In Pyramid, the WSGI server is not built in. It does provide easy integration with wsgiref, cherrypy and waitress and the default scaffolds set it up for you in a pinch.
and the whistles It's as easy as: $ pcreate -s alchemy myapp $ cd myapp $ python ./setup.py develop As you can see Pyramid uses standard Python Packaging
only appears for clients from IP addresses # '127.0.0.1' and '::1'. # debugtoolbar.hosts = 127.0.0.1 ::1 ### # wsgi server configuration ### [server:main] use = egg:waitress#main host = 0.0.0.0