mirror of
https://github.com/kennethreitz/clint.git
synced 2026-06-05 23:00:18 +00:00
202 lines
12 KiB
HTML
202 lines
12 KiB
HTML
|
|
<!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> |