docs: :file:`app.py`, :file:`yourapp/templates`
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by defuz · committed by defuz · parent 3fa4fd0 · Browse files · 32 files·+93 added−93 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
32 files+93 added−93 removed
- other:+93 added−93 removed
Provenance
- Commit
a8f570c- Tree
0f0acdc- Author
defuz <defuz.net@gmail.com>- Committer
defuz <defuz.net@gmail.com>- Parent
3fa4fd0
32 files changed+93 added−93 removed
modified:docs/deploying/mod_wsgi.rst+1 added-1 removed
130 unmodified lines131 **Problem:** application does not run, errorlog shows SystemExit ignored132 You have a ``app.run()`` call in your application file that is not133 guarded by an ``if __name__ == '__main__':`` condition. Either134 remove that :meth:`~flask.Flask.run` call from the file and move it135- into a separate `run.py` file or put it into such an if block.135+ into a separate :file:`run.py` file or put it into such an if block.136 137 **Problem:** application gives permission errors138 Probably caused by your application running as the wrong user. Make139 sure the folders the application needs access to have the proper58 unmodified linesmodified:docs/patterns/appfactories.rst+5 added-5 removed
58 unmodified lines59 60 It's preferable to create your extensions and app factories so that the61 extension object does not initially get bound to the application.62 63-Using `Flask-SQLAlchemy <http://pythonhosted.org/Flask-SQLAlchemy/>`_, 63+Using `Flask-SQLAlchemy <http://pythonhosted.org/Flask-SQLAlchemy/>`_,64 as an example, you should not do something along those lines::65- 65+66 def create_app(config_filename):67 app = Flask(__name__)68 app.config.from_pyfile(config_filename)69 1 unmodified line71 72 But, rather, in model.py (or equivalent)::73 74 db = SQLAlchemy()75- 75+76 and in your application.py (or equivalent)::77 78 def create_app(config_filename):79 app = Flask(__name__)2 unmodified lines82 from yourapplication.model import db83 db.init_app(app)84 85 Using this design pattern, no application-specific state is stored on the86-extension object, so one extension object can be used for multiple apps. 86+extension object, so one extension object can be used for multiple apps.87 For more information about the design of extensions refer to :doc:`/extensiondev`.88 89 Using Applications90 ------------------91 92 So to use such an application you then have to create the application93 first in a separate file otherwise the ``flask`` command won't be able94-to find it. Here an example `exampleapp.py` file that creates such94+to find it. Here an example :file:`exampleapp.py` file that creates such95 an application::96 97 from yourapplication import create_app98 app = create_app('/path/to/config.cfg')17 unmodified linesmodified:docs/patterns/distribute.rst+10 added-10 removed
25 unmodified lines26 Flask itself, and all the libraries you can find on the cheeseshop27 are distributed with either distribute, the older setuptools or distutils.28 29 In this case we assume your application is called30-`yourapplication.py` and you are not using a module, but a :ref:`package30+:file:`yourapplication.py` and you are not using a module, but a :ref:`package31 <larger-applications>`. Distributing resources with standard modules is32 not supported by `distribute`_ so we will not bother with it. If you have33 not yet converted your application into a package, head over to the34 :ref:`larger-applications` pattern to see how this can be done.6 unmodified lines41 ------------------42 43 Because you have Flask running, you either have setuptools or distribute44 available on your system anyways. If you do not, fear not, there is a45-script to install it for you: `distribute_setup.py`_. Just download and45+script to install it for you: :file:`distribute_setup.py`_. Just download and46 run with your Python interpreter.47 48 Standard disclaimer applies: :ref:`you better use a virtualenv49 <virtualenv>`.50 51-Your setup code always goes into a file named `setup.py` next to your51+Your setup code always goes into a file named :file:`setup.py` next to your52 application. The name of the file is only convention, but because53 everybody will look for a file with that name, you better not change it.54 55 Yes, even if you are using `distribute`, you are importing from a package56 called `setuptools`. `distribute` is fully backwards compatible with57 `setuptools`, so it also uses the same import name.58 59-A basic `setup.py` file for a Flask application looks like this::59+A basic :file:`setup.py` file for a Flask application looks like this::60 61 from setuptools import setup62 63 setup(18 unmodified lines82 )83 84 Most parameters to the `setup` function should be self explanatory,85 `include_package_data` and `zip_safe` might not be.86-`include_package_data` tells distribute to look for a `MANIFEST.in` file86+`include_package_data` tells distribute to look for a :file:`MANIFEST.in` file87 and install all the entries that match as package data. We will use this88 to distribute the static files and templates along with the Python module89 (see :ref:`distributing-resources`). The `zip_safe` flag can be used to90 force or prevent zip Archive creation. In general you probably don't want6 unmodified lines97 Distributing Resources98 ----------------------99 100 If you try to install the package you just created, you will notice that101-folders like `static` or `templates` are not installed for you. The101+folders like :file:`static` or :file:`templates` are not installed for you. The102 reason for this is that distribute does not know which files to add for103-you. What you should do, is to create a `MANIFEST.in` file next to your104-`setup.py` file. This file lists all the files that should be added to103+you. What you should do, is to create a :file:`MANIFEST.in` file next to your104+:file:`setup.py` file. This file lists all the files that should be added to105 your tarball::106 107 recursive-include yourapplication/templates *108 recursive-include yourapplication/static *109 110-Don't forget that even if you enlist them in your `MANIFEST.in` file, they110+Don't forget that even if you enlist them in your :file:`MANIFEST.in` file, they111 won't be installed for you unless you set the `include_package_data`112 parameter of the `setup` function to ``True``!113 114 29 unmodified lines144 Installing / Developing145 -----------------------146 147 To install your application (ideally into a virtualenv) just run the148-`setup.py` script with the `install` parameter. It will install your148+:file:`setup.py` script with the `install` parameter. It will install your149 application into the virtualenv's site-packages folder and also download150 and install all dependencies::151 152 $ python setup.py install14 unmodified linesmodified:docs/patterns/fabric.rst+3 added-3 removed
13 unmodified lines14 15 - Fabric 1.0 has to be installed locally. This tutorial assumes the16 latest version of Fabric.17 - The application already has to be a package and requires a working18- `setup.py` file (:ref:`distribute-deployment`).18+ :file:`setup.py` file (:ref:`distribute-deployment`).19 - In the following example we are using `mod_wsgi` for the remote20 servers. You can of course use your own favourite server there, but21 for this example we chose Apache + `mod_wsgi` because it's very easy22 to setup and has a simple way to reload applications without root1 unmodified line24 25 Creating the first Fabfile26 --------------------------27 28-A fabfile is what controls what Fabric executes. It is named `fabfile.py`28+A fabfile is what controls what Fabric executes. It is named :file:`fabfile.py`29 and executed by the `fab` command. All the functions defined in that file30 will show up as `fab` subcommands. They are executed on one or more31 hosts. These hosts can be defined either in the fabfile or on the command32 line. In this case we will add them to the fabfile.134 unmodified lines167 168 Fabric will now connect to all servers and run the commands as written169 down in the fabfile. First it will execute pack so that we have our170 tarball ready and then it will execute deploy and upload the source code171-to all servers and install it there. Thanks to the `setup.py` file we171+to all servers and install it there. Thanks to the :file:`setup.py` file we172 will automatically pull in the required libraries into our virtual173 environment.174 175 Next Steps21 unmodified linesmodified:docs/patterns/flashing.rst+1 added-1 removed
36 unmodified lines37 flash('You were successfully logged in')38 return redirect(url_for('index'))39 return render_template('login.html', error=error)40 41-And here the ``layout.html`` template which does the magic:41+And here the :file:`layout.html` template which does the magic:42 43 .. sourcecode:: html+jinja44 45 <!doctype html>102 unmodified linesmodified:docs/patterns/jquery.rst+2 added-2 removed
117 unmodified lines118 119 The HTML120 --------121 122-Your index.html template either has to extend a `layout.html` template with122+Your index.html template either has to extend a :file:`layout.html` template with123 jQuery loaded and the `$SCRIPT_ROOT` variable set, or do that on the top.124-Here's the HTML code needed for our little application (`index.html`).124+Here's the HTML code needed for our little application (:file:`index.html`).125 Notice that we also drop the script directly into the HTML here. It is126 usually a better idea to have that in a separate script file:127 128 .. sourcecode:: html40 unmodified linesmodified:docs/patterns/lazyloading.rst+1 added-1 removed
31 unmodified lines32 def user(username):33 pass34 35 Then the centralized approach you would have one file with the views36-(`views.py`) but without any decorator::36+(:file:`views.py`) but without any decorator::37 38 def index():39 pass40 64 unmodified linesmodified:docs/patterns/mongokit.rst+2 added-2 removed
18 unmodified lines19 20 The default behavior of MongoKit is the declarative one that is based on21 common ideas from Django or the SQLAlchemy declarative extension.22 23-Here an example `app.py` module for your application::23+Here an example :file:`app.py` module for your application::24 25 from flask import Flask26 from mongokit import Connection, Document27 18 unmodified lines46 MongoDB is schemaless. This means you can modify the data structure from one47 insert query to the next without any problem. MongoKit is just schemaless48 too, but implements some validation to ensure data integrity.49 50-Here is an example document (put this also into `app.py`, e.g.)::50+Here is an example document (put this also into :file:`app.py`, e.g.)::51 52 def max_length(length):53 def validate(value):54 if len(value) <= length:87 unmodified linesmodified:docs/patterns/packages.rst+8 added-8 removed
20 unmodified lines21 ---------------22 23 To convert that into a larger one, just create a new folder24 `yourapplication` inside the existing one and move everything below it.25-Then rename `yourapplication.py` to `__init__.py`. (Make sure to delete25+Then rename :file:`yourapplication.py` to :file:`__init__.py`. (Make sure to delete26 all `.pyc` files first, otherwise things would most likely break)27 28 You should then end up with something like that::29 10 unmodified lines40 41 But how do you run your application now? The naive ``python42 yourapplication/__init__.py`` will not work. Let's just say that Python43 does not want modules in packages to be the startup file. But that is not44-a big problem, just add a new file called `runserver.py` next to the inner44+a big problem, just add a new file called :file:`runserver.py` next to the inner45 `yourapplication` folder with the following contents::46 47 from yourapplication import app48 app.run(debug=True)2 unmodified lines51 into multiple modules. The only thing you have to remember is the52 following quick checklist:53 54 1. the `Flask` application object creation has to be in the55- `__init__.py` file. That way each module can import it safely and the55+ :file:`__init__.py` file. That way each module can import it safely and the56 `__name__` variable will resolve to the correct package.57 2. all the view functions (the ones with a :meth:`~flask.Flask.route`58- decorator on top) have to be imported in the `__init__.py` file.58+ decorator on top) have to be imported in the :file:`__init__.py` file.59 Not the object itself, but the module it is in. Import the view module60 **after the application object is created**.61 62-Here's an example `__init__.py`::62+Here's an example :file:`__init__.py`::63 64 from flask import Flask65 app = Flask(__name__)66 67 import yourapplication.views68 69-And this is what `views.py` would look like::69+And this is what :file:`views.py` would look like::70 71 from yourapplication import app72 73 @app.route('/')18 unmodified lines92 .. admonition:: Circular Imports93 94 Every Python programmer hates them, and yet we just added some:95 circular imports (That's when two modules depend on each other. In this96- case `views.py` depends on `__init__.py`). Be advised that this is a96+ case :file:`views.py` depends on :file:`__init__.py`). Be advised that this is a97 bad idea in general but here it is actually fine. The reason for this is98- that we are not actually using the views in `__init__.py` and just98+ that we are not actually using the views in :file:`__init__.py` and just99 ensuring the module is imported and we are doing that at the bottom of100 the file.101 102 There are still some problems with that approach but if you want to use13 unmodified linesmodified:docs/patterns/sqlalchemy.rst+4 added-4 removed
31 unmodified lines32 SQLAlchemy. It allows you to define tables and models in one go, similar33 to how Django works. In addition to the following text I recommend the34 official documentation on the `declarative`_ extension.35 36-Here the example `database.py` module for your application::36+Here the example :file:`database.py` module for your application::37 38 from sqlalchemy import create_engine39 from sqlalchemy.orm import scoped_session, sessionmaker40 from sqlalchemy.ext.declarative import declarative_base28 unmodified lines69 @app.teardown_appcontext70 def shutdown_session(exception=None):71 db_session.remove()72 73-Here is an example model (put this into `models.py`, e.g.)::73+Here is an example model (put this into :file:`models.py`, e.g.)::74 75 from sqlalchemy import Column, Integer, String76 from yourapplication.database import Base77 43 unmodified lines121 flexible but a little more to type. In general it works like the122 declarative approach, so make sure to also split up your application into123 multiple modules in a package.124 125-Here is an example `database.py` module for your application::125+Here is an example :file:`database.py` module for your application::126 127 from sqlalchemy import create_engine, MetaData128 from sqlalchemy.orm import scoped_session, sessionmaker129 14 unmodified lines144 @app.teardown_appcontext145 def shutdown_session(exception=None):146 db_session.remove()147 148-Here is an example table and model (put this into `models.py`)::148+Here is an example table and model (put this into :file:`models.py`)::149 150 from sqlalchemy import Table, Column, Integer, String151 from sqlalchemy.orm import mapper152 from yourapplication.database import metadata, db_session66 unmodified linesmodified:docs/patterns/templateinheritance.rst+1 added-1 removed
12 unmodified lines13 14 Base Template15 -------------16 17-This template, which we'll call ``layout.html``, defines a simple HTML skeleton17+This template, which we'll call :file:`layout.html`, defines a simple HTML skeleton18 document that you might use for a simple two-column page. It's the job of19 "child" templates to fill the empty blocks with content:20 21 .. sourcecode:: html+jinja49 unmodified linesmodified:docs/patterns/wtforms.rst+3 added-3 removed
75 unmodified lines76 how easy this is. WTForms does half the form generation for us already.77 To make it even nicer, we can write a macro that renders a field with78 label and a list of errors if there are any.79 80-Here's an example `_formhelpers.html` template with such a macro:80+Here's an example :file:`_formhelpers.html` template with such a macro:81 82 .. sourcecode:: html+jinja83 84 {% macro render_field(field) %}16 unmodified lines101 the input element. Note that WTForms returns standard Python unicode102 strings, so we have to tell Jinja2 that this data is already HTML escaped103 with the `|safe` filter.104 105-Here the `register.html` template for the function we used above which106-takes advantage of the `_formhelpers.html` template:105+Here the :file:`register.html` template for the function we used above which106+takes advantage of the :file:`_formhelpers.html` template:107 108 .. sourcecode:: html+jinja109 110 {% from "_formhelpers.html" import render_field %}16 unmodified linesmodified:docs/tutorial/css.rst+1 added-1 removed
2 unmodified lines3 Step 7: Adding Style4 ====================5 6 Now that everything else works, it's time to add some style to the7-application. Just create a stylesheet called `style.css` in the `static`7+application. Just create a stylesheet called :file:`style.css` in the :file:`static`8 folder we created before:9 10 .. sourcecode:: css11 20 unmodified linesmodified:docs/tutorial/dbinit.rst+1 added-1 removed
20 unmodified lines21 for you to the application.22 23 To do this we can create a function and hook it into the ``flask`` command24 that initializes the database. Let me show you the code first. Just add25-this function below the `connect_db` function in `flaskr.py`::25+this function below the `connect_db` function in :file:`flaskr.py`::26 27 def init_db():28 db = get_db()29 with app.open_resource('schema.sql', mode='r') as f:43 unmodified linesmodified:docs/tutorial/folders.rst+2 added-2 removed
11 unmodified lines12 13 The `flaskr` folder is not a python package, but just something where we14 drop our files. We will then put our database schema as well as main module15 into this folder. It is done in the following way. The files inside16-the `static` folder are available to users of the application via HTTP.16+the :file:`static` folder are available to users of the application via HTTP.17 This is the place where css and javascript files go. Inside the18-`templates` folder Flask will look for `Jinja2`_ templates. The18+:file:`templates` folder Flask will look for `Jinja2`_ templates. The19 templates you create later in the tutorial will go in this directory.20 21 Continue with :ref:`tutorial-schema`.22 1 unmodified linemodified:docs/tutorial/setup.rst+8 added-8 removed
9 unmodified lines10 directly into the module, and this is what we will be doing here. However11 a cleaner solution would be to create a separate `.ini` or `.py` file and12 load that or import the values from there.13 14-First we add the imports in `flaskr.py`::14+First we add the imports in :file:`flaskr.py`::15 16 # all the imports17 import os18 import sqlite319 from flask import Flask, request, session, g, redirect, url_for, abort, \20 render_template, flash21 22 Next we can create our actual application and initialize it with the23-config from the same file, in `flaskr.py`::23+config from the same file, in :file:`flaskr.py`::24 25 # create our little application :)26 app = Flask(__name__)27 app.config.from_object(__name__)25 unmodified lines53 For a real-work application it's recommended to use54 :ref:`instance-folders` instead.55 56 Usually, it is a good idea to load a separate, environment specific57-configuration file. Flask allows you to import multiple configurations and it 58-will use the setting defined in the last import. This enables robust 59-configuration setups. :meth:`~flask.Config.from_envvar` can help achieve this. 60- 57+configuration file. Flask allows you to import multiple configurations and it58+will use the setting defined in the last import. This enables robust59+configuration setups. :meth:`~flask.Config.from_envvar` can help achieve this.60+61 app.config.from_envvar('FLASKR_SETTINGS', silent=True)62 63-Simply define the environment variable :envvar:`FLASKR_SETTINGS` that points to 64-a config file to be loaded. The silent switch just tells Flask to not complain 63+Simply define the environment variable :envvar:`FLASKR_SETTINGS` that points to64+a config file to be loaded. The silent switch just tells Flask to not complain65 if no such environment key is set.66 67 In addition to that you can use the :meth:`~flask.Config.from_object`68 method on the config object and provide it with an import name of a45 unmodified linesmodified:docs/tutorial/templates.rst+2 added-2 removed
12 unmodified lines13 14 We are also using template inheritance which makes it possible to reuse15 the layout of the website in all pages.16 17-Put the following templates into the `templates` folder:17+Put the following templates into the :file:`templates` folder:18 19 .. _Jinja2: http://jinja.pocoo.org/docs/templates20 21 layout.html32 unmodified lines54 55 show_entries.html56 -----------------57 58-This template extends the `layout.html` template from above to display the58+This template extends the :file:`layout.html` template from above to display the59 messages. Note that the `for` loop iterates over the messages we passed60 in with the :func:`~flask.render_template` function. We also tell the61 form to submit to your `add_entry` function and use ``POST`` as HTTP62 method:49 unmodified linesmodified:docs/tutorial/views.rst+1 added-1 removed
14 unmodified lines15 returned from the cursor look a bit like tuples because we are using16 the :class:`sqlite3.Row` row factory.17 18 The view function will pass the entries as dicts to the19-`show_entries.html` template and return the rendered one::19+:file:`show_entries.html` template and return the rendered one::20 21 @app.route('/')22 def show_entries():23 db = get_db()75 unmodified linesmodified:docs/blueprints.rst+4 added-4 removed
157 unmodified lines158 159 admin = Blueprint('admin', __name__, static_folder='static')160 161 By default the rightmost part of the path is where it is exposed on the162-web. Because the folder is called ``static`` here it will be available at162+web. Because the folder is called :file:`static` here it will be available at163 the location of the blueprint + ``/static``. Say the blueprint is164 registered for ``/admin`` the static folder will be at ``/admin/static``.165 166 The endpoint is named `blueprint_name.static` so you can generate URLs to17 unmodified lines184 185 So if you have a blueprint in the folder ``yourapplication/admin`` and you186 want to render the template ``'admin/index.html'`` and you have provided187 ``templates`` as a `template_folder` you will have to create a file like188-this: ``yourapplication/admin/templates/admin/index.html``.188+this: :file:`yourapplication/admin/templates/admin/index.html`.189 190 To further reiterate this: if you have a blueprint named ``admin`` and you191-want to render a template called ``index.html`` which is specific to this191+want to render a template called :file:`index.html` which is specific to this192 blueprint, the best idea is to lay out your templates like this::193 194 yourpackage/195 blueprints/2 unmodified lines198 admin/199 index.html200 __init__.py201 202-And then when you want to render the template, use ``admin/index.html`` as202+And then when you want to render the template, use :file:`admin/index.html` as203 the name to look up the template by. If you encounter problems loading204 the correct templates enable the ``EXPLAIN_TEMPLATE_LOADING`` config205 variable which will instruct Flask to print out the steps it goes through206 to locate templates on every ``render_template`` call.33 unmodified linesmodified:docs/cli.rst+3 added-3 removed
33 unmodified lines34 35 In that imported file the name of the app needs to be called ``app`` or36 optionally be specified after a colon.37 38-Given a ``hello.py`` file with the application in it named ``app`` this is38+Given a :file:`hello.py` file with the application in it named ``app`` this is39 how it can be run.40 41 Environment variables (On Windows use ``set`` instead of ``export``)::42 89 unmodified lines132 For instance if you have a factory function that creates an application133 from a filename you could make a separate file that creates such an134 application from an environment variable.135 136-This could be a file named ``autoapp.py`` with these contents::136+This could be a file named :file:`autoapp.py` with these contents::137 138 import os139 from yourapplication import create_app140 app = create_app(os.environ['YOURAPPLICATION_CONFIG'])37 unmodified lines178 We won't go into detail now about the differences but if you are curious179 you can have a look at the :ref:`script-info-object` section to learn all180 about it.181 182-To explain all of this, here is an example ``manage.py`` script that182+To explain all of this, here is an example :file:`manage.py` script that183 manages a hypothetical wiki application. We will go through the details184 afterwards::185 186 import click73 unmodified linesmodified:docs/config.rst+1 added-1 removed
321 unmodified lines322 app = Flask(__name__)323 app.config.from_object('yourapplication.default_settings')324 app.config.from_envvar('YOURAPPLICATION_SETTINGS')325 326-Then you just have to add a separate `config.py` file and export326+Then you just have to add a separate :file:`config.py` file and export327 ``YOURAPPLICATION_SETTINGS=/path/to/config.py`` and you are done. However328 there are alternative ways as well. For example you could use imports or329 subclassing.330 132 unmodified linesmodified:docs/extensiondev.rst+5 added-5 removed
25 unmodified lines26 The name of the actual extension (the human readable name) however would27 be something like "Flask-SimpleXML". Make sure to include the name28 "Flask" somewhere in that name and that you check the capitalization.29 This is how users can then register dependencies to your extension in30-their `setup.py` files.30+their :file:`setup.py` files.31 32 Flask sets up a redirect package called :data:`flask.ext` where users33 should import the extensions from. If you for instance have a package34 called ``flask_something`` users would import it as6 unmodified lines41 :ref:`app-factories` pattern to create their application as needed to aid42 unittests and to support multiple configurations. Because of that it is43 crucial that your application supports that kind of behavior.44 45-Most importantly the extension must be shipped with a `setup.py` file and45+Most importantly the extension must be shipped with a :file:`setup.py` file and46 registered on PyPI. Also the development checkout link should work so47 that people can easily install the development version into their48 virtualenv without having to download the library by hand.49 19 unmodified lines69 70 setup.py71 ````````72 73-The next file that is absolutely required is the `setup.py` file which is73+The next file that is absolutely required is the :file:`setup.py` file which is74 used to install your Flask extension. The following contents are75 something you can work with::76 77 """287 unmodified lines365 2. It must ship a testing suite that can either be invoked with ``make test``366 or ``python setup.py test``. For test suites invoked with ``make367 test`` the extension has to ensure that all dependencies for the test368 are installed automatically. If tests are invoked with ``python setup.py369- test``, test dependencies can be specified in the `setup.py` file. The369+ test``, test dependencies can be specified in the :file:`setup.py` file. The370 test suite also has to be part of the distribution.371 3. APIs of approved extensions will be checked for the following372 characteristics:373 5 unmodified lines379 4. The license must be BSD/MIT/WTFPL licensed.380 5. The naming scheme for official extensions is *Flask-ExtensionName* or381 *ExtensionName-Flask*.382 6. Approved extensions must define all their dependencies in the383- `setup.py` file unless a dependency cannot be met because it is not383+ :file:`setup.py` file unless a dependency cannot be met because it is not384 available on PyPI.385 7. The extension must have documentation that uses one of the two Flask386 themes for Sphinx documentation.387 8. The setup.py description (and thus the PyPI description) has to33 unmodified linesmodified:docs/extensions.rst+2 added-2 removed
7 unmodified lines8 ------------------9 10 Flask extensions are listed on the `Flask Extension Registry`_ and can be11 downloaded with ``easy_install`` or ``pip``. If you add a Flask extension12-as dependency to your ``requirements.rst`` or ``setup.py`` file they are12+as dependency to your ``requirements.rst`` or :file:`setup.py` file they are13 usually installed with a simple command or when your application installs.14 15 Using Extensions16 ----------------14 unmodified lines31 depending on how the extension is distributed. If you want to develop an32 application that supports Flask 0.7 or earlier you should still import33 from the :data:`flask.ext` package. We provide you with a compatibility34 module that provides this package for older versions of Flask. You can35-download it from github: `flaskext_compat.py`_35+download it from github: :file:`flaskext_compat.py`_36 37 And here is how you can use it::38 39 import flaskext_compat9 unmodified linesmodified:docs/foreword.rst+1 added-1 removed
28 unmodified lines29 30 Flask has many configuration values, with sensible defaults, and a few31 conventions when getting started. By convention templates and static files are32 stored in subdirectories within the application's Python source tree, with the33-names `templates` and `static` respectively. While this can be changed you33+names :file:`templates` and :file:`static` respectively. While this can be changed you34 usually don't have to, especially when getting started.35 36 Growing with Flask37 ------------------20 unmodified linesmodified:docs/installation.rst+1 added-1 removed
148 unmodified lines149 `get-pip.py`_150 151 To install the latest setuptools, you can use its bootstrap file:152 153-`ez_setup.py`_153+:file:`ez_setup.py`_154 155 Either should be double-clickable once you download them. If you already have pip,156 you can upgrade them by running::157 19 unmodified linesmodified:docs/quickstart.rst+5 added-5 removed
18 unmodified lines19 @app.route('/')20 def hello_world():21 return 'Hello World!'22 23-Just save it as `hello.py` (or something similar) and run it with your Python24-interpreter. Make sure to not call your application `flask.py` because this23+Just save it as :file:`hello.py` (or something similar) and run it with your Python24+interpreter. Make sure to not call your application :file:`flask.py` because this25 would conflict with Flask itself.26 27 To run the application you can either use the ``flask`` command or28 python's ``-m`` switch with Flask::340 unmodified lines369 370 Dynamic web applications also need static files. That's usually where371 the CSS and JavaScript files are coming from. Ideally your web server is372 configured to serve them for you, but during development Flask can do that373-as well. Just create a folder called `static` in your package or next to373+as well. Just create a folder called :file:`static` in your package or next to374 your module and it will be available at `/static` on the application.375 376 To generate URLs for static files, use the special ``'static'`` endpoint name::377 378 url_for('static', filename='style.css')379 380-The file has to be stored on the filesystem as ``static/style.css``.380+The file has to be stored on the filesystem as :file:`static/style.css`.381 382 Rendering Templates383 -------------------384 13 unmodified lines398 @app.route('/hello/<name>')399 def hello(name=None):400 return render_template('hello.html', name=name)401 402-Flask will look for templates in the `templates` folder. So if your402+Flask will look for templates in the :file:`templates` folder. So if your403 application is a module, this folder is next to that module, if it's a404 package it's actually inside your package:405 406 **Case 1**: a module::487 unmodified linesmodified:docs/testing.rst+1 added-1 removed
28 unmodified lines29 The Testing Skeleton30 --------------------31 32 In order to test the application, we add a second module33-(`flaskr_tests.py`) and create a unittest skeleton there::33+(:file:`flaskr_tests.py`) and create a unittest skeleton there::34 35 import os36 import flaskr37 import unittest316 unmodified linesmodified:docs/upgrading.rst+6 added-6 removed
134 unmodified lines135 136 patch -p1 < patchfile.diff137 138 5. If you were using per-module template folders you need to move some139- templates around. Previously if you had a folder named ``templates``139+ templates around. Previously if you had a folder named :file:`templates`140 next to a blueprint named ``admin`` the implicit template path141- automatically was ``admin/index.html`` for a template file called142- ``templates/index.html``. This no longer is the case. Now you need143- to name the template ``templates/admin/index.html``. The tool will141+ automatically was :file:`admin/index.html` for a template file called142+ :file:`templates/index.html`. This no longer is the case. Now you need143+ to name the template :file:`templates/admin/index.html`. The tool will144 not detect this so you will have to do that on your own.145 146 Please note that deprecation warnings are disabled by default starting147 with Python 2.7. In order to see the deprecation warnings that might be122 unmodified lines270 some unnecessary leading dots in your code if you're not using271 modules.272 - Blueprints do not automatically provide static folders. They will273 also no longer automatically export templates from a folder called274- `templates` next to their location however but it can be enabled from274+ :file:`templates` next to their location however but it can be enabled from275 the constructor. Same with static files: if you want to continue276 serving static files you need to tell the constructor explicitly the277 path to the static folder (which can be relative to the blueprint's278 module path).279 - Rendering templates was simplified. Now the blueprints can provide280 template folders which are added to a general template searchpath.281 This means that you need to add another subfolder with the blueprint's282- name into that folder if you want ``blueprintname/template.html`` as282+ name into that folder if you want :file:`blueprintname/template.html` as283 the template name.284 285 If you continue to use the `Module` object which is deprecated, Flask will286 restore the previous behavior as good as possible. However we strongly96 unmodified linesmodified:examples/flaskr/README+2 added-2 removed
21 unmodified lines22 flask --app=flaskr run23 24 the application will greet you on25 http://localhost:5000/26- 26+27 ~ Is it tested?28 29- You betcha. Run the `test_flaskr.py` file to see29+ You betcha. Run the :file:`test_flaskr.py` file to see30 the tests pass.modified:examples/minitwit/README+2 added-2 removed
22 unmodified lines23 flask --app=minitwit run24 25 the application will greet you on26 http://localhost:5000/27- 27+28 ~ Is it tested?29 30- You betcha. Run the `test_minitwit.py` file to30+ You betcha. Run the :file:`test_minitwit.py` file to31 see the tests pass.modified:flask/app.py+3 added-3 removed
69 unmodified lines70 71 The name of the package is used to resolve resources from inside the72 package or the folder the module is contained in depending on if the73 package parameter resolves to an actual python package (a folder with74- an `__init__.py` file inside) or a standard module (just a `.py` file).74+ an :file:`__init__.py` file inside) or a standard module (just a `.py` file).75 76 For more information about resource loading, see :func:`open_resource`.77 78 Usually you create a :class:`Flask` instance in your main module or79- in the `__init__.py` file of your package like this::79+ in the :file:`__init__.py` file of your package like this::80 81 from flask import Flask82 app = Flask(__name__)83 8 unmodified lines92 module, `__name__` is always the correct value. If you however are93 using a package, it's usually recommended to hardcode the name of94 your package there.95 96- For example if your application is defined in `yourapplication/app.py`96+ For example if your application is defined in :file:`yourapplication/app.py`97 you should create it with one of the two versions below::98 99 app = Flask('yourapplication')100 app = Flask(__name__.split('.')[0])1809 unmodified linesmodified:flask/helpers.py+1 added-1 removed
321 unmodified lines322 323 def get_template_attribute(template_name, attribute):324 """Loads a macro (or variable) a template exports. This can be used to325 invoke a macro from within Python code. If you for example have a326- template named `_cider.html` with the following contents:326+ template named :file:`_cider.html` with the following contents:327 328 .. sourcecode:: html+jinja329 330 {% macro hello(name) %}Hello {{ name }}!{% endmacro %}573 unmodified lines