Skip to content
Myr

setup.rst

This file

Path

docs/tutorial/setup.rst

101 lines · 4.0 KiB

Last changed by

Merge pull request #2399 from davidism/docs-secret-keyDavid Lordbefore adoptiond13e41d

History of this file

Take it

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

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 6–8why, one step back
6Next, we will create the application module, :file:`flaskr.py`. Just like the
7:file:`schema.sql` file you created in the previous step, this file should be8placed inside of the :file:`flaskr/flaskr` folder.

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

line 9why, one step back
9

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 10–12why, one step back
10For this tutorial, all the Python code we use will be put into this file
11(except for one line in ``__init__.py``, and any testing or optional files you12decide to create).

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

line 13why, one step back
13

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 14–22why, one step back
14The first several lines of code in the application module are the needed import
15statements. After that there will be a few lines of configuration code.1617For small applications like ``flaskr``, it is possible to drop the configuration18directly into the module. However, a cleaner solution is to create a separate19``.py`` file, load that, and import the values from there.2021Here are the import statements (in :file:`flaskr.py`)::22

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

lines 23–25why, one step back
23 import os
24 import sqlite325

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 26–27why, one step back
26 from flask import (Flask, request, session, g, redirect, url_for, abort,
27 render_template, flash)

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

line 28why, one step back
28

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

line 31why, one step back
31

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 32–34why, one step back
32 app = Flask(__name__) # create the application instance :)
33 app.config.from_object(__name__) # load config from this file , flaskr.py34

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

line 35why, one step back
35 # Load default config and override config from an environment variable

Merge pull request #2399 from davidism/docs-secret-keyDavid Lordbefore adoptiond13e41d

line 36why, one step back
36 app.config.update(

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

line 37why, one step back
37 DATABASE=os.path.join(app.root_path, 'flaskr.db'),

Merge pull request #2399 from davidism/docs-secret-keyDavid Lordbefore adoptiond13e41d

line 38why, one step back
38 SECRET_KEY=b'_5#y2L"F4Q8z\n\xec]/',

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

lines 39–40why, one step back
39 USERNAME='admin',
40 PASSWORD='default'

Merge pull request #2399 from davidism/docs-secret-keyDavid Lordbefore adoptiond13e41d

line 41why, one step back
41 )

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 44–45why, one step back
44In the above code, the :class:`~flask.Config` object works similarly to a
45dictionary, so it can be updated with new values.

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

lines 46–49why, one step back
46
47.. admonition:: Database Path4849 Operating systems know the concept of a current working directory for

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

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

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

lines 51–54why, one step back
51 applications because you might have more than one application in the
52 same process.5354 For this reason the ``app.root_path`` attribute can be used to

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

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

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

lines 57–58why, one step back
57 database right next to it.
58

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

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

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

lines 60–61why, one step back
60 :ref:`instance-folders` instead.
61

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

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

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

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 64–66why, one step back
64will use the setting defined in the last import. This enables robust
65configuration setups. :meth:`~flask.Config.from_envvar` can help achieve66this. ::

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

line 67why, one step back
67

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

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 70–73why, one step back
70If you want to do this (not required for this tutorial) simply define the
71environment variable :envvar:`FLASKR_SETTINGS` that points to a config file72to be loaded. The silent switch just tells Flask to not complain if no such73environment key is set.

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

line 74why, one step back
74

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

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

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

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

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

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

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

line 79why, one step back
79

Merge pull request #2399 from davidism/docs-secret-keyDavid Lordbefore adoptiond13e41d

line 80why, one step back
80The :data:`SECRET_KEY` is needed to keep the client-side sessions secure.

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

lines 81–82why, one step back
81Choose that key wisely and as hard to guess and complex as possible.
82

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 83–84why, one step back
83Lastly, add a method that allows for easy connections to the specified
84database. ::

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

lines 85–87why, one step back
85
86 def connect_db():87 """Connects to the specific database."""

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 88why, one step back
88

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

lines 89–92why, one step back
89 rv = sqlite3.connect(app.config['DATABASE'])
90 rv.row_factory = sqlite3.Row91 return rv92

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 93–97why, one step back
93This can be used to open a connection on request and also from the
94interactive Python shell or a script. This will come in handy later.95You can create a simple database connection through SQLite and then tell96it to use the :class:`sqlite3.Row` object to represent rows. This allows97the rows to be treated as if they were dictionaries instead of tuples.

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

line 98why, one step back
98

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

Updated intersphinxArmin Ronacherbefore adoptiond1821b3

line 100why, one step back
100

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 101why, one step back
101Continue with :ref:`tutorial-packaging`.