shell.rst
This file
The walk stopped after 200 commits: lines older than bcd746e point at it.
Added another testcase for subdomain supportArmin Ronacherbefore adoptionbcd746e
lines 1–110why, one step back1Working with the Shell2======================34.. versionadded:: 0.356One of the reasons everybody loves Python is the interactive shell. It7basically allows you to execute Python commands in real time and8immediately get results back. Flask itself does not come with an9interactive shell, because it does not require any specific setup upfront,10just import your application and start playing around.1112There are however some handy helpers to make playing around in the shell a13more pleasant experience. The main issue with interactive console14sessions is that you're not triggering a request like a browser does which15means that :data:`~flask.g`, :data:`~flask.request` and others are not16available. But the code you want to test might depend on them, so what17can you do?1819This is where some helper functions come in handy. Keep in mind however20that these functions are not only there for interactive shell usage, but21also for unittesting and other situations that require a faked request22context.2324Diving into Context Locals25--------------------------2627Say you have a utility function that returns the URL the user should be28redirected to. Imagine it would always redirect to the URL's ``next``29parameter or the HTTP referrer or the index page::3031 from flask import request, url_for3233 def redirect_url():34 return request.args.get('next') or \35 request.referrer or \36 url_for('index')3738As you can see, it accesses the request object. If you try to run this39from a plain Python shell, this is the exception you will see:4041>>> redirect_url()42Traceback (most recent call last):43 File "<stdin>", line 1, in <module>44AttributeError: 'NoneType' object has no attribute 'request'4546That makes a lot of sense because we currently do not have a request we47could access. So we have to make a request and bind it to the current48context. The :attr:`~flask.Flask.test_request_context` method can create49us a request context:5051>>> ctx = app.test_request_context('/?next=http://example.com/')5253This context can be used in two ways. Either with the `with` statement54(which unfortunately is not very handy for shell sessions). The55alternative way is to call the `push` and `pop` methods:5657>>> ctx.push()5859From that point onwards you can work with the request object:6061>>> redirect_url()62u'http://example.com/'6364Until you call `pop`:6566>>> ctx.pop()67>>> redirect_url()68Traceback (most recent call last):69 File "<stdin>", line 1, in <module>70AttributeError: 'NoneType' object has no attribute 'request'717273Firing Before/After Request74---------------------------7576By just creating a request context, you still don't have run the code that77is normally run before a request. This probably results in your database78being unavailable, the current user not being stored on the79:data:`~flask.g` object etc.8081This however can easily be done yourself. Just call82:meth:`~flask.Flask.preprocess_request`:8384>>> ctx = app.test_request_context()85>>> ctx.push()86>>> app.preprocess_request()8788Keep in mind that the :meth:`~flask.Flask.preprocess_request` function89might return a response object, in that case just ignore it.9091To shutdown a request, you need to trick a bit before the after request92functions (triggered by :meth:`~flask.Flask.process_response`) operate on93a response object:9495>>> app.process_response(app.response_class())96<Response 0 bytes [200 OK]>97>>> ctx.pop()9899100Further Improving the Shell Experience101--------------------------------------102103If you like the idea of experimenting in a shell, create yourself a module104with stuff you want to star import into your interactive session. There105you could also define some more helper methods for common things such as106initializing the database, dropping tables etc.107108Just put them into a module (like `shelltools` and import from there):109110>>> from shelltools import *