Skip to content
Myr

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

32 files+93 added−93 removed

modified:docs/deploying/mod_wsgi.rst+1 added-1 removed
130 unmodified lines
131**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.136137**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 proper
58 unmodified lines
modified:docs/patterns/appfactories.rst+5 added-5 removed
58 unmodified lines
5960It's preferable to create your extensions and app factories so that the61extension object does not initially get bound to the application.6263Using `Flask-SQLAlchemy <http://pythonhosted.org/Flask-SQLAlchemy/>`_, 63Using `Flask-SQLAlchemy <http://pythonhosted.org/Flask-SQLAlchemy/>`_,64as an example, you should not do something along those lines::65 6566 def create_app(config_filename):67 app = Flask(__name__)68 app.config.from_pyfile(config_filename)69
1 unmodified line
7172But, rather, in model.py (or equivalent)::7374 db = SQLAlchemy()75 7576and in your application.py (or equivalent)::7778 def create_app(config_filename):79 app = Flask(__name__)
2 unmodified lines
82 from yourapplication.model import db83 db.init_app(app)8485Using this design pattern, no application-specific state is stored on the86extension object, so one extension object can be used for multiple apps. 86extension object, so one extension object can be used for multiple apps.87For more information about the design of extensions refer to :doc:`/extensiondev`.8889Using Applications90------------------9192So to use such an application you then have to create the application93first in a separate file otherwise the ``flask`` command won't be able94to find it. Here an example `exampleapp.py` file that creates such94to find it. Here an example :file:`exampleapp.py` file that creates such95an application::9697 from yourapplication import create_app98 app = create_app('/path/to/config.cfg')
17 unmodified lines
modified:docs/patterns/distribute.rst+10 added-10 removed
25 unmodified lines
26Flask itself, and all the libraries you can find on the cheeseshop27are distributed with either distribute, the older setuptools or distutils.2829In 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 is32not supported by `distribute`_ so we will not bother with it. If you have33not yet converted your application into a package, head over to the34:ref:`larger-applications` pattern to see how this can be done.
6 unmodified lines
41------------------4243Because you have Flask running, you either have setuptools or distribute44available on your system anyways. If you do not, fear not, there is a45script to install it for you: `distribute_setup.py`_. Just download and45script to install it for you: :file:`distribute_setup.py`_. Just download and46run with your Python interpreter.4748Standard disclaimer applies: :ref:`you better use a virtualenv49<virtualenv>`.5051Your setup code always goes into a file named `setup.py` next to your51Your setup code always goes into a file named :file:`setup.py` next to your52application. The name of the file is only convention, but because53everybody will look for a file with that name, you better not change it.5455Yes, even if you are using `distribute`, you are importing from a package56called `setuptools`. `distribute` is fully backwards compatible with57`setuptools`, so it also uses the same import name.5859A basic `setup.py` file for a Flask application looks like this::59A basic :file:`setup.py` file for a Flask application looks like this::6061 from setuptools import setup6263 setup(
18 unmodified lines
82 )8384Most 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` file87and install all the entries that match as package data. We will use this88to distribute the static files and templates along with the Python module89(see :ref:`distributing-resources`). The `zip_safe` flag can be used to90force or prevent zip Archive creation. In general you probably don't want
6 unmodified lines
97Distributing Resources98----------------------99100If you try to install the package you just created, you will notice that101folders like `static` or `templates` are not installed for you. The101folders like :file:`static` or :file:`templates` are not installed for you. The102reason for this is that distribute does not know which files to add for103you. 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 to103you. 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 to105your tarball::106107 recursive-include yourapplication/templates *108 recursive-include yourapplication/static *109110Don't forget that even if you enlist them in your `MANIFEST.in` file, they110Don't forget that even if you enlist them in your :file:`MANIFEST.in` file, they111won't be installed for you unless you set the `include_package_data`112parameter of the `setup` function to ``True``!113114
29 unmodified lines
144Installing / Developing145-----------------------146147To 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 your149application into the virtualenv's site-packages folder and also download150and install all dependencies::151152 $ python setup.py install
14 unmodified lines
modified:docs/patterns/fabric.rst+3 added-3 removed
13 unmodified lines
1415- 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 root
1 unmodified line
2425Creating the first Fabfile26--------------------------2728A fabfile is what controls what Fabric executes. It is named `fabfile.py`28A fabfile is what controls what Fabric executes. It is named :file:`fabfile.py`29and executed by the `fab` command. All the functions defined in that file30will show up as `fab` subcommands. They are executed on one or more31hosts. These hosts can be defined either in the fabfile or on the command32line. In this case we will add them to the fabfile.
134 unmodified lines
167168Fabric will now connect to all servers and run the commands as written169down in the fabfile. First it will execute pack so that we have our170tarball ready and then it will execute deploy and upload the source code171to all servers and install it there. Thanks to the `setup.py` file we171to all servers and install it there. Thanks to the :file:`setup.py` file we172will automatically pull in the required libraries into our virtual173environment.174175Next Steps
21 unmodified lines
modified:docs/patterns/flashing.rst+1 added-1 removed
36 unmodified lines
37 flash('You were successfully logged in')38 return redirect(url_for('index'))39 return render_template('login.html', error=error)4041And here the ``layout.html`` template which does the magic:41And here the :file:`layout.html` template which does the magic:4243.. sourcecode:: html+jinja4445 <!doctype html>
102 unmodified lines
modified:docs/patterns/jquery.rst+2 added-2 removed
117 unmodified lines
118119The HTML120--------121122Your index.html template either has to extend a `layout.html` template with122Your index.html template either has to extend a :file:`layout.html` template with123jQuery loaded and the `$SCRIPT_ROOT` variable set, or do that on the top.124Here's the HTML code needed for our little application (`index.html`).124Here's the HTML code needed for our little application (:file:`index.html`).125Notice that we also drop the script directly into the HTML here. It is126usually a better idea to have that in a separate script file:127128.. sourcecode:: html
40 unmodified lines
modified:docs/patterns/lazyloading.rst+1 added-1 removed
31 unmodified lines
32 def user(username):33 pass3435Then 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::3738 def index():39 pass40
64 unmodified lines
modified:docs/patterns/mongokit.rst+2 added-2 removed
18 unmodified lines
1920The default behavior of MongoKit is the declarative one that is based on21common ideas from Django or the SQLAlchemy declarative extension.2223Here an example `app.py` module for your application::23Here an example :file:`app.py` module for your application::2425 from flask import Flask26 from mongokit import Connection, Document27
18 unmodified lines
46MongoDB is schemaless. This means you can modify the data structure from one47insert query to the next without any problem. MongoKit is just schemaless48too, but implements some validation to ensure data integrity.4950Here is an example document (put this also into `app.py`, e.g.)::50Here is an example document (put this also into :file:`app.py`, e.g.)::5152 def max_length(length):53 def validate(value):54 if len(value) <= length:
87 unmodified lines
modified:docs/patterns/packages.rst+8 added-8 removed
20 unmodified lines
21---------------2223To convert that into a larger one, just create a new folder24`yourapplication` inside the existing one and move everything below it.25Then rename `yourapplication.py` to `__init__.py`. (Make sure to delete25Then rename :file:`yourapplication.py` to :file:`__init__.py`. (Make sure to delete26all `.pyc` files first, otherwise things would most likely break)2728You should then end up with something like that::29
10 unmodified lines
4041But how do you run your application now? The naive ``python42yourapplication/__init__.py`` will not work. Let's just say that Python43does not want modules in packages to be the startup file. But that is not44a big problem, just add a new file called `runserver.py` next to the inner44a big problem, just add a new file called :file:`runserver.py` next to the inner45`yourapplication` folder with the following contents::4647 from yourapplication import app48 app.run(debug=True)
2 unmodified lines
51into multiple modules. The only thing you have to remember is the52following quick checklist:53541. 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.572. 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**.6162Here's an example `__init__.py`::62Here's an example :file:`__init__.py`::6364 from flask import Flask65 app = Flask(__name__)6667 import yourapplication.views6869And this is what `views.py` would look like::69And this is what :file:`views.py` would look like::7071 from yourapplication import app7273 @app.route('/')
18 unmodified lines
92.. admonition:: Circular Imports9394 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.101102 There are still some problems with that approach but if you want to use
13 unmodified lines
modified:docs/patterns/sqlalchemy.rst+4 added-4 removed
31 unmodified lines
32SQLAlchemy. It allows you to define tables and models in one go, similar33to how Django works. In addition to the following text I recommend the34official documentation on the `declarative`_ extension.3536Here the example `database.py` module for your application::36Here the example :file:`database.py` module for your application::3738 from sqlalchemy import create_engine39 from sqlalchemy.orm import scoped_session, sessionmaker40 from sqlalchemy.ext.declarative import declarative_base
28 unmodified lines
69 @app.teardown_appcontext70 def shutdown_session(exception=None):71 db_session.remove()7273Here is an example model (put this into `models.py`, e.g.)::73Here is an example model (put this into :file:`models.py`, e.g.)::7475 from sqlalchemy import Column, Integer, String76 from yourapplication.database import Base77
43 unmodified lines
121flexible but a little more to type. In general it works like the122declarative approach, so make sure to also split up your application into123multiple modules in a package.124125Here is an example `database.py` module for your application::125Here is an example :file:`database.py` module for your application::126127 from sqlalchemy import create_engine, MetaData128 from sqlalchemy.orm import scoped_session, sessionmaker129
14 unmodified lines
144 @app.teardown_appcontext145 def shutdown_session(exception=None):146 db_session.remove()147148Here is an example table and model (put this into `models.py`)::148Here is an example table and model (put this into :file:`models.py`)::149150 from sqlalchemy import Table, Column, Integer, String151 from sqlalchemy.orm import mapper152 from yourapplication.database import metadata, db_session
66 unmodified lines
modified:docs/patterns/templateinheritance.rst+1 added-1 removed
12 unmodified lines
1314Base Template15-------------1617This template, which we'll call ``layout.html``, defines a simple HTML skeleton17This template, which we'll call :file:`layout.html`, defines a simple HTML skeleton18document that you might use for a simple two-column page. It's the job of19"child" templates to fill the empty blocks with content:2021.. sourcecode:: html+jinja
49 unmodified lines
modified:docs/patterns/wtforms.rst+3 added-3 removed
75 unmodified lines
76how easy this is. WTForms does half the form generation for us already.77To make it even nicer, we can write a macro that renders a field with78label and a list of errors if there are any.7980Here's an example `_formhelpers.html` template with such a macro:80Here's an example :file:`_formhelpers.html` template with such a macro:8182.. sourcecode:: html+jinja8384 {% macro render_field(field) %}
16 unmodified lines
101the input element. Note that WTForms returns standard Python unicode102strings, so we have to tell Jinja2 that this data is already HTML escaped103with the `|safe` filter.104105Here the `register.html` template for the function we used above which106takes advantage of the `_formhelpers.html` template:105Here the :file:`register.html` template for the function we used above which106takes advantage of the :file:`_formhelpers.html` template:107108.. sourcecode:: html+jinja109110 {% from "_formhelpers.html" import render_field %}
16 unmodified lines
modified:docs/tutorial/css.rst+1 added-1 removed
2 unmodified lines
3Step 7: Adding Style4====================56Now that everything else works, it's time to add some style to the7application. Just create a stylesheet called `style.css` in the `static`7application. Just create a stylesheet called :file:`style.css` in the :file:`static`8folder we created before:910.. sourcecode:: css11
20 unmodified lines
modified:docs/tutorial/dbinit.rst+1 added-1 removed
20 unmodified lines
21for you to the application.2223To do this we can create a function and hook it into the ``flask`` command24that initializes the database. Let me show you the code first. Just add25this function below the `connect_db` function in `flaskr.py`::25this function below the `connect_db` function in :file:`flaskr.py`::2627 def init_db():28 db = get_db()29 with app.open_resource('schema.sql', mode='r') as f:
43 unmodified lines
modified:docs/tutorial/folders.rst+2 added-2 removed
11 unmodified lines
1213The `flaskr` folder is not a python package, but just something where we14drop our files. We will then put our database schema as well as main module15into this folder. It is done in the following way. The files inside16the `static` folder are available to users of the application via HTTP.16the :file:`static` folder are available to users of the application via HTTP.17This 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. The19templates you create later in the tutorial will go in this directory.2021Continue with :ref:`tutorial-schema`.22
1 unmodified line
modified:docs/tutorial/setup.rst+8 added-8 removed
9 unmodified lines
10directly into the module, and this is what we will be doing here. However11a cleaner solution would be to create a separate `.ini` or `.py` file and12load that or import the values from there.1314First we add the imports in `flaskr.py`::14First we add the imports in :file:`flaskr.py`::1516 # all the imports17 import os18 import sqlite319 from flask import Flask, request, session, g, redirect, url_for, abort, \20 render_template, flash2122Next we can create our actual application and initialize it with the23config from the same file, in `flaskr.py`::23config from the same file, in :file:`flaskr.py`::2425 # create our little application :)26 app = Flask(__name__)27 app.config.from_object(__name__)
25 unmodified lines
53 For a real-work application it's recommended to use54 :ref:`instance-folders` instead.5556Usually, it is a good idea to load a separate, environment specific57configuration file. Flask allows you to import multiple configurations and it 58will use the setting defined in the last import. This enables robust 59configuration setups. :meth:`~flask.Config.from_envvar` can help achieve this. 60 57configuration file. Flask allows you to import multiple configurations and it58will use the setting defined in the last import. This enables robust59configuration setups. :meth:`~flask.Config.from_envvar` can help achieve this.6061 app.config.from_envvar('FLASKR_SETTINGS', silent=True)6263Simply define the environment variable :envvar:`FLASKR_SETTINGS` that points to 64a config file to be loaded. The silent switch just tells Flask to not complain 63Simply define the environment variable :envvar:`FLASKR_SETTINGS` that points to64a config file to be loaded. The silent switch just tells Flask to not complain65if no such environment key is set.6667In addition to that you can use the :meth:`~flask.Config.from_object`68method on the config object and provide it with an import name of a
45 unmodified lines
modified:docs/tutorial/templates.rst+2 added-2 removed
12 unmodified lines
1314We are also using template inheritance which makes it possible to reuse15the layout of the website in all pages.1617Put the following templates into the `templates` folder:17Put the following templates into the :file:`templates` folder:1819.. _Jinja2: http://jinja.pocoo.org/docs/templates2021layout.html
32 unmodified lines
5455show_entries.html56-----------------5758This template extends the `layout.html` template from above to display the58This template extends the :file:`layout.html` template from above to display the59messages. Note that the `for` loop iterates over the messages we passed60in with the :func:`~flask.render_template` function. We also tell the61form to submit to your `add_entry` function and use ``POST`` as HTTP62method:
49 unmodified lines
modified:docs/tutorial/views.rst+1 added-1 removed
14 unmodified lines
15returned from the cursor look a bit like tuples because we are using16the :class:`sqlite3.Row` row factory.1718The 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::2021 @app.route('/')22 def show_entries():23 db = get_db()
75 unmodified lines
modified:docs/blueprints.rst+4 added-4 removed
157 unmodified lines
158159 admin = Blueprint('admin', __name__, static_folder='static')160161By default the rightmost part of the path is where it is exposed on the162web. Because the folder is called ``static`` here it will be available at162web. Because the folder is called :file:`static` here it will be available at163the location of the blueprint + ``/static``. Say the blueprint is164registered for ``/admin`` the static folder will be at ``/admin/static``.165166The endpoint is named `blueprint_name.static` so you can generate URLs to
17 unmodified lines
184185So if you have a blueprint in the folder ``yourapplication/admin`` and you186want to render the template ``'admin/index.html'`` and you have provided187``templates`` as a `template_folder` you will have to create a file like188this: ``yourapplication/admin/templates/admin/index.html``.188this: :file:`yourapplication/admin/templates/admin/index.html`.189190To further reiterate this: if you have a blueprint named ``admin`` and you191want to render a template called ``index.html`` which is specific to this191want to render a template called :file:`index.html` which is specific to this192blueprint, the best idea is to lay out your templates like this::193194 yourpackage/195 blueprints/
2 unmodified lines
198 admin/199 index.html200 __init__.py201202And then when you want to render the template, use ``admin/index.html`` as202And then when you want to render the template, use :file:`admin/index.html` as203the name to look up the template by. If you encounter problems loading204the correct templates enable the ``EXPLAIN_TEMPLATE_LOADING`` config205variable which will instruct Flask to print out the steps it goes through206to locate templates on every ``render_template`` call.
33 unmodified lines
modified:docs/cli.rst+3 added-3 removed
33 unmodified lines
3435In that imported file the name of the app needs to be called ``app`` or36optionally be specified after a colon.3738Given a ``hello.py`` file with the application in it named ``app`` this is38Given a :file:`hello.py` file with the application in it named ``app`` this is39how it can be run.4041Environment variables (On Windows use ``set`` instead of ``export``)::42
89 unmodified lines
132For instance if you have a factory function that creates an application133from a filename you could make a separate file that creates such an134application from an environment variable.135136This could be a file named ``autoapp.py`` with these contents::136This could be a file named :file:`autoapp.py` with these contents::137138 import os139 from yourapplication import create_app140 app = create_app(os.environ['YOURAPPLICATION_CONFIG'])
37 unmodified lines
178We won't go into detail now about the differences but if you are curious179you can have a look at the :ref:`script-info-object` section to learn all180about it.181182To explain all of this, here is an example ``manage.py`` script that182To explain all of this, here is an example :file:`manage.py` script that183manages a hypothetical wiki application. We will go through the details184afterwards::185186 import click
73 unmodified lines
modified:docs/config.rst+1 added-1 removed
321 unmodified lines
322 app = Flask(__name__)323 app.config.from_object('yourapplication.default_settings')324 app.config.from_envvar('YOURAPPLICATION_SETTINGS')325326Then you just have to add a separate `config.py` file and export326Then you just have to add a separate :file:`config.py` file and export327``YOURAPPLICATION_SETTINGS=/path/to/config.py`` and you are done. However328there are alternative ways as well. For example you could use imports or329subclassing.330
132 unmodified lines
modified:docs/extensiondev.rst+5 added-5 removed
25 unmodified lines
26The name of the actual extension (the human readable name) however would27be something like "Flask-SimpleXML". Make sure to include the name28"Flask" somewhere in that name and that you check the capitalization.29This is how users can then register dependencies to your extension in30their `setup.py` files.30their :file:`setup.py` files.3132Flask sets up a redirect package called :data:`flask.ext` where users33should import the extensions from. If you for instance have a package34called ``flask_something`` users would import it as
6 unmodified lines
41:ref:`app-factories` pattern to create their application as needed to aid42unittests and to support multiple configurations. Because of that it is43crucial that your application supports that kind of behavior.4445Most importantly the extension must be shipped with a `setup.py` file and45Most importantly the extension must be shipped with a :file:`setup.py` file and46registered on PyPI. Also the development checkout link should work so47that people can easily install the development version into their48virtualenv without having to download the library by hand.49
19 unmodified lines
6970setup.py71````````7273The next file that is absolutely required is the `setup.py` file which is73The next file that is absolutely required is the :file:`setup.py` file which is74used to install your Flask extension. The following contents are75something you can work with::7677 """
287 unmodified lines
3652. 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.3713. APIs of approved extensions will be checked for the following372 characteristics:373
5 unmodified lines
3794. The license must be BSD/MIT/WTFPL licensed.3805. The naming scheme for official extensions is *Flask-ExtensionName* or381 *ExtensionName-Flask*.3826. 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.3857. The extension must have documentation that uses one of the two Flask386 themes for Sphinx documentation.3878. The setup.py description (and thus the PyPI description) has to
33 unmodified lines
modified:docs/extensions.rst+2 added-2 removed
7 unmodified lines
8------------------910Flask extensions are listed on the `Flask Extension Registry`_ and can be11downloaded with ``easy_install`` or ``pip``. If you add a Flask extension12as dependency to your ``requirements.rst`` or ``setup.py`` file they are12as dependency to your ``requirements.rst`` or :file:`setup.py` file they are13usually installed with a simple command or when your application installs.1415Using Extensions16----------------
14 unmodified lines
31depending on how the extension is distributed. If you want to develop an32application that supports Flask 0.7 or earlier you should still import33from the :data:`flask.ext` package. We provide you with a compatibility34module that provides this package for older versions of Flask. You can35download it from github: `flaskext_compat.py`_35download it from github: :file:`flaskext_compat.py`_3637And here is how you can use it::3839 import flaskext_compat
9 unmodified lines
modified:docs/foreword.rst+1 added-1 removed
28 unmodified lines
2930Flask has many configuration values, with sensible defaults, and a few31conventions when getting started. By convention templates and static files are32stored in subdirectories within the application's Python source tree, with the33names `templates` and `static` respectively. While this can be changed you33names :file:`templates` and :file:`static` respectively. While this can be changed you34usually don't have to, especially when getting started.3536Growing with Flask37------------------
20 unmodified lines
modified:docs/installation.rst+1 added-1 removed
148 unmodified lines
149`get-pip.py`_150151To install the latest setuptools, you can use its bootstrap file:152153`ez_setup.py`_153:file:`ez_setup.py`_154155Either should be double-clickable once you download them. If you already have pip,156you can upgrade them by running::157
19 unmodified lines
modified:docs/quickstart.rst+5 added-5 removed
18 unmodified lines
19 @app.route('/')20 def hello_world():21 return 'Hello World!'2223Just save it as `hello.py` (or something similar) and run it with your Python24interpreter. Make sure to not call your application `flask.py` because this23Just save it as :file:`hello.py` (or something similar) and run it with your Python24interpreter. Make sure to not call your application :file:`flask.py` because this25would conflict with Flask itself.2627To run the application you can either use the ``flask`` command or28python's ``-m`` switch with Flask::
340 unmodified lines
369370Dynamic web applications also need static files. That's usually where371the CSS and JavaScript files are coming from. Ideally your web server is372configured to serve them for you, but during development Flask can do that373as well. Just create a folder called `static` in your package or next to373as well. Just create a folder called :file:`static` in your package or next to374your module and it will be available at `/static` on the application.375376To generate URLs for static files, use the special ``'static'`` endpoint name::377378 url_for('static', filename='style.css')379380The file has to be stored on the filesystem as ``static/style.css``.380The file has to be stored on the filesystem as :file:`static/style.css`.381382Rendering Templates383-------------------384
13 unmodified lines
398 @app.route('/hello/<name>')399 def hello(name=None):400 return render_template('hello.html', name=name)401402Flask will look for templates in the `templates` folder. So if your402Flask will look for templates in the :file:`templates` folder. So if your403application is a module, this folder is next to that module, if it's a404package it's actually inside your package:405406**Case 1**: a module::
487 unmodified lines
modified:docs/testing.rst+1 added-1 removed
28 unmodified lines
29The Testing Skeleton30--------------------3132In 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::3435 import os36 import flaskr37 import unittest
316 unmodified lines
modified:docs/upgrading.rst+6 added-6 removed
134 unmodified lines
135136 patch -p1 < patchfile.diff1371385. 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.145146Please note that deprecation warnings are disabled by default starting147with Python 2.7. In order to see the deprecation warnings that might be
122 unmodified lines
270 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.284285If you continue to use the `Module` object which is deprecated, Flask will286restore the previous behavior as good as possible. However we strongly
96 unmodified lines
modified:examples/flaskr/README+2 added-2 removed
21 unmodified lines
22 flask --app=flaskr run2324 the application will greet you on25 http://localhost:5000/26 2627 ~ Is it tested?2829 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 lines
23 flask --app=minitwit run2425 the application will greet you on26 http://localhost:5000/27 2728 ~ Is it tested?2930 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 lines
7071 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).7576 For more information about resource loading, see :func:`open_resource`.7778 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::8081 from flask import Flask82 app = Flask(__name__)83
8 unmodified lines
92 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.9596 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::9899 app = Flask('yourapplication')100 app = Flask(__name__.split('.')[0])
1809 unmodified lines
modified:flask/helpers.py+1 added-1 removed
321 unmodified lines
322323def 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:327328 .. sourcecode:: html+jinja329330 {% macro hello(name) %}Hello {{ name }}!{% endmacro %}
573 unmodified lines