Skip to content
Myr

setup.rst

This file

Path

docs/tutorial/setup.rst

115 lines · 4.5 KiB

Last changed by

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

History of this file

Take it

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

Set release date for 0.10.1Armin Ronacherbefore adoptione562acf

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

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

lines 6–8why, one step back
6Now that we have the schema in place, we can create the application module.
7Let's call it ``flaskr.py``. We will place this file inside the ``flaskr``8folder. We will begin by adding the imports we need and by adding the config

Rewrapped linesArmin Ronacherbefore adoptionafd3c45

line 9why, one step back
9section. For small applications, it is possible to drop the configuration

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

lines 10–12why, one step back
10directly into the module, and this is what we will be doing here. However,
11a cleaner solution would be to create a separate ``.ini`` or ``.py`` file,12load that, and import the values from there.

Set release date for 0.10.1Armin Ronacherbefore adoptione562acf

line 13why, one step back
13

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

line 14why, one step back
14First, we add the imports in :file:`flaskr.py`::

Set release date for 0.10.1Armin Ronacherbefore adoptione562acf

lines 15–16why, one step back
15
16 # all the imports

Added a missing importArmin Ronacherbefore adoption95fa9f2

line 17why, one step back
17 import os

Set release date for 0.10.1Armin Ronacherbefore adoptione562acf

line 18why, one step back
18 import sqlite3

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

lines 19–20why, one step back
19 from flask import Flask, request, session, g, redirect, url_for, abort, \
20 render_template, flash

Set release date for 0.10.1Armin Ronacherbefore adoptione562acf

line 21why, one step back
21

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

lines 22–23why, one step back
22Next, we can create our actual application and initialize it with the
23config from the same file in :file:`flaskr.py`::

Set release date for 0.10.1Armin Ronacherbefore adoptione562acf

lines 24–28why, one step back
24
25 # create our little application :)26 app = Flask(__name__)27 app.config.from_object(__name__)28

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

lines 29–30why, one step back
29 # Load default config and override config from an environment variable
30 app.config.update(dict(

Switch away from /tmp for windows usersArmin Ronacherbefore adoption9074bc4

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

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

lines 32–36why, one step back
32 SECRET_KEY='development key',
33 USERNAME='admin',34 PASSWORD='default'35 ))36 app.config.from_envvar('FLASKR_SETTINGS', silent=True)

Set release date for 0.10.1Armin Ronacherbefore adoptione562acf

line 37why, one step back
37

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

line 38why, one step back
38The :class:`~flask.Config` object works similarly to a dictionary so we

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

lines 39–40why, one step back
39can update it with new values.
40

Switch away from /tmp for windows usersArmin Ronacherbefore adoption9074bc4

line 41why, one step back
41.. admonition:: Database Path

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 42why, one step back
42

Switch away from /tmp for windows usersArmin Ronacherbefore adoption9074bc4

line 43why, one step back
43 Operating systems know the concept of a current working directory for

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

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

Switch away from /tmp for windows usersArmin Ronacherbefore adoption9074bc4

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

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

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

Switch away from /tmp for windows usersArmin Ronacherbefore adoption9074bc4

lines 51–52why, one step back
51 database right next to it.
52

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

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

Switch away from /tmp for windows usersArmin Ronacherbefore adoption9074bc4

line 54why, one step back
54 :ref:`instance-folders` instead.

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 55why, one step back
55

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

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

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

lines 57–60why, one step back
57configuration file. Flask allows you to import multiple configurations and it
58will use the setting defined in the last import. This enables robust59configuration setups. :meth:`~flask.Config.from_envvar` can help achieve this.60

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

line 61why, one step back
61.. code-block:: python

Set release date for 0.10.1Armin Ronacherbefore adoptione562acf

line 62why, one step back
62

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

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

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

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

Set release date for 0.10.1Armin Ronacherbefore adoptione562acf

lines 67–68why, one step back
67if no such environment key is set.
68

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

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

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 70why, one step back
70method on the config object and provide it with an import name of a

Merge pull request #1054 from lord63/masterKenneth Reitzbefore adoption8070670

line 71why, one step back
71module. Flask will then initialize the variable from that module. Note

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

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

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

lines 73–74why, one step back
73
74The ``SECRET_KEY`` is needed to keep the client-side sessions secure.

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

line 75why, one step back
75Choose that key wisely and as hard to guess and complex as possible.

Set release date for 0.10.1Armin Ronacherbefore adoptione562acf

line 76why, one step back
76

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

lines 77–78why, one step back
77We will also add a method that allows for easy connections to the
78specified database. This can be used to open a connection on request and

Rewrapped linesArmin Ronacherbefore adoptionafd3c45

lines 79–83why, one step back
79also from the interactive Python shell or a script. This will come in
80handy later. We create a simple database connection through SQLite and81then tell it to use the :class:`sqlite3.Row` object to represent rows.82This allows us to treat the rows as if they were dictionaries instead of83tuples.

Set release date for 0.10.1Armin Ronacherbefore adoptione562acf

lines 84–87why, one step back
84
85::8687 def connect_db():

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

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

Set release date for 0.10.1Armin Ronacherbefore adoptione562acf

line 92why, one step back
92

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

line 93why, one step back
93With that out of the way, you should be able to start up the application

Set release date for 0.10.1Armin Ronacherbefore adoptione562acf

lines 94–95why, one step back
94without problems. Do this with the following command::
95

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

lines 96–97why, one step back
96 flask --app=flaskr --debug run
97

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

line 98why, one step back
98The :option:`--debug` flag enables or disables the interactive debugger. *Never

Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075

lines 99–100why, one step back
99leave debug mode activated in a production system*, because it will allow
100users to execute code on the server!

Set release date for 0.10.1Armin Ronacherbefore adoptione562acf

lines 101–104why, one step back
101
102You will see a message telling you that server has started along with103the address at which you can access it.104

Grammatical fixes in Foreword and TutorialKevin Yapbefore adoption23fc2e5

lines 105–107why, one step back
105When you head over to the server in your browser, you will get a 404 error
106because we don't have any views yet. We will focus on that a little later,107but first, we should get the database working.

Set release date for 0.10.1Armin Ronacherbefore adoptione562acf

lines 108–114why, one step back
108
109.. admonition:: Externally Visible Server110111 Want your server to be publicly available? Check out the112 :ref:`externally visible server <public-server>` section for more113 information.114

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 115why, one step back
115Continue with :ref:`tutorial-dbcon`.