Skip to content
Myr

setup.rst

This file

Path

docs/tutorial/setup.rst

118 lines · 4.6 KiB

Last changed by

Switch away from /tmp for windows usersArmin Ronacherbefore adoption9074bc4

History of this file

Take it

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

Updated documentation for the new sessionsArmin Ronacherbefore adoption261c4a6

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.

Updated documentation for the new sessionsArmin Ronacherbefore adoption261c4a6

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`::

Updated documentation for the new sessionsArmin Ronacherbefore adoption261c4a6

lines 15–17why, one step back
15
16 # all the imports17 import sqlite3

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

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

Updated documentation for the new sessionsArmin Ronacherbefore adoption261c4a6

lines 20–27why, one step back
20
21Next we can create our actual application and initialize it with the22config from the same file, in `flaskr.py`::2324 # create our little application :)25 app = Flask(__name__)26 app.config.from_object(__name__)27

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

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

Switch away from /tmp for windows usersArmin Ronacherbefore adoption9074bc4

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

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

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

Updated documentation for the new sessionsArmin Ronacherbefore adoption261c4a6

line 37why, one step back
37

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

lines 38–40why, one step back
38The :class:`~flask.Config` object works similar to a dictionary so we
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

lines 43–54why, one step back
43 Operating systems know the concept of a current working directory for
44 each process. Unfortunately you cannot depend on this in web45 applications because you might have more than one application in the46 same process.4748 For this reason the ``app.root_path`` attribute can be used to49 get the path to the application. Together with the ``os.path`` module50 files can then easily be found. In this example we place the51 database right next to it.5253 For a real-work application it's recommended to use54 :ref:`instance-folders` instead.

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

line 55why, one step back
55

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

lines 56–60why, one step back
56Usually, it is a good idea to load a separate, environment specific
57configuration 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

Updated documentation for the new sessionsArmin Ronacherbefore adoption261c4a6

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

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

lines 63–64why, one step back
63Simply 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

Updated documentation for the new sessionsArmin Ronacherbefore adoption261c4a6

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

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

lines 67–72why, one step back
67In 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 a69module. Flask will the initialize the variable from that module. Note70that in all cases only variable names that are uppercase are considered.7172The ``SECRET_KEY`` is needed to keep the client-side sessions secure.

Updated documentation for the new sessionsArmin Ronacherbefore adoption261c4a6

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

Rewrapped linesArmin Ronacherbefore adoptionafd3c45

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

Updated documentation for the new sessionsArmin Ronacherbefore adoption261c4a6

lines 85–88why, one step back
85
86::8788 def connect_db():

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

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

Updated documentation for the new sessionsArmin Ronacherbefore adoption261c4a6

lines 93–117why, one step back
93
94Finally we just add a line to the bottom of the file that fires up the95server if we want to run that file as a standalone application::9697 if __name__ == '__main__':98 app.run()99100With that out of the way you should be able to start up the application101without problems. Do this with the following command::102103 python flaskr.py104105You will see a message telling you that server has started along with106the address at which you can access it.107108When you head over to the server in your browser you will get an 404109page not found error because we don't have any views yet. But we will110focus on that a little later. First we should get the database working.111112.. admonition:: Externally Visible Server113114 Want your server to be publicly available? Check out the115 :ref:`externally visible server <public-server>` section for more116 information.117

Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3

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