Browse Source

docs: :file:`app.py`, :file:`yourapp/templates`

pull/1240/head
defuz 10 years ago
parent
commit
a8f570cc62
  1. 8
      docs/blueprints.rst
  2. 6
      docs/cli.rst
  3. 2
      docs/config.rst
  4. 2
      docs/deploying/mod_wsgi.rst
  5. 10
      docs/extensiondev.rst
  6. 4
      docs/extensions.rst
  7. 2
      docs/foreword.rst
  8. 2
      docs/installation.rst
  9. 10
      docs/patterns/appfactories.rst
  10. 20
      docs/patterns/distribute.rst
  11. 6
      docs/patterns/fabric.rst
  12. 2
      docs/patterns/flashing.rst
  13. 4
      docs/patterns/jquery.rst
  14. 2
      docs/patterns/lazyloading.rst
  15. 4
      docs/patterns/mongokit.rst
  16. 16
      docs/patterns/packages.rst
  17. 8
      docs/patterns/sqlalchemy.rst
  18. 2
      docs/patterns/templateinheritance.rst
  19. 6
      docs/patterns/wtforms.rst
  20. 10
      docs/quickstart.rst
  21. 2
      docs/testing.rst
  22. 2
      docs/tutorial/css.rst
  23. 2
      docs/tutorial/dbinit.rst
  24. 4
      docs/tutorial/folders.rst
  25. 16
      docs/tutorial/setup.rst
  26. 4
      docs/tutorial/templates.rst
  27. 2
      docs/tutorial/views.rst
  28. 12
      docs/upgrading.rst
  29. 4
      examples/flaskr/README
  30. 4
      examples/minitwit/README
  31. 6
      flask/app.py
  32. 2
      flask/helpers.py

8
docs/blueprints.rst

@ -159,7 +159,7 @@ blueprint::
admin = Blueprint('admin', __name__, static_folder='static') admin = Blueprint('admin', __name__, static_folder='static')
By default the rightmost part of the path is where it is exposed on the By default the rightmost part of the path is where it is exposed on the
web. Because the folder is called ``static`` here it will be available at web. Because the folder is called :file:`static` here it will be available at
the location of the blueprint + ``/static``. Say the blueprint is the location of the blueprint + ``/static``. Say the blueprint is
registered for ``/admin`` the static folder will be at ``/admin/static``. registered for ``/admin`` the static folder will be at ``/admin/static``.
@ -185,10 +185,10 @@ provides in the actual application.
So if you have a blueprint in the folder ``yourapplication/admin`` and you So if you have a blueprint in the folder ``yourapplication/admin`` and you
want to render the template ``'admin/index.html'`` and you have provided want to render the template ``'admin/index.html'`` and you have provided
``templates`` as a `template_folder` you will have to create a file like ``templates`` as a `template_folder` you will have to create a file like
this: ``yourapplication/admin/templates/admin/index.html``. this: :file:`yourapplication/admin/templates/admin/index.html`.
To further reiterate this: if you have a blueprint named ``admin`` and you To further reiterate this: if you have a blueprint named ``admin`` and you
want to render a template called ``index.html`` which is specific to this want to render a template called :file:`index.html` which is specific to this
blueprint, the best idea is to lay out your templates like this:: blueprint, the best idea is to lay out your templates like this::
yourpackage/ yourpackage/
@ -199,7 +199,7 @@ blueprint, the best idea is to lay out your templates like this::
index.html index.html
__init__.py __init__.py
And then when you want to render the template, use ``admin/index.html`` as And then when you want to render the template, use :file:`admin/index.html` as
the name to look up the template by. If you encounter problems loading the name to look up the template by. If you encounter problems loading
the correct templates enable the ``EXPLAIN_TEMPLATE_LOADING`` config the correct templates enable the ``EXPLAIN_TEMPLATE_LOADING`` config
variable which will instruct Flask to print out the steps it goes through variable which will instruct Flask to print out the steps it goes through

6
docs/cli.rst

@ -35,7 +35,7 @@ automatically and discover the module name but that might not always work.
In that imported file the name of the app needs to be called ``app`` or In that imported file the name of the app needs to be called ``app`` or
optionally be specified after a colon. optionally be specified after a colon.
Given a ``hello.py`` file with the application in it named ``app`` this is Given a :file:`hello.py` file with the application in it named ``app`` this is
how it can be run. how it can be run.
Environment variables (On Windows use ``set`` instead of ``export``):: Environment variables (On Windows use ``set`` instead of ``export``)::
@ -133,7 +133,7 @@ For instance if you have a factory function that creates an application
from a filename you could make a separate file that creates such an from a filename you could make a separate file that creates such an
application from an environment variable. application from an environment variable.
This could be a file named ``autoapp.py`` with these contents:: This could be a file named :file:`autoapp.py` with these contents::
import os import os
from yourapplication import create_app from yourapplication import create_app
@ -179,7 +179,7 @@ We won't go into detail now about the differences but if you are curious
you can have a look at the :ref:`script-info-object` section to learn all you can have a look at the :ref:`script-info-object` section to learn all
about it. about it.
To explain all of this, here is an example ``manage.py`` script that To explain all of this, here is an example :file:`manage.py` script that
manages a hypothetical wiki application. We will go through the details manages a hypothetical wiki application. We will go through the details
afterwards:: afterwards::

2
docs/config.rst

@ -323,7 +323,7 @@ in the example above::
app.config.from_object('yourapplication.default_settings') app.config.from_object('yourapplication.default_settings')
app.config.from_envvar('YOURAPPLICATION_SETTINGS') app.config.from_envvar('YOURAPPLICATION_SETTINGS')
Then you just have to add a separate `config.py` file and export Then you just have to add a separate :file:`config.py` file and export
``YOURAPPLICATION_SETTINGS=/path/to/config.py`` and you are done. However ``YOURAPPLICATION_SETTINGS=/path/to/config.py`` and you are done. However
there are alternative ways as well. For example you could use imports or there are alternative ways as well. For example you could use imports or
subclassing. subclassing.

2
docs/deploying/mod_wsgi.rst

@ -132,7 +132,7 @@ If your application does not run, follow this guide to troubleshoot:
You have a ``app.run()`` call in your application file that is not You have a ``app.run()`` call in your application file that is not
guarded by an ``if __name__ == '__main__':`` condition. Either guarded by an ``if __name__ == '__main__':`` condition. Either
remove that :meth:`~flask.Flask.run` call from the file and move it remove that :meth:`~flask.Flask.run` call from the file and move it
into a separate `run.py` file or put it into such an if block. into a separate :file:`run.py` file or put it into such an if block.
**Problem:** application gives permission errors **Problem:** application gives permission errors
Probably caused by your application running as the wrong user. Make Probably caused by your application running as the wrong user. Make

10
docs/extensiondev.rst

@ -27,7 +27,7 @@ The name of the actual extension (the human readable name) however would
be something like "Flask-SimpleXML". Make sure to include the name be something like "Flask-SimpleXML". Make sure to include the name
"Flask" somewhere in that name and that you check the capitalization. "Flask" somewhere in that name and that you check the capitalization.
This is how users can then register dependencies to your extension in This is how users can then register dependencies to your extension in
their `setup.py` files. their :file:`setup.py` files.
Flask sets up a redirect package called :data:`flask.ext` where users Flask sets up a redirect package called :data:`flask.ext` where users
should import the extensions from. If you for instance have a package should import the extensions from. If you for instance have a package
@ -42,7 +42,7 @@ a requirement because many people will use patterns like the
unittests and to support multiple configurations. Because of that it is unittests and to support multiple configurations. Because of that it is
crucial that your application supports that kind of behavior. crucial that your application supports that kind of behavior.
Most importantly the extension must be shipped with a `setup.py` file and Most importantly the extension must be shipped with a :file:`setup.py` file and
registered on PyPI. Also the development checkout link should work so registered on PyPI. Also the development checkout link should work so
that people can easily install the development version into their that people can easily install the development version into their
virtualenv without having to download the library by hand. virtualenv without having to download the library by hand.
@ -70,7 +70,7 @@ Here's the contents of the most important files:
setup.py setup.py
```````` ````````
The next file that is absolutely required is the `setup.py` file which is The next file that is absolutely required is the :file:`setup.py` file which is
used to install your Flask extension. The following contents are used to install your Flask extension. The following contents are
something you can work with:: something you can work with::
@ -366,7 +366,7 @@ extension to be approved you have to follow these guidelines:
or ``python setup.py test``. For test suites invoked with ``make or ``python setup.py test``. For test suites invoked with ``make
test`` the extension has to ensure that all dependencies for the test test`` the extension has to ensure that all dependencies for the test
are installed automatically. If tests are invoked with ``python setup.py are installed automatically. If tests are invoked with ``python setup.py
test``, test dependencies can be specified in the `setup.py` file. The test``, test dependencies can be specified in the :file:`setup.py` file. The
test suite also has to be part of the distribution. test suite also has to be part of the distribution.
3. APIs of approved extensions will be checked for the following 3. APIs of approved extensions will be checked for the following
characteristics: characteristics:
@ -380,7 +380,7 @@ extension to be approved you have to follow these guidelines:
5. The naming scheme for official extensions is *Flask-ExtensionName* or 5. The naming scheme for official extensions is *Flask-ExtensionName* or
*ExtensionName-Flask*. *ExtensionName-Flask*.
6. Approved extensions must define all their dependencies in the 6. Approved extensions must define all their dependencies in the
`setup.py` file unless a dependency cannot be met because it is not :file:`setup.py` file unless a dependency cannot be met because it is not
available on PyPI. available on PyPI.
7. The extension must have documentation that uses one of the two Flask 7. The extension must have documentation that uses one of the two Flask
themes for Sphinx documentation. themes for Sphinx documentation.

4
docs/extensions.rst

@ -9,7 +9,7 @@ Finding Extensions
Flask extensions are listed on the `Flask Extension Registry`_ and can be Flask extensions are listed on the `Flask Extension Registry`_ and can be
downloaded with ``easy_install`` or ``pip``. If you add a Flask extension downloaded with ``easy_install`` or ``pip``. If you add a Flask extension
as dependency to your ``requirements.rst`` or ``setup.py`` file they are as dependency to your ``requirements.rst`` or :file:`setup.py` file they are
usually installed with a simple command or when your application installs. usually installed with a simple command or when your application installs.
Using Extensions Using Extensions
@ -32,7 +32,7 @@ depending on how the extension is distributed. If you want to develop an
application that supports Flask 0.7 or earlier you should still import application that supports Flask 0.7 or earlier you should still import
from the :data:`flask.ext` package. We provide you with a compatibility from the :data:`flask.ext` package. We provide you with a compatibility
module that provides this package for older versions of Flask. You can module that provides this package for older versions of Flask. You can
download it from github: `flaskext_compat.py`_ download it from github: :file:`flaskext_compat.py`_
And here is how you can use it:: And here is how you can use it::

2
docs/foreword.rst

@ -30,7 +30,7 @@ Configuration and Conventions
Flask has many configuration values, with sensible defaults, and a few Flask has many configuration values, with sensible defaults, and a few
conventions when getting started. By convention templates and static files are conventions when getting started. By convention templates and static files are
stored in subdirectories within the application's Python source tree, with the stored in subdirectories within the application's Python source tree, with the
names `templates` and `static` respectively. While this can be changed you names :file:`templates` and :file:`static` respectively. While this can be changed you
usually don't have to, especially when getting started. usually don't have to, especially when getting started.
Growing with Flask Growing with Flask

2
docs/installation.rst

@ -150,7 +150,7 @@ If you don't currently have either, then `get-pip.py` will install both for you
To install the latest setuptools, you can use its bootstrap file: To install the latest setuptools, you can use its bootstrap file:
`ez_setup.py`_ :file:`ez_setup.py`_
Either should be double-clickable once you download them. If you already have pip, Either should be double-clickable once you download them. If you already have pip,
you can upgrade them by running:: you can upgrade them by running::

10
docs/patterns/appfactories.rst

@ -60,9 +60,9 @@ Factories & Extensions
It's preferable to create your extensions and app factories so that the It's preferable to create your extensions and app factories so that the
extension object does not initially get bound to the application. extension object does not initially get bound to the application.
Using `Flask-SQLAlchemy <http://pythonhosted.org/Flask-SQLAlchemy/>`_, Using `Flask-SQLAlchemy <http://pythonhosted.org/Flask-SQLAlchemy/>`_,
as an example, you should not do something along those lines:: as an example, you should not do something along those lines::
def create_app(config_filename): def create_app(config_filename):
app = Flask(__name__) app = Flask(__name__)
app.config.from_pyfile(config_filename) app.config.from_pyfile(config_filename)
@ -72,7 +72,7 @@ as an example, you should not do something along those lines::
But, rather, in model.py (or equivalent):: But, rather, in model.py (or equivalent)::
db = SQLAlchemy() db = SQLAlchemy()
and in your application.py (or equivalent):: and in your application.py (or equivalent)::
def create_app(config_filename): def create_app(config_filename):
@ -83,7 +83,7 @@ and in your application.py (or equivalent)::
db.init_app(app) db.init_app(app)
Using this design pattern, no application-specific state is stored on the Using this design pattern, no application-specific state is stored on the
extension object, so one extension object can be used for multiple apps. extension object, so one extension object can be used for multiple apps.
For more information about the design of extensions refer to :doc:`/extensiondev`. For more information about the design of extensions refer to :doc:`/extensiondev`.
Using Applications Using Applications
@ -91,7 +91,7 @@ Using Applications
So to use such an application you then have to create the application So to use such an application you then have to create the application
first in a separate file otherwise the ``flask`` command won't be able first in a separate file otherwise the ``flask`` command won't be able
to find it. Here an example `exampleapp.py` file that creates such to find it. Here an example :file:`exampleapp.py` file that creates such
an application:: an application::
from yourapplication import create_app from yourapplication import create_app

20
docs/patterns/distribute.rst

@ -27,7 +27,7 @@ Flask itself, and all the libraries you can find on the cheeseshop
are distributed with either distribute, the older setuptools or distutils. are distributed with either distribute, the older setuptools or distutils.
In this case we assume your application is called In this case we assume your application is called
`yourapplication.py` and you are not using a module, but a :ref:`package :file:`yourapplication.py` and you are not using a module, but a :ref:`package
<larger-applications>`. Distributing resources with standard modules is <larger-applications>`. Distributing resources with standard modules is
not supported by `distribute`_ so we will not bother with it. If you have not supported by `distribute`_ so we will not bother with it. If you have
not yet converted your application into a package, head over to the not yet converted your application into a package, head over to the
@ -42,13 +42,13 @@ Basic Setup Script
Because you have Flask running, you either have setuptools or distribute Because you have Flask running, you either have setuptools or distribute
available on your system anyways. If you do not, fear not, there is a available on your system anyways. If you do not, fear not, there is a
script to install it for you: `distribute_setup.py`_. Just download and script to install it for you: :file:`distribute_setup.py`_. Just download and
run with your Python interpreter. run with your Python interpreter.
Standard disclaimer applies: :ref:`you better use a virtualenv Standard disclaimer applies: :ref:`you better use a virtualenv
<virtualenv>`. <virtualenv>`.
Your setup code always goes into a file named `setup.py` next to your Your setup code always goes into a file named :file:`setup.py` next to your
application. The name of the file is only convention, but because application. The name of the file is only convention, but because
everybody will look for a file with that name, you better not change it. everybody will look for a file with that name, you better not change it.
@ -56,7 +56,7 @@ Yes, even if you are using `distribute`, you are importing from a package
called `setuptools`. `distribute` is fully backwards compatible with called `setuptools`. `distribute` is fully backwards compatible with
`setuptools`, so it also uses the same import name. `setuptools`, so it also uses the same import name.
A basic `setup.py` file for a Flask application looks like this:: A basic :file:`setup.py` file for a Flask application looks like this::
from setuptools import setup from setuptools import setup
@ -83,7 +83,7 @@ the `find_packages` function::
Most parameters to the `setup` function should be self explanatory, Most parameters to the `setup` function should be self explanatory,
`include_package_data` and `zip_safe` might not be. `include_package_data` and `zip_safe` might not be.
`include_package_data` tells distribute to look for a `MANIFEST.in` file `include_package_data` tells distribute to look for a :file:`MANIFEST.in` file
and install all the entries that match as package data. We will use this and install all the entries that match as package data. We will use this
to distribute the static files and templates along with the Python module to distribute the static files and templates along with the Python module
(see :ref:`distributing-resources`). The `zip_safe` flag can be used to (see :ref:`distributing-resources`). The `zip_safe` flag can be used to
@ -98,16 +98,16 @@ Distributing Resources
---------------------- ----------------------
If you try to install the package you just created, you will notice that If you try to install the package you just created, you will notice that
folders like `static` or `templates` are not installed for you. The folders like :file:`static` or :file:`templates` are not installed for you. The
reason for this is that distribute does not know which files to add for reason for this is that distribute does not know which files to add for
you. What you should do, is to create a `MANIFEST.in` file next to your you. What you should do, is to create a :file:`MANIFEST.in` file next to your
`setup.py` file. This file lists all the files that should be added to :file:`setup.py` file. This file lists all the files that should be added to
your tarball:: your tarball::
recursive-include yourapplication/templates * recursive-include yourapplication/templates *
recursive-include yourapplication/static * recursive-include yourapplication/static *
Don't forget that even if you enlist them in your `MANIFEST.in` file, they Don't forget that even if you enlist them in your :file:`MANIFEST.in` file, they
won't be installed for you unless you set the `include_package_data` won't be installed for you unless you set the `include_package_data`
parameter of the `setup` function to ``True``! parameter of the `setup` function to ``True``!
@ -145,7 +145,7 @@ Installing / Developing
----------------------- -----------------------
To install your application (ideally into a virtualenv) just run the To install your application (ideally into a virtualenv) just run the
`setup.py` script with the `install` parameter. It will install your :file:`setup.py` script with the `install` parameter. It will install your
application into the virtualenv's site-packages folder and also download application into the virtualenv's site-packages folder and also download
and install all dependencies:: and install all dependencies::

6
docs/patterns/fabric.rst

@ -15,7 +15,7 @@ upfront:
- Fabric 1.0 has to be installed locally. This tutorial assumes the - Fabric 1.0 has to be installed locally. This tutorial assumes the
latest version of Fabric. latest version of Fabric.
- The application already has to be a package and requires a working - The application already has to be a package and requires a working
`setup.py` file (:ref:`distribute-deployment`). :file:`setup.py` file (:ref:`distribute-deployment`).
- In the following example we are using `mod_wsgi` for the remote - In the following example we are using `mod_wsgi` for the remote
servers. You can of course use your own favourite server there, but servers. You can of course use your own favourite server there, but
for this example we chose Apache + `mod_wsgi` because it's very easy for this example we chose Apache + `mod_wsgi` because it's very easy
@ -25,7 +25,7 @@ upfront:
Creating the first Fabfile Creating the first Fabfile
-------------------------- --------------------------
A fabfile is what controls what Fabric executes. It is named `fabfile.py` A fabfile is what controls what Fabric executes. It is named :file:`fabfile.py`
and executed by the `fab` command. All the functions defined in that file and executed by the `fab` command. All the functions defined in that file
will show up as `fab` subcommands. They are executed on one or more will show up as `fab` subcommands. They are executed on one or more
hosts. These hosts can be defined either in the fabfile or on the command hosts. These hosts can be defined either in the fabfile or on the command
@ -168,7 +168,7 @@ can pack up the application and deploy it::
Fabric will now connect to all servers and run the commands as written Fabric will now connect to all servers and run the commands as written
down in the fabfile. First it will execute pack so that we have our down in the fabfile. First it will execute pack so that we have our
tarball ready and then it will execute deploy and upload the source code tarball ready and then it will execute deploy and upload the source code
to all servers and install it there. Thanks to the `setup.py` file we to all servers and install it there. Thanks to the :file:`setup.py` file we
will automatically pull in the required libraries into our virtual will automatically pull in the required libraries into our virtual
environment. environment.

2
docs/patterns/flashing.rst

@ -38,7 +38,7 @@ So here is a full example::
return redirect(url_for('index')) return redirect(url_for('index'))
return render_template('login.html', error=error) return render_template('login.html', error=error)
And here the ``layout.html`` template which does the magic: And here the :file:`layout.html` template which does the magic:
.. sourcecode:: html+jinja .. sourcecode:: html+jinja

4
docs/patterns/jquery.rst

@ -119,9 +119,9 @@ special error reporting in that case.
The HTML The HTML
-------- --------
Your index.html template either has to extend a `layout.html` template with Your index.html template either has to extend a :file:`layout.html` template with
jQuery loaded and the `$SCRIPT_ROOT` variable set, or do that on the top. jQuery loaded and the `$SCRIPT_ROOT` variable set, or do that on the top.
Here's the HTML code needed for our little application (`index.html`). Here's the HTML code needed for our little application (:file:`index.html`).
Notice that we also drop the script directly into the HTML here. It is Notice that we also drop the script directly into the HTML here. It is
usually a better idea to have that in a separate script file: usually a better idea to have that in a separate script file:

2
docs/patterns/lazyloading.rst

@ -33,7 +33,7 @@ Imagine the current application looks somewhat like this::
pass pass
Then the centralized approach you would have one file with the views Then the centralized approach you would have one file with the views
(`views.py`) but without any decorator:: (:file:`views.py`) but without any decorator::
def index(): def index():
pass pass

4
docs/patterns/mongokit.rst

@ -20,7 +20,7 @@ Declarative
The default behavior of MongoKit is the declarative one that is based on The default behavior of MongoKit is the declarative one that is based on
common ideas from Django or the SQLAlchemy declarative extension. common ideas from Django or the SQLAlchemy declarative extension.
Here an example `app.py` module for your application:: Here an example :file:`app.py` module for your application::
from flask import Flask from flask import Flask
from mongokit import Connection, Document from mongokit import Connection, Document
@ -47,7 +47,7 @@ MongoDB is schemaless. This means you can modify the data structure from one
insert query to the next without any problem. MongoKit is just schemaless insert query to the next without any problem. MongoKit is just schemaless
too, but implements some validation to ensure data integrity. too, but implements some validation to ensure data integrity.
Here is an example document (put this also into `app.py`, e.g.):: Here is an example document (put this also into :file:`app.py`, e.g.)::
def max_length(length): def max_length(length):
def validate(value): def validate(value):

16
docs/patterns/packages.rst

@ -22,7 +22,7 @@ Simple Packages
To convert that into a larger one, just create a new folder To convert that into a larger one, just create a new folder
`yourapplication` inside the existing one and move everything below it. `yourapplication` inside the existing one and move everything below it.
Then rename `yourapplication.py` to `__init__.py`. (Make sure to delete Then rename :file:`yourapplication.py` to :file:`__init__.py`. (Make sure to delete
all `.pyc` files first, otherwise things would most likely break) all `.pyc` files first, otherwise things would most likely break)
You should then end up with something like that:: You should then end up with something like that::
@ -41,7 +41,7 @@ You should then end up with something like that::
But how do you run your application now? The naive ``python But how do you run your application now? The naive ``python
yourapplication/__init__.py`` will not work. Let's just say that Python yourapplication/__init__.py`` will not work. Let's just say that Python
does not want modules in packages to be the startup file. But that is not does not want modules in packages to be the startup file. But that is not
a big problem, just add a new file called `runserver.py` next to the inner a big problem, just add a new file called :file:`runserver.py` next to the inner
`yourapplication` folder with the following contents:: `yourapplication` folder with the following contents::
from yourapplication import app from yourapplication import app
@ -52,21 +52,21 @@ into multiple modules. The only thing you have to remember is the
following quick checklist: following quick checklist:
1. the `Flask` application object creation has to be in the 1. the `Flask` application object creation has to be in the
`__init__.py` file. That way each module can import it safely and the :file:`__init__.py` file. That way each module can import it safely and the
`__name__` variable will resolve to the correct package. `__name__` variable will resolve to the correct package.
2. all the view functions (the ones with a :meth:`~flask.Flask.route` 2. all the view functions (the ones with a :meth:`~flask.Flask.route`
decorator on top) have to be imported in the `__init__.py` file. decorator on top) have to be imported in the :file:`__init__.py` file.
Not the object itself, but the module it is in. Import the view module Not the object itself, but the module it is in. Import the view module
**after the application object is created**. **after the application object is created**.
Here's an example `__init__.py`:: Here's an example :file:`__init__.py`::
from flask import Flask from flask import Flask
app = Flask(__name__) app = Flask(__name__)
import yourapplication.views import yourapplication.views
And this is what `views.py` would look like:: And this is what :file:`views.py` would look like::
from yourapplication import app from yourapplication import app
@ -93,9 +93,9 @@ You should then end up with something like that::
Every Python programmer hates them, and yet we just added some: Every Python programmer hates them, and yet we just added some:
circular imports (That's when two modules depend on each other. In this circular imports (That's when two modules depend on each other. In this
case `views.py` depends on `__init__.py`). Be advised that this is a case :file:`views.py` depends on :file:`__init__.py`). Be advised that this is a
bad idea in general but here it is actually fine. The reason for this is bad idea in general but here it is actually fine. The reason for this is
that we are not actually using the views in `__init__.py` and just that we are not actually using the views in :file:`__init__.py` and just
ensuring the module is imported and we are doing that at the bottom of ensuring the module is imported and we are doing that at the bottom of
the file. the file.

8
docs/patterns/sqlalchemy.rst

@ -33,7 +33,7 @@ SQLAlchemy. It allows you to define tables and models in one go, similar
to how Django works. In addition to the following text I recommend the to how Django works. In addition to the following text I recommend the
official documentation on the `declarative`_ extension. official documentation on the `declarative`_ extension.
Here the example `database.py` module for your application:: Here the example :file:`database.py` module for your application::
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker from sqlalchemy.orm import scoped_session, sessionmaker
@ -70,7 +70,7 @@ when the application shuts down::
def shutdown_session(exception=None): def shutdown_session(exception=None):
db_session.remove() db_session.remove()
Here is an example model (put this into `models.py`, e.g.):: Here is an example model (put this into :file:`models.py`, e.g.)::
from sqlalchemy import Column, Integer, String from sqlalchemy import Column, Integer, String
from yourapplication.database import Base from yourapplication.database import Base
@ -122,7 +122,7 @@ flexible but a little more to type. In general it works like the
declarative approach, so make sure to also split up your application into declarative approach, so make sure to also split up your application into
multiple modules in a package. multiple modules in a package.
Here is an example `database.py` module for your application:: Here is an example :file:`database.py` module for your application::
from sqlalchemy import create_engine, MetaData from sqlalchemy import create_engine, MetaData
from sqlalchemy.orm import scoped_session, sessionmaker from sqlalchemy.orm import scoped_session, sessionmaker
@ -145,7 +145,7 @@ application module::
def shutdown_session(exception=None): def shutdown_session(exception=None):
db_session.remove() db_session.remove()
Here is an example table and model (put this into `models.py`):: Here is an example table and model (put this into :file:`models.py`)::
from sqlalchemy import Table, Column, Integer, String from sqlalchemy import Table, Column, Integer, String
from sqlalchemy.orm import mapper from sqlalchemy.orm import mapper

2
docs/patterns/templateinheritance.rst

@ -14,7 +14,7 @@ with an example.
Base Template Base Template
------------- -------------
This template, which we'll call ``layout.html``, defines a simple HTML skeleton This template, which we'll call :file:`layout.html`, defines a simple HTML skeleton
document that you might use for a simple two-column page. It's the job of document that you might use for a simple two-column page. It's the job of
"child" templates to fill the empty blocks with content: "child" templates to fill the empty blocks with content:

6
docs/patterns/wtforms.rst

@ -77,7 +77,7 @@ how easy this is. WTForms does half the form generation for us already.
To make it even nicer, we can write a macro that renders a field with To make it even nicer, we can write a macro that renders a field with
label and a list of errors if there are any. label and a list of errors if there are any.
Here's an example `_formhelpers.html` template with such a macro: Here's an example :file:`_formhelpers.html` template with such a macro:
.. sourcecode:: html+jinja .. sourcecode:: html+jinja
@ -102,8 +102,8 @@ the input element. Note that WTForms returns standard Python unicode
strings, so we have to tell Jinja2 that this data is already HTML escaped strings, so we have to tell Jinja2 that this data is already HTML escaped
with the `|safe` filter. with the `|safe` filter.
Here the `register.html` template for the function we used above which Here the :file:`register.html` template for the function we used above which
takes advantage of the `_formhelpers.html` template: takes advantage of the :file:`_formhelpers.html` template:
.. sourcecode:: html+jinja .. sourcecode:: html+jinja

10
docs/quickstart.rst

@ -20,8 +20,8 @@ A minimal Flask application looks something like this::
def hello_world(): def hello_world():
return 'Hello World!' return 'Hello World!'
Just save it as `hello.py` (or something similar) and run it with your Python Just save it as :file:`hello.py` (or something similar) and run it with your Python
interpreter. Make sure to not call your application `flask.py` because this interpreter. Make sure to not call your application :file:`flask.py` because this
would conflict with Flask itself. would conflict with Flask itself.
To run the application you can either use the ``flask`` command or To run the application you can either use the ``flask`` command or
@ -370,14 +370,14 @@ Static Files
Dynamic web applications also need static files. That's usually where Dynamic web applications also need static files. That's usually where
the CSS and JavaScript files are coming from. Ideally your web server is the CSS and JavaScript files are coming from. Ideally your web server is
configured to serve them for you, but during development Flask can do that configured to serve them for you, but during development Flask can do that
as well. Just create a folder called `static` in your package or next to as well. Just create a folder called :file:`static` in your package or next to
your module and it will be available at `/static` on the application. your module and it will be available at `/static` on the application.
To generate URLs for static files, use the special ``'static'`` endpoint name:: To generate URLs for static files, use the special ``'static'`` endpoint name::
url_for('static', filename='style.css') url_for('static', filename='style.css')
The file has to be stored on the filesystem as ``static/style.css``. The file has to be stored on the filesystem as :file:`static/style.css`.
Rendering Templates Rendering Templates
------------------- -------------------
@ -399,7 +399,7 @@ Here's a simple example of how to render a template::
def hello(name=None): def hello(name=None):
return render_template('hello.html', name=name) return render_template('hello.html', name=name)
Flask will look for templates in the `templates` folder. So if your Flask will look for templates in the :file:`templates` folder. So if your
application is a module, this folder is next to that module, if it's a application is a module, this folder is next to that module, if it's a
package it's actually inside your package: package it's actually inside your package:

2
docs/testing.rst

@ -30,7 +30,7 @@ The Testing Skeleton
-------------------- --------------------
In order to test the application, we add a second module In order to test the application, we add a second module
(`flaskr_tests.py`) and create a unittest skeleton there:: (:file:`flaskr_tests.py`) and create a unittest skeleton there::
import os import os
import flaskr import flaskr

2
docs/tutorial/css.rst

@ -4,7 +4,7 @@ Step 7: Adding Style
==================== ====================
Now that everything else works, it's time to add some style to the Now that everything else works, it's time to add some style to the
application. Just create a stylesheet called `style.css` in the `static` application. Just create a stylesheet called :file:`style.css` in the :file:`static`
folder we created before: folder we created before:
.. sourcecode:: css .. sourcecode:: css

2
docs/tutorial/dbinit.rst

@ -22,7 +22,7 @@ for you to the application.
To do this we can create a function and hook it into the ``flask`` command To do this we can create a function and hook it into the ``flask`` command
that initializes the database. Let me show you the code first. Just add that initializes the database. Let me show you the code first. Just add
this function below the `connect_db` function in `flaskr.py`:: this function below the `connect_db` function in :file:`flaskr.py`::
def init_db(): def init_db():
db = get_db() db = get_db()

4
docs/tutorial/folders.rst

@ -13,9 +13,9 @@ application::
The `flaskr` folder is not a python package, but just something where we The `flaskr` folder is not a python package, but just something where we
drop our files. We will then put our database schema as well as main module drop our files. We will then put our database schema as well as main module
into this folder. It is done in the following way. The files inside into this folder. It is done in the following way. The files inside
the `static` folder are available to users of the application via HTTP. the :file:`static` folder are available to users of the application via HTTP.
This is the place where css and javascript files go. Inside the This is the place where css and javascript files go. Inside the
`templates` folder Flask will look for `Jinja2`_ templates. The :file:`templates` folder Flask will look for `Jinja2`_ templates. The
templates you create later in the tutorial will go in this directory. templates you create later in the tutorial will go in this directory.
Continue with :ref:`tutorial-schema`. Continue with :ref:`tutorial-schema`.

16
docs/tutorial/setup.rst

@ -11,7 +11,7 @@ directly into the module, and this is what we will be doing here. However
a cleaner solution would be to create a separate `.ini` or `.py` file and a cleaner solution would be to create a separate `.ini` or `.py` file and
load that or import the values from there. load that or import the values from there.
First we add the imports in `flaskr.py`:: First we add the imports in :file:`flaskr.py`::
# all the imports # all the imports
import os import os
@ -20,7 +20,7 @@ First we add the imports in `flaskr.py`::
render_template, flash render_template, flash
Next we can create our actual application and initialize it with the Next we can create our actual application and initialize it with the
config from the same file, in `flaskr.py`:: config from the same file, in :file:`flaskr.py`::
# create our little application :) # create our little application :)
app = Flask(__name__) app = Flask(__name__)
@ -54,14 +54,14 @@ can update it with new values.
:ref:`instance-folders` instead. :ref:`instance-folders` instead.
Usually, it is a good idea to load a separate, environment specific Usually, it is a good idea to load a separate, environment specific
configuration file. Flask allows you to import multiple configurations and it configuration file. Flask allows you to import multiple configurations and it
will use the setting defined in the last import. This enables robust will use the setting defined in the last import. This enables robust
configuration setups. :meth:`~flask.Config.from_envvar` can help achieve this. configuration setups. :meth:`~flask.Config.from_envvar` can help achieve this.
app.config.from_envvar('FLASKR_SETTINGS', silent=True) app.config.from_envvar('FLASKR_SETTINGS', silent=True)
Simply define the environment variable :envvar:`FLASKR_SETTINGS` that points to Simply define the environment variable :envvar:`FLASKR_SETTINGS` that points to
a config file to be loaded. The silent switch just tells Flask to not complain a config file to be loaded. The silent switch just tells Flask to not complain
if no such environment key is set. if no such environment key is set.
In addition to that you can use the :meth:`~flask.Config.from_object` In addition to that you can use the :meth:`~flask.Config.from_object`

4
docs/tutorial/templates.rst

@ -14,7 +14,7 @@ escaped with their XML equivalents.
We are also using template inheritance which makes it possible to reuse We are also using template inheritance which makes it possible to reuse
the layout of the website in all pages. the layout of the website in all pages.
Put the following templates into the `templates` folder: Put the following templates into the :file:`templates` folder:
.. _Jinja2: http://jinja.pocoo.org/docs/templates .. _Jinja2: http://jinja.pocoo.org/docs/templates
@ -55,7 +55,7 @@ the session:
show_entries.html show_entries.html
----------------- -----------------
This template extends the `layout.html` template from above to display the This template extends the :file:`layout.html` template from above to display the
messages. Note that the `for` loop iterates over the messages we passed messages. Note that the `for` loop iterates over the messages we passed
in with the :func:`~flask.render_template` function. We also tell the in with the :func:`~flask.render_template` function. We also tell the
form to submit to your `add_entry` function and use ``POST`` as HTTP form to submit to your `add_entry` function and use ``POST`` as HTTP

2
docs/tutorial/views.rst

@ -16,7 +16,7 @@ returned from the cursor look a bit like tuples because we are using
the :class:`sqlite3.Row` row factory. the :class:`sqlite3.Row` row factory.
The view function will pass the entries as dicts to the The view function will pass the entries as dicts to the
`show_entries.html` template and return the rendered one:: :file:`show_entries.html` template and return the rendered one::
@app.route('/') @app.route('/')
def show_entries(): def show_entries():

12
docs/upgrading.rst

@ -136,11 +136,11 @@ To apply the upgrade script do the following:
patch -p1 < patchfile.diff patch -p1 < patchfile.diff
5. If you were using per-module template folders you need to move some 5. If you were using per-module template folders you need to move some
templates around. Previously if you had a folder named ``templates`` templates around. Previously if you had a folder named :file:`templates`
next to a blueprint named ``admin`` the implicit template path next to a blueprint named ``admin`` the implicit template path
automatically was ``admin/index.html`` for a template file called automatically was :file:`admin/index.html` for a template file called
``templates/index.html``. This no longer is the case. Now you need :file:`templates/index.html`. This no longer is the case. Now you need
to name the template ``templates/admin/index.html``. The tool will to name the template :file:`templates/admin/index.html`. The tool will
not detect this so you will have to do that on your own. not detect this so you will have to do that on your own.
Please note that deprecation warnings are disabled by default starting Please note that deprecation warnings are disabled by default starting
@ -271,7 +271,7 @@ to upgrade. What changed?
modules. modules.
- Blueprints do not automatically provide static folders. They will - Blueprints do not automatically provide static folders. They will
also no longer automatically export templates from a folder called also no longer automatically export templates from a folder called
`templates` next to their location however but it can be enabled from :file:`templates` next to their location however but it can be enabled from
the constructor. Same with static files: if you want to continue the constructor. Same with static files: if you want to continue
serving static files you need to tell the constructor explicitly the serving static files you need to tell the constructor explicitly the
path to the static folder (which can be relative to the blueprint's path to the static folder (which can be relative to the blueprint's
@ -279,7 +279,7 @@ to upgrade. What changed?
- Rendering templates was simplified. Now the blueprints can provide - Rendering templates was simplified. Now the blueprints can provide
template folders which are added to a general template searchpath. template folders which are added to a general template searchpath.
This means that you need to add another subfolder with the blueprint's This means that you need to add another subfolder with the blueprint's
name into that folder if you want ``blueprintname/template.html`` as name into that folder if you want :file:`blueprintname/template.html` as
the template name. the template name.
If you continue to use the `Module` object which is deprecated, Flask will If you continue to use the `Module` object which is deprecated, Flask will

4
examples/flaskr/README

@ -23,8 +23,8 @@
the application will greet you on the application will greet you on
http://localhost:5000/ http://localhost:5000/
~ Is it tested? ~ Is it tested?
You betcha. Run the `test_flaskr.py` file to see You betcha. Run the :file:`test_flaskr.py` file to see
the tests pass. the tests pass.

4
examples/minitwit/README

@ -24,8 +24,8 @@
the application will greet you on the application will greet you on
http://localhost:5000/ http://localhost:5000/
~ Is it tested? ~ Is it tested?
You betcha. Run the `test_minitwit.py` file to You betcha. Run the :file:`test_minitwit.py` file to
see the tests pass. see the tests pass.

6
flask/app.py

@ -71,12 +71,12 @@ class Flask(_PackageBoundObject):
The name of the package is used to resolve resources from inside the The name of the package is used to resolve resources from inside the
package or the folder the module is contained in depending on if the package or the folder the module is contained in depending on if the
package parameter resolves to an actual python package (a folder with package parameter resolves to an actual python package (a folder with
an `__init__.py` file inside) or a standard module (just a `.py` file). an :file:`__init__.py` file inside) or a standard module (just a `.py` file).
For more information about resource loading, see :func:`open_resource`. For more information about resource loading, see :func:`open_resource`.
Usually you create a :class:`Flask` instance in your main module or Usually you create a :class:`Flask` instance in your main module or
in the `__init__.py` file of your package like this:: in the :file:`__init__.py` file of your package like this::
from flask import Flask from flask import Flask
app = Flask(__name__) app = Flask(__name__)
@ -93,7 +93,7 @@ class Flask(_PackageBoundObject):
using a package, it's usually recommended to hardcode the name of using a package, it's usually recommended to hardcode the name of
your package there. your package there.
For example if your application is defined in `yourapplication/app.py` For example if your application is defined in :file:`yourapplication/app.py`
you should create it with one of the two versions below:: you should create it with one of the two versions below::
app = Flask('yourapplication') app = Flask('yourapplication')

2
flask/helpers.py

@ -323,7 +323,7 @@ def url_for(endpoint, **values):
def get_template_attribute(template_name, attribute): def get_template_attribute(template_name, attribute):
"""Loads a macro (or variable) a template exports. This can be used to """Loads a macro (or variable) a template exports. This can be used to
invoke a macro from within Python code. If you for example have a invoke a macro from within Python code. If you for example have a
template named `_cider.html` with the following contents: template named :file:`_cider.html` with the following contents:
.. sourcecode:: html+jinja .. sourcecode:: html+jinja

Loading…
Cancel
Save