Skip to content
Myr

setup.rst

This file

Path

docs/tutorial/setup.rst

97 lines · 3.9 KiB

Last changed by

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

History of this file

Take it

The walk stopped after 200 commits: lines older than a25ee7e point at it.

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 1–5why, one step back
1.. _tutorial-setup:
23Step 2: Application Setup Code4==============================5

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

lines 6–13why, one step back
6Now that the schema is in place, you can create the application module,
7:file:`flaskr.py`. This file should be placed inside of the8:file:`flaskr/flaskr` folder. The first several lines of code in the9application module are the needed import statements. After that there will be a10few lines of configuration code. For small applications like ``flaskr``, it is11possible to drop the configuration directly into the module. However, a cleaner12solution is to create a separate ``.ini`` or ``.py`` file, load that, and13import the values from there.

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

line 14why, one step back
14

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

line 15why, one step back
15Here are the import statements (in :file:`flaskr.py`)::

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 16–22why, one step back
16
17 # all the imports18 import os19 import sqlite320 from flask import Flask, request, session, g, redirect, url_for, abort, \21 render_template, flash22

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

lines 23–24why, one step back
23The next couple lines will create the actual application instance and
24initialize it with the config from the same file in :file:`flaskr.py`:

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

line 25why, one step back
25

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

line 26why, one step back
26.. sourcecode:: python

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

line 27why, one step back
27

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

lines 28–30why, one step back
28 app = Flask(__name__) # create the application instance :)
29 app.config.from_object(__name__) # load config from this file , flaskr.py30

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 31–39why, one step back
31 # Load default config and override config from an environment variable
32 app.config.update(dict(33 DATABASE=os.path.join(app.root_path, 'flaskr.db'),34 SECRET_KEY='development key',35 USERNAME='admin',36 PASSWORD='default'37 ))38 app.config.from_envvar('FLASKR_SETTINGS', silent=True)39

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

lines 40–41why, one step back
40The :class:`~flask.Config` object works similarly to a dictionary, so it can be
41updated with new values.

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 42–45why, one step back
42
43.. admonition:: Database Path4445 Operating systems know the concept of a current working directory for

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

line 46why, one step back
46 each process. Unfortunately, you cannot depend on this in web

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 47–50why, one step back
47 applications because you might have more than one application in the
48 same process.4950 For this reason the ``app.root_path`` attribute can be used to

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

lines 51–52why, one step back
51 get the path to the application. Together with the ``os.path`` module,
52 files can then easily be found. In this example, we place the

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 53–54why, one step back
53 database right next to it.
54

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

line 55why, one step back
55 For a real-world application, it's recommended to use

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 56–57why, one step back
56 :ref:`instance-folders` instead.
57

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

line 58why, one step back
58Usually, it is a good idea to load a separate, environment-specific

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 59why, one step back
59configuration file. Flask allows you to import multiple configurations and it

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

line 60why, one step back
60will use the setting defined in the last import. This enables robust

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

lines 61–62why, one step back
61configuration setups. :meth:`~flask.Config.from_envvar` can help achieve this.
62

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

line 63why, one step back
63.. sourcecode:: python

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

line 64why, one step back
64

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

lines 65–66why, one step back
65 app.config.from_envvar('FLASKR_SETTINGS', silent=True)
66

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

lines 67–68why, one step back
67Simply define the environment variable :envvar:`FLASKR_SETTINGS` that points to
68a config file to be loaded. The silent switch just tells Flask to not complain

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 69–70why, one step back
69if no such environment key is set.
70

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

line 71why, one step back
71In addition to that, you can use the :meth:`~flask.Config.from_object`

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 72–73why, one step back
72method on the config object and provide it with an import name of a
73module. Flask will then initialize the variable from that module. Note

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

line 74why, one step back
74that in all cases, only variable names that are uppercase are considered.

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 75–78why, one step back
75
76The ``SECRET_KEY`` is needed to keep the client-side sessions secure.77Choose that key wisely and as hard to guess and complex as possible.78

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

lines 79–80why, one step back
79Lastly, you will add a method that allows for easy connections to the
80specified database. This can be used to open a connection on request and

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

line 81why, one step back
81also from the interactive Python shell or a script. This will come in

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

line 82why, one step back
82handy later. You can create a simple database connection through SQLite and

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

line 83why, one step back
83then tell it to use the :class:`sqlite3.Row` object to represent rows.

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

line 84why, one step back
84This allows the rows to be treated as if they were dictionaries instead of

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 85–86why, one step back
85tuples.
86

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

line 87why, one step back
87.. sourcecode:: python

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

lines 88–94why, one step back
88
89 def connect_db():90 """Connects to the specific database."""91 rv = sqlite3.connect(app.config['DATABASE'])92 rv.row_factory = sqlite3.Row93 return rv94

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

line 95why, one step back
95In the next section you will see how to run the application.

Merge pull request #1186 from llimllib/masterMarkus Unterwaditzerbefore adoptiona25ee7e

line 96why, one step back
96

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

line 97why, one step back
97Continue with :ref:`tutorial-setuptools`.