Skip to content
Myr

shell.rst

This file

Path

docs/shell.rst

93 lines · 3.3 KiB

Last changed by

Merge branch 'new-request-dispatching' into blueprintsArmin Ronacherbefore adoption673fa18

History of this file

Take it

The walk stopped after 200 commits: lines older than 2912ff6 point at it.

Merge branch 'new-request-dispatching' into blueprintsArmin Ronacherbefore adoption673fa18

lines 1–2why, one step back
1.. _shell:
2

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 3–25why, one step back
3Working with the Shell
4======================56.. versionadded:: 0.378One of the reasons everybody loves Python is the interactive shell. It9basically allows you to execute Python commands in real time and10immediately get results back. Flask itself does not come with an11interactive shell, because it does not require any specific setup upfront,12just import your application and start playing around.1314There are however some handy helpers to make playing around in the shell a15more pleasant experience. The main issue with interactive console16sessions is that you're not triggering a request like a browser does which17means that :data:`~flask.g`, :data:`~flask.request` and others are not18available. But the code you want to test might depend on them, so what19can you do?2021This is where some helper functions come in handy. Keep in mind however22that these functions are not only there for interactive shell usage, but23also for unittesting and other situations that require a faked request24context.25

Merge branch 'new-request-dispatching' into blueprintsArmin Ronacherbefore adoption673fa18

lines 26–29why, one step back
26Generally it's recommended that you read the :ref:`request-context`
27chapter of the documentation first.2829Creating a Request Context

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 30–31why, one step back
30--------------------------
31

Merge branch 'new-request-dispatching' into blueprintsArmin Ronacherbefore adoption673fa18

lines 32–34why, one step back
32The easiest way to create a proper request context from the shell is by
33using the :attr:`~flask.Flask.test_request_context` method which creates34us a :class:`~flask.ctx.RequestContext`:

Added some whitespaceArmin Ronacherbefore adoption2912ff6

line 35why, one step back
35

Merge branch 'new-request-dispatching' into blueprintsArmin Ronacherbefore adoption673fa18

line 36why, one step back
36>>> ctx = app.test_request_context()

Added some whitespaceArmin Ronacherbefore adoption2912ff6

line 37why, one step back
37

Merge branch 'new-request-dispatching' into blueprintsArmin Ronacherbefore adoption673fa18

lines 38–41why, one step back
38Normally you would use the `with` statement to make this request object
39active, but in the shell it's easier to use the40:meth:`~flask.ctx.RequestContext.push` and41:meth:`~flask.ctx.RequestContext.pop` methods by hand:

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 42–44why, one step back
42
43>>> ctx.push()44

Merge branch 'new-request-dispatching' into blueprintsArmin Ronacherbefore adoption673fa18

lines 45–46why, one step back
45From that point onwards you can work with the request object until you
46call `pop`:

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 47–53why, one step back
47
48>>> ctx.pop()4950Firing Before/After Request51---------------------------5253By just creating a request context, you still don't have run the code that

Merge branch 'new-request-dispatching' into blueprintsArmin Ronacherbefore adoption673fa18

lines 54–56why, one step back
54is normally run before a request. This might result in your database
55being unavailable if you are connecting to the database in a56before-request callback or the current user not being stored on the

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 57–75why, one step back
57:data:`~flask.g` object etc.
5859This however can easily be done yourself. Just call60:meth:`~flask.Flask.preprocess_request`:6162>>> ctx = app.test_request_context()63>>> ctx.push()64>>> app.preprocess_request()6566Keep in mind that the :meth:`~flask.Flask.preprocess_request` function67might return a response object, in that case just ignore it.6869To shutdown a request, you need to trick a bit before the after request70functions (triggered by :meth:`~flask.Flask.process_response`) operate on71a response object:7273>>> app.process_response(app.response_class())74<Response 0 bytes [200 OK]>75>>> ctx.pop()

Merge branch 'new-request-dispatching' into blueprintsArmin Ronacherbefore adoption673fa18

lines 76–80why, one step back
76
77The functions registered as :meth:`~flask.Flask.teardown_request` are78automatically called when the context is popped. So this is the perfect79place to automatically tear down resources that were needed by the request80context (such as database connections).

Added some whitespaceArmin Ronacherbefore adoption2912ff6

lines 81–93why, one step back
81
8283Further Improving the Shell Experience84--------------------------------------8586If you like the idea of experimenting in a shell, create yourself a module87with stuff you want to star import into your interactive session. There88you could also define some more helper methods for common things such as89initializing the database, dropping tables etc.9091Just put them into a module (like `shelltools` and import from there):9293>>> from shelltools import *