update doxs to use a Configurator context manager
backport of #3119 to 1.9-branch
- Add term "context manager"
- Link context manager term to glossary
- Update example apps to use config context manager
- Use lineno-match instead of linenos
- Update example apps to use config context manager in Quick Tour
- Add a term for context manager in Quick Tour
- Add a term for context manager in Quick Tutorial
| | |
| | | .. code-block:: python |
| | | :linenos: |
| | | |
| | | from pyramid.response import Response # explicit response, no thread local |
| | | from wsgiref.simple_server import make_server # explicitly WSGI |
| | | from wsgiref.simple_server import make_server # explicitly WSGI |
| | | from pyramid.config import Configurator # to configure app registry |
| | | from pyramid.response import Response # explicit response, no threadlocal |
| | | |
| | | def hello_world(request): # accepts a request; no request thread local reqd |
| | | def hello_world(request): # accept a request; no request threadlocal reqd |
| | | # explicit response object means no response threadlocal |
| | | return Response('Hello world!') |
| | | |
| | | if __name__ == '__main__': |
| | | from pyramid.config import Configurator |
| | | config = Configurator() # no global application object |
| | | config.add_view(hello_world) # explicit non-decorator registration |
| | | app = config.make_wsgi_app() # explicitly WSGI |
| | | with Configurator() as config: # no global application object |
| | | config.add_view(hello_world) # explicit non-decorator registration |
| | | app = config.make_wsgi_app() # explicitly WSGI |
| | | server = make_server('0.0.0.0', 8080, app) |
| | | server.serve_forever() # explicitly WSGI |
| | | server.serve_forever() # explicitly WSGI |
| | | |
| | | |
| | | Pyramid doesn't offer pluggable apps |
| | |
| | | Too Complex |
| | | +++++++++++ |
| | | |
| | | If you can understand this hello world program, you can use Pyramid: |
| | | If you can understand this "hello world" program, you can use Pyramid: |
| | | |
| | | .. code-block:: python |
| | | :linenos: |
| | |
| | | return Response('Hello world!') |
| | | |
| | | if __name__ == '__main__': |
| | | config = Configurator() |
| | | config.add_view(hello_world) |
| | | app = config.make_wsgi_app() |
| | | with Configurator() as config: |
| | | config.add_view(hello_world) |
| | | app = config.make_wsgi_app() |
| | | server = make_server('0.0.0.0', 8080, app) |
| | | server.serve_forever() |
| | | |
| | |
| | | side effect |
| | | A statement or function has a side effect when it changes a value outside its own scope. |
| | | Put another way, if one can observe the change made by a function from outside that function, it has a side effect. |
| | | |
| | | context manager |
| | | A context manager is an object that defines the runtime context to be established when executing a :ref:`with <python:with>` statement in Python. The context manager handles the entry into, and the exit from, the desired runtime context for the execution of the block of code. Context managers are normally invoked using the ``with`` statement, but can also be used by directly invoking their methods. Pyramid adds context managers for :class:`pyramid.config.Configurator`, :meth:`pyramid.interfaces.IRouter.request_context`, :func:`pyramid.paster.bootstrap`, :func:`pyramid.scripting.prepare`, and :func:`pyramid.testing.testConfig`. |
| | |
| | | return Response('Hello world!') |
| | | |
| | | if __name__ == '__main__': |
| | | config = Configurator() |
| | | config.add_view(hello_world) |
| | | app = config.make_wsgi_app() |
| | | with Configurator() as config: |
| | | config.add_view(hello_world) |
| | | app = config.make_wsgi_app() |
| | | server = make_server('0.0.0.0', 8080, app) |
| | | server.serve_forever() |
| | | |
| | |
| | | return Response('Hello') |
| | | |
| | | if __name__ == '__main__': |
| | | config = Configurator() |
| | | config.scan() |
| | | app = config.make_wsgi_app() |
| | | with Configurator() as config: |
| | | config.scan() |
| | | app = config.make_wsgi_app() |
| | | server = make_server('0.0.0.0', 8080, app) |
| | | server.serve_forever() |
| | | |
| | |
| | | The above ``helloworld.py`` script uses the following set of import statements: |
| | | |
| | | .. literalinclude:: helloworld.py |
| | | :linenos: |
| | | :lineno-match: |
| | | :lines: 1-3 |
| | | |
| | | The script imports the :class:`~pyramid.config.Configurator` class from the |
| | |
| | | ``hello_world``. |
| | | |
| | | .. literalinclude:: helloworld.py |
| | | :linenos: |
| | | :lineno-match: |
| | | :pyobject: hello_world |
| | | |
| | | The function accepts a single argument (``request``) and it returns an instance |
| | |
| | | statement: |
| | | |
| | | .. literalinclude:: helloworld.py |
| | | :linenos: |
| | | :lineno-match: |
| | | :lines: 9-15 |
| | | |
| | | Let's break this down piece by piece. |
| | |
| | | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
| | | |
| | | .. literalinclude:: helloworld.py |
| | | :linenos: |
| | | :lineno-match: |
| | | :lines: 9-10 |
| | | |
| | | The ``if __name__ == '__main__':`` line in the code sample above represents a |
| | |
| | | another; the code within the ``if`` block should only be run during a direct |
| | | script execution. |
| | | |
| | | The ``config = Configurator()`` line above creates an instance of the |
| | | :class:`~pyramid.config.Configurator` class. The resulting ``config`` object |
| | | The ``with Configurator() as config:`` line above creates an instance of the |
| | | :class:`~pyramid.config.Configurator` class using a :term:`context manager`. The resulting ``config`` object |
| | | represents an API which the script uses to configure this particular |
| | | :app:`Pyramid` application. Methods called on the Configurator will cause |
| | | registrations to be made in an :term:`application registry` associated with the |
| | |
| | | ~~~~~~~~~~~~~~~~~~~~ |
| | | |
| | | .. literalinclude:: helloworld.py |
| | | :linenos: |
| | | :lineno-match: |
| | | :lines: 11-12 |
| | | |
| | | The first line above calls the :meth:`pyramid.config.Configurator.add_route` |
| | |
| | | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
| | | |
| | | .. literalinclude:: helloworld.py |
| | | :linenos: |
| | | :lineno-match: |
| | | :lines: 13 |
| | | |
| | | After configuring views and ending configuration, the script creates a WSGI |
| | |
| | | ~~~~~~~~~~~~~~~~~~~~~~~~ |
| | | |
| | | .. literalinclude:: helloworld.py |
| | | :linenos: |
| | | :lineno-match: |
| | | :lines: 14-15 |
| | | |
| | | Finally, we actually serve the application to requestors by starting up a WSGI |
| | |
| | | return Response('Hello %(name)s!' % request.matchdict) |
| | | |
| | | if __name__ == '__main__': |
| | | config = Configurator() |
| | | config.add_route('hello', '/hello/{name}') |
| | | config.add_view(hello_world, route_name='hello') |
| | | app = config.make_wsgi_app() |
| | | with Configurator() as config: |
| | | config.add_route('hello', '/hello/{name}') |
| | | config.add_view(hello_world, route_name='hello') |
| | | app = config.make_wsgi_app() |
| | | server = make_server('0.0.0.0', 8080, app) |
| | | server.serve_forever() |
| | | |
| | |
| | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| | | |
| | | An alternative style of setting up a test configuration is to use the ``with`` |
| | | statement and :func:`pyramid.testing.testConfig` to create a context manager. |
| | | statement and :func:`pyramid.testing.testConfig` to create a :term:`context manager`. |
| | | The context manager will call :func:`pyramid.testing.setUp` before the code |
| | | under test and :func:`pyramid.testing.tearDown` afterwards. |
| | | |
| | |
| | | #. *Line 10*. ``if __name__ == '__main__':`` is Python's way of saying "Start |
| | | here when running from the command line". |
| | | |
| | | #. *Lines 11-13*. Use Pyramid's :term:`configurator` to connect :term:`view` |
| | | #. *Lines 11-13*. Use Pyramid's :term:`configurator` in a :term:`context manager` to connect :term:`view` |
| | | code to a particular URL :term:`route`. |
| | | |
| | | #. *Lines 6-7*. Implement the view code that generates the :term:`response`. |
| | |
| | | |
| | | |
| | | if __name__ == '__main__': |
| | | config = Configurator() |
| | | config.add_route('hello', '/') |
| | | config.add_view(hello_world, route_name='hello') |
| | | app = config.make_wsgi_app() |
| | | 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() |
| | |
| | | from pyramid.config import Configurator |
| | | |
| | | if __name__ == '__main__': |
| | | config = Configurator() |
| | | config.add_route('hello', '/howdy/{name}') |
| | | config.include('pyramid_jinja2') |
| | | config.scan('views') |
| | | app = config.make_wsgi_app() |
| | | with Configurator() as config: |
| | | config.add_route('hello', '/howdy/{name}') |
| | | config.include('pyramid_jinja2') |
| | | config.scan('views') |
| | | app = config.make_wsgi_app() |
| | | server = make_server('0.0.0.0', 6543, app) |
| | | server.serve_forever() |
| | |
| | | from pyramid.config import Configurator |
| | | |
| | | if __name__ == '__main__': |
| | | config = Configurator() |
| | | config.add_route('hello', '/howdy/{name}') |
| | | config.add_route('hello_json', 'hello.json') |
| | | config.add_static_view(name='static', path='static') |
| | | config.include('pyramid_jinja2') |
| | | config.scan('views') |
| | | app = config.make_wsgi_app() |
| | | with Configurator() as config: |
| | | config.add_route('hello', '/howdy/{name}') |
| | | config.add_route('hello_json', 'hello.json') |
| | | config.add_static_view(name='static', path='static') |
| | | config.include('pyramid_jinja2') |
| | | config.scan('views') |
| | | app = config.make_wsgi_app() |
| | | server = make_server('0.0.0.0', 6543, app) |
| | | server.serve_forever() |
| | |
| | | |
| | | |
| | | if __name__ == '__main__': |
| | | config = Configurator() |
| | | config.add_route('hello', '/') |
| | | config.add_view(hello_world, route_name='hello') |
| | | app = config.make_wsgi_app() |
| | | 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() |
| | |
| | | from pyramid.config import Configurator |
| | | |
| | | if __name__ == '__main__': |
| | | config = Configurator() |
| | | config.add_route('hello', '/howdy/{first}/{last}') |
| | | config.scan('views') |
| | | app = config.make_wsgi_app() |
| | | with Configurator() as config: |
| | | config.add_route('hello', '/howdy/{first}/{last}') |
| | | config.scan('views') |
| | | app = config.make_wsgi_app() |
| | | server = make_server('0.0.0.0', 6543, app) |
| | | server.serve_forever() |
| | | server.serve_forever() |
| | |
| | | from pyramid.config import Configurator |
| | | |
| | | if __name__ == '__main__': |
| | | config = Configurator() |
| | | config.add_route('hello', '/howdy/{name}') |
| | | config.add_static_view(name='static', path='static') |
| | | config.include('pyramid_jinja2') |
| | | config.scan('views') |
| | | app = config.make_wsgi_app() |
| | | with Configurator() as config: |
| | | config.add_route('hello', '/howdy/{name}') |
| | | config.add_static_view(name='static', path='static') |
| | | config.include('pyramid_jinja2') |
| | | config.scan('views') |
| | | app = config.make_wsgi_app() |
| | | server = make_server('0.0.0.0', 6543, app) |
| | | server.serve_forever() |
| | |
| | | from pyramid.config import Configurator |
| | | |
| | | if __name__ == '__main__': |
| | | config = Configurator() |
| | | config.add_route('hello', '/howdy/{name}') |
| | | config.include('pyramid_chameleon') |
| | | config.scan('views') |
| | | app = config.make_wsgi_app() |
| | | with Configurator() as config: |
| | | config.add_route('hello', '/howdy/{name}') |
| | | config.include('pyramid_chameleon') |
| | | config.scan('views') |
| | | app = config.make_wsgi_app() |
| | | server = make_server('0.0.0.0', 6543, app) |
| | | server.serve_forever() |
| | |
| | | from pyramid.config import Configurator |
| | | |
| | | if __name__ == '__main__': |
| | | config = Configurator() |
| | | config.add_route('hello', '/howdy/{name}') |
| | | config.add_route('hello_json', 'hello.json') |
| | | config.add_static_view(name='static', path='static') |
| | | config.include('pyramid_jinja2') |
| | | config.scan('views') |
| | | app = config.make_wsgi_app() |
| | | with Configurator() as config: |
| | | config.add_route('hello', '/howdy/{name}') |
| | | config.add_route('hello_json', 'hello.json') |
| | | config.add_static_view(name='static', path='static') |
| | | config.include('pyramid_jinja2') |
| | | config.scan('views') |
| | | app = config.make_wsgi_app() |
| | | server = make_server('0.0.0.0', 6543, app) |
| | | server.serve_forever() |
| | |
| | | from pyramid.config import Configurator |
| | | |
| | | if __name__ == '__main__': |
| | | config = Configurator() |
| | | config.add_route('home', '/') |
| | | config.add_route('hello', '/howdy') |
| | | config.add_route('redirect', '/goto') |
| | | config.add_route('exception', '/problem') |
| | | config.scan('views') |
| | | app = config.make_wsgi_app() |
| | | with Configurator() as config: |
| | | config.add_route('home', '/') |
| | | config.add_route('hello', '/howdy') |
| | | config.add_route('redirect', '/goto') |
| | | config.add_route('exception', '/problem') |
| | | config.scan('views') |
| | | app = config.make_wsgi_app() |
| | | server = make_server('0.0.0.0', 6543, app) |
| | | server.serve_forever() |
| | |
| | | "Start here when running from the command line", rather than when this |
| | | module is imported. |
| | | |
| | | #. *Lines 12-14*. Use Pyramid's :term:`configurator` to connect :term:`view` |
| | | #. *Lines 12-14*. Use Pyramid's :term:`configurator` in a :term:`context manager` to connect :term:`view` |
| | | code to a particular URL :term:`route`. |
| | | |
| | | #. *Lines 6-8*. Implement the view code that generates the :term:`response`. |
| | |
| | | |
| | | |
| | | if __name__ == '__main__': |
| | | config = Configurator() |
| | | config.add_route('hello', '/') |
| | | config.add_view(hello_world, route_name='hello') |
| | | app = config.make_wsgi_app() |
| | | 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() |
| | |
| | | |
| | | |
| | | def hello_world(request): |
| | | print ('Incoming request') |
| | | print('Incoming request') |
| | | return Response('<body><h1>Hello World!</h1></body>') |
| | | |
| | | |
| | | if __name__ == '__main__': |
| | | config = Configurator() |
| | | config.add_route('hello', '/') |
| | | config.add_view(hello_world, route_name='hello') |
| | | app = config.make_wsgi_app() |
| | | 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() |
| | | server.serve_forever() |