Skip to content
Myr

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

5 files+202 added−1 removed

added:docs/patterns/distribute.rst+160 added
1Deploying with Distribute2=========================34`distribute`_, formerly setuptools, is an extension library that is5commonly used to (like the name says) distribute Python libraries and6extensions. It extends distutils, a basic module installation system7shipped with Python to also support various more complex constructs that8make larger applications easier to distribute:910- **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.2324Flask itself, and all the libraries you can find on the cheeseshop will25are distributed with either distribute, the older setuptools or distutils.2627In 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 is30not supported by `distribute`_ so we will not bother with it. If you have31not yet converted your application into a package, head over to the32:ref:`larger-applications` pattern to see how this can be done.3334Basic Setup Script35------------------3637Because you have Flask running, you either have setuptools or distribute38available on your system anyways. If you do not, fear not, there is a39script to install it for you: `distribute_setup.py`_. Just download and40run with your Python interpreter.4142Standard disclaimer applies: :ref:`you better use a virtualenv43<virtualenv>`.4445Your setup code always goes into a file named `setup.py` next to your46application. The name of the file is only convention, but because47everybody will look for a file with that name, you better not change it.4849Yes, even if you are using `distribute`, you are importing from a package50called `setuptools`. `distribute` is fully backwards compatible with51`setuptools`, so it also uses the same import name.5253A basic `setup.py` file for a Flask application looks like this::5455 from setuptools import setup5657 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 )6667Please keep in mind that you have to list subpackages explicitly. If you68want distribute to lookup the packages for you automatically, you can use69the `find_packages` function::7071 from setuptools import setup, find_packages7273 setup(74 ...75 packages=find_packages()76 )7778Most 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` file81and install all the entries that match as package data. We will use this82to distribute the static files and templates along with the Python module83(see :ref:`distributing-resources`). The `zip_safe` flag can be used to84force or prevent zip Archive creation. In general you probably don't want85your packages to be installed as zip files because some tools do not86support them and they make debugging a lot harder.878889.. _distributing-resources:9091Distributing Resources92----------------------9394If you try to install the package you just created, you will notice that95folders like `static` or `templates` are not installed for you. The96reason for this is that distribute does not know which files to add for97you. 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 to99your tarball::100101 recursive-include yourapplication/templates102 recursive-include yourapplication/static103104Don't forget that even if you enlist them in your `MANIFEST.in` file, they105won't be installed for you unless you set the `include_package_data`106parameter of the `setup` function to `True`!107108109Declaring Dependencies110----------------------111112Dependencies are declared in the `install_requires` parameter as list.113Each item in that list is the name of a package that should be pulled from114PyPI on installation. By default it will always use the most recent115version, but you can also provide minimum and maximum version116requirements. Here some examples::117118 install_requires=[119 'Flask>=0.2',120 'SQLAlchemy>=0.6',121 'BrokenPackage>=0.7,<=1.0'122 ]123124I mentioned earlier that dependencies are pulled from PyPI. What if you125want to depend on a package that cannot be found on PyPI and won't be126because it is an internal package you don't want to share with anyone?127Just still do as if there was a PyPI entry for it and provide a list of128alternative locations where distribute should look for tarballs::129130 dependency_links=['http://example.com/yourfiles']131132Make sure that page has a directory listing and the links on the page are133pointing to the actual tarballs with their correct filenames as this is134how distribute will find the files. If you have an internal company135server that contains the packages, provide the URL to that server there.136137138Installing / Developing139-----------------------140141To install your application (ideally into a virtualenv) just run the142`setup.py` script with the `install` parameter. It will install your143application into the virtualenv's site-packages folder and also download144and install all dependencies::145146 $ python setup.py install147148If you are developing on the package and also want the requirements to be149installed, you can use the `develop` command instead::150151 $ python setup.py develop152153This has the advantage of just installing a link to the site-packages154folder instead of copying the data over. You can then continue to work on155the code without having to run `install` again after each change.156157158.. _distribute: http://pypi.python.org/pypi/distribute159.. _pip: http://pypi.python.org/pypi/pip160.. _distribute_setup.py: http://python-distribute.org/distribute_setup.py
modified:docs/patterns/flashing.rst+38 added
9 unmodified lines
10possible to record a message at the end of a request and access it next11request and only next request. This is usually combined with a layout12template that does this.1314Simple Flashing15---------------1617So here a full example::1819 from flask import flash, redirect, url_for, render_template20
60 unmodified lines
81 </dl>82 <p><input type=submit value=Login>83 </form>84 {% endblock %}8586Flashing With Categories87------------------------8889.. versionadded:: 0.59091It is also possible to provide categories when flashing a message. The92default category if nothing is provided is ``'message'``. Alternative93categories can be used to give the user better feedback. For example94error messages could be displayed with a red background.9596To flash a message with a different category, just use the second argument97to the :func:`~flask.flash` function::9899 flash(u'Invalid password provided', 'error')100101Inside the template you then have to tell the102:func:`~flask.get_flashed_messages` function to also return the103categories. The loop looks slighty different in that situation then:104105.. sourcecode:: html+jinja106107 {% 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 %}116117This is just one example of how to render these flashed messages. One118might 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 lines
16.. toctree::17 :maxdepth: 21819 packages20 distribute21 sqlite322 sqlalchemy23 fileuploads24 caching
5 unmodified lines
modified:docs/installation.rst+2 added
17 unmodified lines
1819So how do you get all that on your computer in no time? The most kick-ass20method is virtualenv, so let's look at that first.2122.. _virtualenv:2324virtualenv25----------2627Virtualenv is what you want to use during development and in production if
148 unmodified lines
modified:flask.py+1 added-1 removed
228 unmodified lines
229 :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))236237
925 unmodified lines