forked from ojii/clint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.html
More file actions
202 lines (191 loc) · 11.7 KB
/
Copy pathshell.html
File metadata and controls
202 lines (191 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Working with the Shell — osxpython v0.0.1 documentation</title>
<link rel="stylesheet" href="_static/flasky.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '',
VERSION: '0.0.1',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="osxpython v0.0.1 documentation" href="index.html" />
<link rel="prev" title="Useful Tools" href="useful-tools.html" />
<link media="only screen and (max-device-width: 480px)" href="_static/small_flask.css" type= "text/css" rel="stylesheet" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="useful-tools.html" title="Useful Tools"
accesskey="P">previous</a> |</li>
<li><a href="index.html">osxpython v0.0.1 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="working-with-the-shell">
<h1>Working with the Shell<a class="headerlink" href="#working-with-the-shell" title="Permalink to this headline">¶</a></h1>
<p class="versionadded">
<span class="versionmodified">New in version 0.3.</span></p>
<p>One of the reasons everybody loves Python is the interactive shell. It
basically allows you to execute Python commands in real time and
immediately get results back. Flask itself does not come with an
interactive shell, because it does not require any specific setup upfront,
just import your application and start playing around.</p>
<p>There are however some handy helpers to make playing around in the shell a
more pleasant experience. The main issue with interactive console
sessions is that you’re not triggering a request like a browser does which
means that <tt class="xref py py-data docutils literal"><span class="pre">g</span></tt>, <tt class="xref py py-data docutils literal"><span class="pre">request</span></tt> and others are not
available. But the code you want to test might depend on them, so what
can you do?</p>
<p>This is where some helper functions come in handy. Keep in mind however
that these functions are not only there for interactive shell usage, but
also for unittesting and other situations that require a faked request
context.</p>
<div class="section" id="diving-into-context-locals">
<h2>Diving into Context Locals<a class="headerlink" href="#diving-into-context-locals" title="Permalink to this headline">¶</a></h2>
<p>Say you have a utility function that returns the URL the user should be
redirected to. Imagine it would always redirect to the URL’s <tt class="docutils literal"><span class="pre">next</span></tt>
parameter or the HTTP referrer or the index page:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">request</span><span class="p">,</span> <span class="n">url_for</span>
<span class="k">def</span> <span class="nf">redirect_url</span><span class="p">():</span>
<span class="k">return</span> <span class="n">request</span><span class="o">.</span><span class="n">args</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'next'</span><span class="p">)</span> <span class="ow">or</span> \
<span class="n">request</span><span class="o">.</span><span class="n">referrer</span> <span class="ow">or</span> \
<span class="n">url_for</span><span class="p">(</span><span class="s">'index'</span><span class="p">)</span>
</pre></div>
</div>
<p>As you can see, it accesses the request object. If you try to run this
from a plain Python shell, this is the exception you will see:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">redirect_url</span><span class="p">()</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
<span class="gr">AttributeError</span>: <span class="n">'NoneType' object has no attribute 'request'</span>
</pre></div>
</div>
<p>That makes a lot of sense because we currently do not have a request we
could access. So we have to make a request and bind it to the current
context. The <tt class="xref py py-attr docutils literal"><span class="pre">test_request_context</span></tt> method can create
us a request context:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ctx</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">test_request_context</span><span class="p">(</span><span class="s">'/?next=http://example.com/'</span><span class="p">)</span>
</pre></div>
</div>
<p>This context can be used in two ways. Either with the <cite>with</cite> statement
(which unfortunately is not very handy for shell sessions). The
alternative way is to call the <cite>push</cite> and <cite>pop</cite> methods:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ctx</span><span class="o">.</span><span class="n">push</span><span class="p">()</span>
</pre></div>
</div>
<p>From that point onwards you can work with the request object:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">redirect_url</span><span class="p">()</span>
<span class="go">u'http://example.com/'</span>
</pre></div>
</div>
<p>Until you call <cite>pop</cite>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ctx</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">redirect_url</span><span class="p">()</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
<span class="gr">AttributeError</span>: <span class="n">'NoneType' object has no attribute 'request'</span>
</pre></div>
</div>
</div>
<div class="section" id="firing-before-after-request">
<h2>Firing Before/After Request<a class="headerlink" href="#firing-before-after-request" title="Permalink to this headline">¶</a></h2>
<p>By just creating a request context, you still don’t have run the code that
is normally run before a request. This probably results in your database
being unavailable, the current user not being stored on the
<tt class="xref py py-data docutils literal"><span class="pre">g</span></tt> object etc.</p>
<p>This however can easily be done yourself. Just call
<tt class="xref py py-meth docutils literal"><span class="pre">preprocess_request()</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">ctx</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">test_request_context</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">ctx</span><span class="o">.</span><span class="n">push</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">app</span><span class="o">.</span><span class="n">preprocess_request</span><span class="p">()</span>
</pre></div>
</div>
<p>Keep in mind that the <tt class="xref py py-meth docutils literal"><span class="pre">preprocess_request()</span></tt> function
might return a response object, in that case just ignore it.</p>
<p>To shutdown a request, you need to trick a bit before the after request
functions (triggered by <tt class="xref py py-meth docutils literal"><span class="pre">process_response()</span></tt>) operate on
a response object:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">app</span><span class="o">.</span><span class="n">process_response</span><span class="p">(</span><span class="n">app</span><span class="o">.</span><span class="n">response_class</span><span class="p">())</span>
<span class="go"><Response 0 bytes [200 OK]></span>
<span class="gp">>>> </span><span class="n">ctx</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="further-improving-the-shell-experience">
<h2>Further Improving the Shell Experience<a class="headerlink" href="#further-improving-the-shell-experience" title="Permalink to this headline">¶</a></h2>
<p>If you like the idea of experimenting in a shell, create yourself a module
with stuff you want to star import into your interactive session. There
you could also define some more helper methods for common things such as
initializing the database, dropping tables etc.</p>
<p>Just put them into a module (like <cite>shelltools</cite> and import from there):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">shelltools</span> <span class="kn">import</span> <span class="o">*</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Working with the Shell</a><ul>
<li><a class="reference internal" href="#diving-into-context-locals">Diving into Context Locals</a></li>
<li><a class="reference internal" href="#firing-before-after-request">Firing Before/After Request</a></li>
<li><a class="reference internal" href="#further-improving-the-shell-experience">Further Improving the Shell Experience</a></li>
</ul>
</li>
</ul>
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
<li>Previous: <a href="useful-tools.html" title="previous chapter">Useful Tools</a></li>
</ul></li>
</ul>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/shell.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" size="18" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
© Copyright 2010, Kenneth Reitz.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
</div>
</body>
</html>