This commit is contained in:
Kenneth Reitz
2010-08-08 18:40:45 -04:00
parent b501abcabd
commit 0375406fc2
9 changed files with 151 additions and 89 deletions
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -6,6 +6,6 @@
Welcome to OSXPython.org
=====================================
Welcome to OSXPython.org. This site exists to provide novice and expert Python developers using OS X a best-practice handbook to the installation, configuration, and usage of Python on a daily basis.
Welcome to OSXPython.org. This site exists to provide both novice and expert Python developers using OS X a best-practice handbook to the installation, configuration, and usage of Python on a daily basis.
.. include:: contents.rst.inc
+25 -2
View File
@@ -22,6 +22,7 @@
<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="#" />
<link rel="next" title="Properly Installing Python" href="installation.html" />
<link media="only screen and (max-device-width: 480px)" href="_static/small_flask.css" type= "text/css" rel="stylesheet" />
@@ -34,6 +35,9 @@
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="installation.html" title="Properly Installing Python"
accesskey="N">next</a> |</li>
<li><a href="#">osxpython v0.0.1 documentation</a> &raquo;</li>
</ul>
</div>
@@ -45,14 +49,32 @@
<div class="section" id="welcome-to-osxpython-org">
<h1>Welcome to OSXPython.org<a class="headerlink" href="#welcome-to-osxpython-org" title="Permalink to this headline"></a></h1>
<p>Welcome to OSXPython.org. This site exists to provide novice and expert Python developers using OS X a best-practice handbook to the installation, configuration, and usage of Python on a daily basis.</p>
<p>Welcome to OSXPython.org. This site exists to provide both novice and expert Python developers using OS X a best-practice handbook to the installation, configuration, and usage of Python on a daily basis.</p>
<div class="section" id="user-s-guide">
<h2>User&#8217;s Guide<a class="headerlink" href="#user-s-guide" title="Permalink to this headline"></a></h2>
<p>This part of the documentation, which is mostly prose, begins with some
background information about Flask, then focuses on step-by-step
instructions for web development with Flask.</p>
<div class="toctree-wrapper compound">
<ul class="simple">
<ul>
<li class="toctree-l1"><a class="reference internal" href="installation.html">Properly Installing Python</a><ul>
<li class="toctree-l2"><a class="reference internal" href="installation.html#package-manager">Package Manager</a></li>
<li class="toctree-l2"><a class="reference internal" href="installation.html#python-interpreter">Python Interpreter</a></li>
<li class="toctree-l2"><a class="reference internal" href="installation.html#distribute-pip">Distribute &amp; Pip</a></li>
<li class="toctree-l2"><a class="reference internal" href="installation.html#further-improving-the-shell-experience">Further Improving the Shell Experience</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="useful-tools.html">Useful Tools</a><ul>
<li class="toctree-l2"><a class="reference internal" href="useful-tools.html#ipython">IPython</a></li>
<li class="toctree-l2"><a class="reference internal" href="useful-tools.html#bpython">BPython</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="shell.html">Working with the Shell</a><ul>
<li class="toctree-l2"><a class="reference internal" href="shell.html#diving-into-context-locals">Diving into Context Locals</a></li>
<li class="toctree-l2"><a class="reference internal" href="shell.html#firing-before-after-request">Firing Before/After Request</a></li>
<li class="toctree-l2"><a class="reference internal" href="shell.html#further-improving-the-shell-experience">Further Improving the Shell Experience</a></li>
</ul>
</li>
</ul>
</div>
</div>
@@ -99,6 +121,7 @@ method, this part of the documentation is for you.</p>
<h3>Related Topics</h3>
<ul>
<li><a href="#">Documentation overview</a><ul>
<li>Next: <a href="installation.html" title="next chapter">Properly Installing Python</a></li>
</ul></li>
</ul>
<h3>This Page</h3>
+1 -1
View File
@@ -1 +1 @@
Search.setIndex({objects:{},terms:{code:[1,2],execut:[1,2],thi:[0,1,2],becaus:[1,2],session:[1,2],still:[1,2],process_respons:[1,2],paramet:[1,2],redirect:[1,2],web:0,onli:[1,2],depend:[1,2],just:[1,2],flask:[0,1,2],configur:0,except:[1,2],should:[1,2],common:[1,2],sens:[1,2],main:[1,2],might:[1,2],unittest:[1,2],real:[1,2],applic:[1,2],them:[1,2],"return":[1,2],around:[1,2],get:[1,2],python:[0,1,2],handi:[1,2],initi:[1,2],focus:0,util:[1,2],framework:2,background:0,veri:[1,2],bit:[1,2],now:2,requir:[1,2],like:[1,2],install_homebrew:2,specif:[0,1,2],changelog:0,drop:[1,2],probabl:[1,2],"try":[1,2],easili:[1,2],stuff:[1,2],either:[1,2],plain:[1,2],attribut:[1,2],where:[1,2],page:[1,2],mean:[1,2],imagin:[1,2],some:[0,1,2],back:[1,2],see:[1,2],design:0,result:[1,2],arg:[1,2],further:[1,2],best:0,index:[1,2],what:[1,2],expert:0,databas:[1,2],legal:0,your:[1,2],content:[],nonetyp:[1,2],"import":[1,2],experi:[1,2],method:[0,1,2],refer:0,altern:[1,2],after:[1,2],run:[1,2],osxpython:0,situat:[1,2],here:0,pleasant:[1,2],step:0,upfront:[1,2],put:[1,2],org:0,"byte":[1,2],come:[1,2],addit:0,about:0,last:[1,2],wai:[1,2],befor:[1,2],attributeerror:[1,2],howev:[1,2],current:[1,2],ctx:[1,2],reason:[1,2],preprocess_request:[1,2],keep:[1,2],etc:[1,2],response_class:[1,2],redirect_url:[1,2],shelltool:[1,2],improv:[1,2],com:[1,2],oper:[1,2],onward:[1,2],love:[1,2],point:[1,2],app:[1,2],pop:[1,2],yourself:[1,2],instruct:0,api:0,done:[1,2],shutdown:[1,2],fake:[1,2],instal:[0,2],guid:0,referr:[1,2],avail:[1,2],from:[1,2],creat:[1,2],would:[1,2],interact:[1,2],two:[1,2],next:[1,2],everybodi:[1,2],start:[1,2],trigger:[1,2],handbook:0,statement:[1,2],basic:[1,2],call:[1,2],immedi:[1,2],until:[1,2],store:[1,2],more:[1,2],"function":[0,1,2],shell:[1,2],consol:[1,2],interest:0,fire:[1,2],rubi:2,search:[],idea:[1,2],part:0,push:[1,2],mostli:0,don:[1,2],line:[1,2],"case":[1,2],url_for:[1,2],look:0,provid:0,local:[1,2],setup:[1,2],work:[1,2],exampl:[1,2],trick:[1,2],defin:[1,2],can:[1,2],site:0,modul:[1,2],star:[1,2],could:[1,2],def:[1,2],browser:[1,2],novic:0,sai:[1,2],traceback:[1,2],file:[1,2],helper:[1,2],sudo:2,mind:[1,2],ani:[1,2],indic:[],raw:2,itself:[1,2],exist:0,unavail:[1,2],have:[1,2],tabl:[1,2],prose:0,curl:2,unfortun:[1,2],gist:2,alwai:[1,2],develop:0,welcom:0,want:[1,2],thing:[1,2],titl:[],make:[1,2],access:[1,2],note:0,also:[1,2],need:[1,2],other:[1,2],which:[0,1,2],test:[1,2],ignor:[1,2],you:[0,1,2],document:0,basi:0,begin:0,http:[1,2],context:[1,2],normal:[1,2],dive:[1,2],object:[1,2],test_request_context:[1,2],most:[1,2],plai:[1,2],lot:[1,2],user:[0,1,2],respons:[1,2],"class":0,recent:[1,2],github:2,usag:[0,1,2],practic:0,url:[1,2],bind:[1,2],stdin:[1,2],request:[1,2],doe:[1,2],issu:[1,2],inform:0,fss:2,command:[1,2],allow:[1,2],time:[1,2],brew:2,daili:0},objtypes:{},titles:["Welcome to OSXPython.org","Working with the Shell","Working with the Shell"],objnames:{},filenames:["index","shell","installation"]})
Search.setIndex({objects:{},terms:{code:[1,2],focus:0,sunt:[],follow:3,depend:1,bpython:[0,2],flask:[0,1,2,3],cupidatat:[],aut:[],sens:1,sourc:3,distribute_setup:[3,2],util:[3,1,2],consequat:[],enviorn:3,veri:1,install_homebrew:3,"try":[1,2],design:0,further:[0,1,3],index:[1,2],what:1,current:[1,2],version:3,nonetyp:1,mollit:[],method:[0,1,3],sint:[],never:[3,2],here:0,interpret:[0,3],search:[],ctx:1,nulla:[],via:3,love:[3,1,2],app:1,deprec:3,unix:3,api:0,fake:1,instal:[0,2,3],from:[3,1,2],would:[1,2],two:1,next:[1,2],everybodi:[3,1,2],few:3,call:1,until:1,more:[1,3],officia:[],notic:3,site:0,setup:[3,1,2],work:[0,1],can:[3,1,2],def:1,novic:0,sudo:3,indic:[],unavail:[1,2],want:[1,3],alwai:[1,2],sit:[],answer:3,updat:3,incididunt:[],dive:[0,1],after:[0,1],befor:[0,1,2],ullamco:[],github:3,practic:0,bind:1,stdin:1,issu:1,inform:0,maintain:3,allow:[3,1,2],help:3,becaus:[3,1,2],elit:[],eiusmod:[],still:[1,2],process_respons:1,paramet:[1,2],labor:[],main:1,might:1,them:[1,3],anim:[],"return":[1,2],python:[0,1,2,3],handi:1,initi:[1,3],framework:[3,2],now:[3,2],ipsum:[],changelog:0,occaecat:[],drop:[1,3],unlik:3,pariatur:[],easili:1,mode:3,attributeerror:1,mean:1,compil:3,replac:3,idea:[1,3],miss:3,content:[],aliquip:[],situat:1,pleasant:1,reason:[3,1,2],qui:[],org:[0,2,3],"byte":1,basi:0,could:[1,3],traceback:1,put:[1,3],preprocess_request:1,keep:1,thing:[1,3],mxcl:3,oper:1,number:3,yourself:[1,3],instruct:0,done:1,enim:[],fantast:3,interact:[3,1,2],system:3,statement:1,store:[1,2],shell:[0,1,2,3],consol:1,option:[3,2],tool:[0,2],setuptool:3,part:0,mostli:0,provid:0,remov:3,browser:1,sai:[1,2],arg:1,mind:1,ani:[3,1,2],raw:3,volupt:[],tabl:[1,3],need:1,imagin:[1,2],reprehenderit:[],built:3,snow:3,note:0,also:[1,3],which:[0,1],begin:0,distribut:[0,2,3],normal:[1,2],object:[1,2],veniam:[],most:1,plai:[3,1,2],"class":0,don:[1,2],url:[1,2],request:[0,1,2],doe:[3,1,2],dolor:[],brew:3,cillum:[],session:[1,3],proident:[],redirect:[1,2],access:[1,2],onli:1,just:[3,1,2],configur:0,activ:3,should:[1,2],consectetur:[],local:[0,1],get:[3,1,2],familiar:3,pypi:3,shelltool:[1,3],requir:[3,1,2],nisi:[],stuff:[1,3],common:[1,3],where:1,labori:[],see:[1,2],sed:[],result:[3,1,2],respons:1,best:0,test_request_context:1,expert:0,hopefulli:[3,2],databas:[3,1,2],nostrud:[],response_class:1,"import":[3,1,2],experi:[0,1,3],attribut:1,altern:1,kei:3,osxpython:0,upfront:[3,1,2],solv:[],homebrew:3,come:[3,1,2],amet:[],addit:0,both:0,last:1,howev:1,tempor:[],etc:[3,1,2],context:[0,1,2],improv:[0,1,3],com:[1,3],simpli:3,point:1,unittest:1,magna:[],pop:1,ipython:[0,2],shutdown:1,linux:3,guid:0,dui:[],compon:3,trigger:1,interest:0,basic:[3,1,2],immedi:[3,1,2],gist:3,fire:[0,1],rubi:3,utliti:[],those:3,"case":1,look:0,packag:[0,3],plain:[1,2],trick:1,defin:[1,3],"while":3,have:[3,1,2],helper:[1,3],culpa:[],non:[],itself:[3,1,2],prose:0,aliqua:[],develop:0,welcom:0,fugiat:[],minim:[],make:1,document:0,commodo:[],http:[3,1,2],redirect_url:1,leopard:3,user:[0,1,2,3],mani:3,adipis:[],recent:1,well:3,exampl:1,command:[3,1,2],thi:[0,1,2,3],irur:[],execut:[3,1,2],laborum:[],web:0,onward:1,except:[1,2],exercit:[],modul:[1,3],real:[3,1,2],applic:[3,1,2],around:[3,1,2],background:0,bit:1,like:[1,3],specif:[0,1,2,3],either:1,velit:[],page:[1,2],some:[0,1,3],back:[3,1,2],virtualenv:3,est:[],ess:[],avoid:3,legal:0,larg:3,refer:0,run:[3,1,2],usag:[0,1],step:0,referr:[1,2],about:0,unfortun:1,excepteur:[],easy_instal:[3,2],your:[3,1,2],manag:[0,3],wai:1,avail:1,start:[3,1,2],handbook:0,lot:1,"function":[0,1,2],properli:[0,3],amongst:3,uninstal:3,line:1,deserunt:[],bug:3,url_for:1,tell:[3,2],creat:[3,1,2],exist:0,file:1,pip:[0,2,3],curl:[3,2],probabl:[1,2],again:[3,2],titl:[],other:[1,3],futur:3,test:1,you:[0,1,2,3],star:[1,3],consid:3,lorem:[],outdat:3,fss:3,ignor:1,time:[3,1,2],push:1,daili:0},objtypes:{},titles:["Welcome to OSXPython.org","Working with the Shell","Useful Tools","Properly Installing Python"],objnames:{},filenames:["index","shell","useful-tools","installation"]})
+1 -1
View File
@@ -11,7 +11,7 @@ instructions for web development with Flask.
foreword
installation
quickstart
useful-tools
tutorial/index
templating
testing
+1 -1
View File
@@ -6,6 +6,6 @@
Welcome to OSXPython.org
=====================================
Welcome to OSXPython.org. This site exists to provide novice and expert Python developers using OS X a best-practice handbook to the installation, configuration, and usage of Python on a daily basis.
Welcome to OSXPython.org. This site exists to provide both novice and expert Python developers using OS X a best-practice handbook to the installation, configuration, and usage of Python on a daily basis.
.. include:: contents.rst.inc
+74 -83
View File
@@ -1,107 +1,98 @@
Working with the Shell
======================
Properly Installing Python
==========================
.. versionadded:: 0.3
*Or, "Installing Python 2.7 via Homebrew".*
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. ::
just import your application and start playing around.
Package Manager
---------------
While Snow Leopard comes with a large number of UNIX utilities, those
familiar with Linux systems will notice one key component missing: a
package manager. Mxcl's *Homebrew* is the answer.
To install Homebrew, simply run: ::
$ ruby -e "$(curl -fsS http://gist.github.com/raw/323731/install_homebrew.rb)"
And we can now install python: ::
It's basic commands are **update**, **install**, and **remove**.
.. man brew
Python Interpreter
------------------
And we can now install Python 2.7: ::
$ sudo brew install python --framework
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 :data:`~flask.g`, :data:`~flask.request` and others are not
available. But the code you want to test might depend on them, so what
can you do?
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.
Diving into Context Locals
--------------------------
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 ``next``
parameter or the HTTP referrer or the index page::
from flask import request, url_for
def redirect_url():
return request.args.get('next') or \
request.referrer or \
url_for('index')
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:
>>> redirect_url()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'request'
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 :attr:`~flask.Flask.test_request_context` method can create
us a request context:
>>> ctx = app.test_request_context('/?next=http://example.com/')
This context can be used in two ways. Either with the `with` statement
(which unfortunately is not very handy for shell sessions). The
alternative way is to call the `push` and `pop` methods:
>>> ctx.push()
From that point onwards you can work with the request object:
>>> redirect_url()
u'http://example.com/'
Until you call `pop`:
>>> ctx.pop()
>>> redirect_url()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'request'
The ``--framework`` option tells Homebrew to compile Python in Framework
mode. The outdated version of Python that Snow Leopard comes packaged with
is built as a Framework, so this helps avoid some future module installation
bugs.
Firing Before/After Request
---------------------------
Distribute & Pip
----------------
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
:data:`~flask.g` object etc.
*Distribute* is a fantastic drop-in replacement for *easy_install* and
*setuptools*. It allows you to install and manage python packages from
pypi.python.org, amongst a few other sources. It also plays well with
*virtualenv* and user-enviornments.
This however can easily be done yourself. Just call
:meth:`~flask.Flask.preprocess_request`:
To install *Distribute* and it's *easy_install* run the following commands: ::
>>> ctx = app.test_request_context()
>>> ctx.push()
>>> app.preprocess_request()
$ curl -O http://python-distribute.org/distribute_setup.py
$ python distribute_setup.py
Keep in mind that the :meth:`~flask.Flask.preprocess_request` function
might return a response object, in that case just ignore it.
**easy_install** is considered by many to be a deprecated system, so we will
install it's replacement: **pip**. Pip allows for uninstallation of packages,
and is actively maintained, unlike setuptool's easy_install.
To shutdown a request, you need to trick a bit before the after request
functions (triggered by :meth:`~flask.Flask.process_response`) operate on
a response object:
To install ``pip``: ::
>>> app.process_response(app.response_class())
<Response 0 bytes [200 OK]>
>>> ctx.pop()
$ easy_install pip
Hopefully you'll never have to use **easy_install** again.
.. Firing Before/After Request
.. ---------------------------
..
.. 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
.. :data:`~flask.g` object etc.
..
.. This however can easily be done yourself. Just call
.. :meth:`~flask.Flask.preprocess_request`:
..
.. >>> ctx = app.test_request_context()
.. >>> ctx.push()
.. >>> app.preprocess_request()
..
.. Keep in mind that the :meth:`~flask.Flask.preprocess_request` function
.. might return a response object, in that case just ignore it.
..
.. To shutdown a request, you need to trick a bit before the after request
.. functions (triggered by :meth:`~flask.Flask.process_response`) operate on
.. a response object:
..
.. >>> app.process_response(app.response_class())
.. <Response 0 bytes [200 OK]>
.. >>> ctx.pop()
Further Improving the Shell Experience
+48
View File
@@ -0,0 +1,48 @@
Useful Tools
=================
*For Python 2.7.*
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. ::
$ pip install bpython
And we can now install Python 2.7: ::
$
The **--framework** option tells Python
IPython
-------------------------------
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 ``next``
parameter or the HTTP referrer or the index page::
$ curl -O http://python-distribute.org/distribute_setup.py
$ python distribute_setup.py
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: ::
$ easy_install pip
Hopefully you'll never have to use **easy_install** again.
BPython
-------
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
:data:`~flask.g` object etc.