Skip to content
Myr

setup.rst

This file

Path

docs/tutorial/setup.rst

119 lines · 4.6 KiB

Last changed by

Added a missing importArmin Ronacherbefore adoption95fa9f2

History of this file

Take it

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

Removed deprecated and awkward flask.session moduleArmin Ronacherbefore adoption5e88c81

lines 1–6why, one step back
1.. _tutorial-setup:
23Step 2: Application Setup Code4==============================56Now that we have the schema in place we can create the application module.

Rewrapped linesArmin Ronacherbefore adoptionafd3c45

lines 7–12why, one step back
7Let's call it flaskr.py. We will place this file inside the flask folder.
8We will begin by adding the imports we need and by adding the config9section. For small applications, it is possible to drop the configuration10directly 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.

Removed deprecated and awkward flask.session moduleArmin Ronacherbefore adoption5e88c81

line 13why, one step back
13

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

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

Removed deprecated and awkward flask.session moduleArmin Ronacherbefore adoption5e88c81

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

Removed deprecated and awkward flask.session moduleArmin Ronacherbefore adoption5e88c81

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

Removed deprecated and awkward flask.session moduleArmin Ronacherbefore adoption5e88c81

lines 21–28why, one step back
21
22Next we can create our actual application and initialize it with the23config from the same file, in `flaskr.py`::2425 # 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–37why, one step back
32 DEBUG=True,
33 SECRET_KEY='development key',34 USERNAME='admin',35 PASSWORD='default'36 ))37 app.config.from_envvar('FLASKR_SETTINGS', silent=True)

Removed deprecated and awkward flask.session moduleArmin Ronacherbefore adoption5e88c81

line 38why, one step back
38

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

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

Switch away from /tmp for windows usersArmin Ronacherbefore adoption9074bc4

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

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 43why, one step back
43

Switch away from /tmp for windows usersArmin Ronacherbefore adoption9074bc4

lines 44–55why, one step back
44 Operating systems know the concept of a current working directory for
45 each process. Unfortunately you cannot depend on this in web46 applications because you might have more than one application in the47 same process.4849 For this reason the ``app.root_path`` attribute can be used to50 get the path to the application. Together with the ``os.path`` module51 files can then easily be found. In this example we place the52 database right next to it.5354 For a real-work application it's recommended to use55 :ref:`instance-folders` instead.

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 56why, one step back
56

Merge pull request #800 from nZac/issue#783Armin Ronacherbefore adoptionafa33e3

lines 57–61why, one step back
57Usually, it is a good idea to load a separate, environment specific
58configuration file. Flask allows you to import multiple configurations and it 59will use the setting defined in the last import. This enables robust 60configuration setups. :meth:`~flask.Config.from_envvar` can help achieve this. 61

Removed deprecated and awkward flask.session moduleArmin Ronacherbefore adoption5e88c81

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

Merge pull request #800 from nZac/issue#783Armin Ronacherbefore adoptionafa33e3

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

Removed deprecated and awkward flask.session moduleArmin Ronacherbefore adoption5e88c81

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

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

lines 68–73why, one step back
68In addition to that you can use the :meth:`~flask.Config.from_object`
69method on the config object and provide it with an import name of a70module. Flask will the initialize the variable from that module. Note71that in all cases only variable names that are uppercase are considered.7273The ``SECRET_KEY`` is needed to keep the client-side sessions secure.

Removed deprecated and awkward flask.session moduleArmin Ronacherbefore adoption5e88c81

lines 74–78why, one step back
74Choose that key wisely and as hard to guess and complex as possible. The
75debug flag enables or disables the interactive debugger. *Never leave76debug mode activated in a production system*, because it will allow users to77execute code on the server!78

Rewrapped linesArmin Ronacherbefore adoptionafd3c45

lines 79–85why, one step back
79We will also add a method that allows for easily connecting to the
80specified database. This can be used to open a connection on request and81also from the interactive Python shell or a script. This will come in82handy later. We create a simple database connection through SQLite and83then tell it to use the :class:`sqlite3.Row` object to represent rows.84This allows us to treat the rows as if they were dictionaries instead of85tuples.

Removed deprecated and awkward flask.session moduleArmin Ronacherbefore adoption5e88c81

lines 86–89why, one step back
86
87::8889 def connect_db():

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

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

Removed deprecated and awkward flask.session moduleArmin Ronacherbefore adoption5e88c81

lines 94–118why, one step back
94
95Finally we just add a line to the bottom of the file that fires up the96server if we want to run that file as a standalone application::9798 if __name__ == '__main__':99 app.run()100101With that out of the way you should be able to start up the application102without problems. Do this with the following command::103104 python flaskr.py105106You will see a message telling you that server has started along with107the address at which you can access it.108109When you head over to the server in your browser you will get an 404110page not found error because we don't have any views yet. But we will111focus on that a little later. First we should get the database working.112113.. admonition:: Externally Visible Server114115 Want your server to be publicly available? Check out the116 :ref:`externally visible server <public-server>` section for more117 information.118

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

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