Skip to content
Myr

shell.rst

This file

Path

docs/shell.rst

93 lines · 3.3 KiB

Last changed by

Not known.

History of this file

Take it

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

Also support full qualified test namesArmin Ronacherbefore adoptionf30b117

lines 1–93why, one step back
1.. _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.2829Creating a Request Context30--------------------------3132The easiest way to create a proper request context from the shell is by33using the :attr:`~flask.Flask.test_request_context` method which creates34us a :class:`~flask.ctx.RequestContext`:3536>>> ctx = app.test_request_context()3738Normally you would use the `with` statement to make this request object39active, but in the shell it's easier to use the40:meth:`~flask.ctx.RequestContext.push` and41:meth:`~flask.ctx.RequestContext.pop` methods by hand:4243>>> ctx.push()4445From that point onwards you can work with the request object until you46call `pop`:4748>>> ctx.pop()4950Firing Before/After Request51---------------------------5253By just creating a request context, you still don't have run the code that54is normally run before a request. This might result in your database55being unavailable if you are connecting to the database in a56before-request callback or the current user not being stored on the57: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()7677The 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).818283Further 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 *