shell.rst
This file
Path
docs/shell.rst
103 lines · 3.6 KiB
Last changed by
1.0 -> 0.11 in the docsArmin Ronacherbefore adoptionf9ea3fe
History of this fileTake it
The walk stopped after 200 commits: lines older than bd66710 point at it.
Changelog for #1763Markus Unterwaditzerbefore adoptionbd66710
lines 1–31why, one step back1.. _shell:23Working with the Shell4======================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.2526Generally it's recommended that you read the :ref:`request-context`27chapter of the documentation first.2829Command Line Interface30----------------------311.0 -> 0.11 in the docsArmin Ronacherbefore adoptionf9ea3fe
line 32why, one step back32Starting with Flask 0.11 the recommended way to work with the shell is theChangelog for #1763Markus Unterwaditzerbefore adoptionbd66710
lines 33–103why, one step back33``flask shell`` command which does a lot of this automatically for you.34For instance the shell is automatically initialized with a loaded35application context.3637For more information see :ref:`cli`.3839Creating a Request Context40--------------------------4142The easiest way to create a proper request context from the shell is by43using the :attr:`~flask.Flask.test_request_context` method which creates44us a :class:`~flask.ctx.RequestContext`:4546>>> ctx = app.test_request_context()4748Normally you would use the ``with`` statement to make this request object49active, but in the shell it's easier to use the50:meth:`~flask.ctx.RequestContext.push` and51:meth:`~flask.ctx.RequestContext.pop` methods by hand:5253>>> ctx.push()5455From that point onwards you can work with the request object until you56call `pop`:5758>>> ctx.pop()5960Firing Before/After Request61---------------------------6263By just creating a request context, you still don't have run the code that64is normally run before a request. This might result in your database65being unavailable if you are connecting to the database in a66before-request callback or the current user not being stored on the67:data:`~flask.g` object etc.6869This however can easily be done yourself. Just call70:meth:`~flask.Flask.preprocess_request`:7172>>> ctx = app.test_request_context()73>>> ctx.push()74>>> app.preprocess_request()7576Keep in mind that the :meth:`~flask.Flask.preprocess_request` function77might return a response object, in that case just ignore it.7879To shutdown a request, you need to trick a bit before the after request80functions (triggered by :meth:`~flask.Flask.process_response`) operate on81a response object:8283>>> app.process_response(app.response_class())84<Response 0 bytes [200 OK]>85>>> ctx.pop()8687The functions registered as :meth:`~flask.Flask.teardown_request` are88automatically called when the context is popped. So this is the perfect89place to automatically tear down resources that were needed by the request90context (such as database connections).919293Further Improving the Shell Experience94--------------------------------------9596If you like the idea of experimenting in a shell, create yourself a module97with stuff you want to star import into your interactive session. There98you could also define some more helper methods for common things such as99initializing the database, dropping tables etc.100101Just put them into a module (like `shelltools`) and import from there:102103>>> from shelltools import *