Fixed a doc bug and added distribtue docs.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 35ed617 · Browse files · 5 files·+202 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+202 added−1 removed
- other:+202 added−1 removed
Provenance
- Commit
931dcc4- Tree
c0bdb48- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
35ed617
5 files changed+202 added−1 removed
added:docs/patterns/distribute.rst+160 added
1+Deploying with Distribute2+=========================3+4+`distribute`_, formerly setuptools, is an extension library that is5+commonly used to (like the name says) distribute Python libraries and6+extensions. It extends distutils, a basic module installation system7+shipped with Python to also support various more complex constructs that8+make larger applications easier to distribute:9+10+- **support for dependencies**: a library or application can declare a11+ list of other libraries it depends on which will be installed12+ automatically for you.13+- **package registry**: setuptools registers your package with your14+ Python installation. This makes it possible to query information15+ provided by one package from another package. The best known feature of16+ this system is the entry point support which allows one package to17+ declare an "entry point" another package can hook into to extend the18+ other package.19+- **installation manager**: `easy_install`, which comes with distribute20+ can install other libraries for you. You can also use `pip`_ which21+ sooner or later will replace `easy_install` which does more than just22+ installing packages for you.23+24+Flask itself, and all the libraries you can find on the cheeseshop will25+are distributed with either distribute, the older setuptools or distutils.26+27+In this case we assume your application is called28+`yourapplication.py` and you are not using a module, but a `package29+<larger-applications>`_. Distributing resources with standard modules is30+not supported by `distribute`_ so we will not bother with it. If you have31+not yet converted your application into a package, head over to the32+:ref:`larger-applications` pattern to see how this can be done.33+34+Basic Setup Script35+------------------36+37+Because you have Flask running, you either have setuptools or distribute38+available on your system anyways. If you do not, fear not, there is a39+script to install it for you: `distribute_setup.py`_. Just download and40+run with your Python interpreter.41+42+Standard disclaimer applies: :ref:`you better use a virtualenv43+<virtualenv>`.44+45+Your setup code always goes into a file named `setup.py` next to your46+application. The name of the file is only convention, but because47+everybody will look for a file with that name, you better not change it.48+49+Yes, even if you are using `distribute`, you are importing from a package50+called `setuptools`. `distribute` is fully backwards compatible with51+`setuptools`, so it also uses the same import name.52+53+A basic `setup.py` file for a Flask application looks like this::54+55+ from setuptools import setup56+57+ setup(58+ name='Your Application',59+ version='1.0',60+ long_description=__doc__,61+ packages=['yourapplication'],62+ include_package_data=True,63+ zip_safe=False,64+ install_requires=['Flask']65+ )66+67+Please keep in mind that you have to list subpackages explicitly. If you68+want distribute to lookup the packages for you automatically, you can use69+the `find_packages` function::70+71+ from setuptools import setup, find_packages72+73+ setup(74+ ...75+ packages=find_packages()76+ )77+78+Most parameters to the `setup` function should be self explanatory,79+`include_package_data` and `zip_safe` might not be.80+`include_package_data` tells distribute to look for a `MANIFEST.in` file81+and install all the entries that match as package data. We will use this82+to distribute the static files and templates along with the Python module83+(see :ref:`distributing-resources`). The `zip_safe` flag can be used to84+force or prevent zip Archive creation. In general you probably don't want85+your packages to be installed as zip files because some tools do not86+support them and they make debugging a lot harder.87+88+89+.. _distributing-resources:90+91+Distributing Resources92+----------------------93+94+If you try to install the package you just created, you will notice that95+folders like `static` or `templates` are not installed for you. The96+reason for this is that distribute does not know which files to add for97+you. What you should do, is to create a `MANIFEST.in` file next to your98+`setup.py` file. This file lists all the files that should be added to99+your tarball::100+101+ recursive-include yourapplication/templates102+ recursive-include yourapplication/static103+104+Don't forget that even if you enlist them in your `MANIFEST.in` file, they105+won't be installed for you unless you set the `include_package_data`106+parameter of the `setup` function to `True`!107+108+109+Declaring Dependencies110+----------------------111+112+Dependencies are declared in the `install_requires` parameter as list.113+Each item in that list is the name of a package that should be pulled from114+PyPI on installation. By default it will always use the most recent115+version, but you can also provide minimum and maximum version116+requirements. Here some examples::117+118+ install_requires=[119+ 'Flask>=0.2',120+ 'SQLAlchemy>=0.6',121+ 'BrokenPackage>=0.7,<=1.0'122+ ]123+124+I mentioned earlier that dependencies are pulled from PyPI. What if you125+want to depend on a package that cannot be found on PyPI and won't be126+because it is an internal package you don't want to share with anyone?127+Just still do as if there was a PyPI entry for it and provide a list of128+alternative locations where distribute should look for tarballs::129+130+ dependency_links=['http://example.com/yourfiles']131+132+Make sure that page has a directory listing and the links on the page are133+pointing to the actual tarballs with their correct filenames as this is134+how distribute will find the files. If you have an internal company135+server that contains the packages, provide the URL to that server there.136+137+138+Installing / Developing139+-----------------------140+141+To install your application (ideally into a virtualenv) just run the142+`setup.py` script with the `install` parameter. It will install your143+application into the virtualenv's site-packages folder and also download144+and install all dependencies::145+146+ $ python setup.py install147+148+If you are developing on the package and also want the requirements to be149+installed, you can use the `develop` command instead::150+151+ $ python setup.py develop152+153+This has the advantage of just installing a link to the site-packages154+folder instead of copying the data over. You can then continue to work on155+the code without having to run `install` again after each change.156+157+158+.. _distribute: http://pypi.python.org/pypi/distribute159+.. _pip: http://pypi.python.org/pypi/pip160+.. _distribute_setup.py: http://python-distribute.org/distribute_setup.pymodified:docs/patterns/flashing.rst+38 added
9 unmodified lines10 possible to record a message at the end of a request and access it next11 request and only next request. This is usually combined with a layout12 template that does this.13 14+Simple Flashing15+---------------16+17 So here a full example::18 19 from flask import flash, redirect, url_for, render_template20 60 unmodified lines81 </dl>82 <p><input type=submit value=Login>83 </form>84 {% endblock %}85+86+Flashing With Categories87+------------------------88+89+.. versionadded:: 0.590+91+It is also possible to provide categories when flashing a message. The92+default category if nothing is provided is ``'message'``. Alternative93+categories can be used to give the user better feedback. For example94+error messages could be displayed with a red background.95+96+To flash a message with a different category, just use the second argument97+to the :func:`~flask.flash` function::98+99+ flash(u'Invalid password provided', 'error')100+101+Inside the template you then have to tell the102+:func:`~flask.get_flashed_messages` function to also return the103+categories. The loop looks slighty different in that situation then:104+105+.. sourcecode:: html+jinja106+107+ {% with messages = get_flashed_messages(with_categories=true) %}108+ {% if messages %}109+ <ul class=flashes>110+ {% for category, message in messages %}111+ <li class="{{ category }}">{{ message }}</li>112+ {% endfor %}113+ </ul>114+ {% endif %}115+ {% endwith %}116+117+This is just one example of how to render these flashed messages. One118+might also use the category to add a prefix such as119+``<strong>Error:</strong>`` to the message.modified:docs/patterns/index.rst+1 added
15 unmodified lines16 .. toctree::17 :maxdepth: 218 19 packages20+ distribute21 sqlite322 sqlalchemy23 fileuploads24 caching5 unmodified linesmodified:docs/installation.rst+2 added
17 unmodified lines18 19 So how do you get all that on your computer in no time? The most kick-ass20 method is virtualenv, so let's look at that first.21 22+.. _virtualenv:23+24 virtualenv25 ----------26 27 Virtualenv is what you want to use during development and in production if148 unmodified linesmodified:flask.py+1 added-1 removed
228 unmodified lines229 :param category: the category for the message. The following values230 are recommended: ``'message'`` for any kind of message,231 ``'error'`` for errors, ``'info'`` for information232 messages and ``'warning'`` for warnings. However any233- kind of string and be used as category.233+ kind of string can be used as category.234 """235 session.setdefault('_flashes', []).append((category, message))236 237 925 unmodified lines