setup.rst
This file
Path
docs/tutorial/setup.rst
113 lines · 4.4 KiB
Last changed by
Merge pull request #1054 from lord63/masterKenneth Reitzbefore adoption8070670
History of this fileTake 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–6why, one step back1.. _tutorial-setup:23Step 2: Application Setup Code4==============================56Now that we have the schema in place we can create the application module.Merge pull request #1054 from lord63/masterKenneth Reitzbefore adoption8070670
line 7why, one step back7Let's call it flaskr.py. We will place this file inside the flaskr folder.Rewrapped linesArmin Ronacherbefore adoptionafd3c45
lines 8–12why, one step back8We 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.Set release date for 0.10.1Armin Ronacherbefore adoptione562acf
line 13why, one step back13Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
line 14why, one step back14First we add the imports in `flaskr.py`::Set release date for 0.10.1Armin Ronacherbefore adoptione562acf
lines 15–16why, one step back1516 # all the importsAdded a missing importArmin Ronacherbefore adoption95fa9f2
line 17why, one step back17 import osSet release date for 0.10.1Armin Ronacherbefore adoptione562acf
line 18why, one step back18 import sqlite3Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 19–20why, one step back19 from flask import Flask, request, session, g, redirect, url_for, abort, \20 render_template, flashSet release date for 0.10.1Armin Ronacherbefore adoptione562acf
lines 21–28why, one step back2122Next 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__)28Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 29–30why, one step back29 # Load default config and override config from an environment variable30 app.config.update(dict(Switch away from /tmp for windows usersArmin Ronacherbefore adoption9074bc4
line 31why, one step back31 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 back32 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 back37Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 38–40why, one step back38The :class:`~flask.Config` object works similar to a dictionary so we39can update it with new values.40Switch away from /tmp for windows usersArmin Ronacherbefore adoption9074bc4
line 41why, one step back41.. admonition:: Database PathRewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
line 42why, one step back42Switch away from /tmp for windows usersArmin Ronacherbefore adoption9074bc4
lines 43–54why, one step back43 Operating systems know the concept of a current working directory for44 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 back55Merge pull request #800 from nZac/issue#783Armin Ronacherbefore adoptionafa33e3
lines 56–60why, one step back56Usually, it is a good idea to load a separate, environment specific57configuration 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 Set release date for 0.10.1Armin Ronacherbefore adoptione562acf
lines 61–62why, one step back61 app.config.from_envvar('FLASKR_SETTINGS', silent=True)62Merge pull request #800 from nZac/issue#783Armin Ronacherbefore adoptionafa33e3
lines 63–64why, one step back63Simply 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 Set release date for 0.10.1Armin Ronacherbefore adoptione562acf
lines 65–66why, one step back65if no such environment key is set.66Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 67–68why, one step back67In 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 aMerge pull request #1054 from lord63/masterKenneth Reitzbefore adoption8070670
line 69why, one step back69module. Flask will then initialize the variable from that module. NoteRewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 70–72why, one step back70that in all cases only variable names that are uppercase are considered.7172The ``SECRET_KEY`` is needed to keep the client-side sessions secure.Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075
line 73why, one step back73Choose that key wisely and as hard to guess and complex as possible.Set release date for 0.10.1Armin Ronacherbefore adoptione562acf
line 74why, one step back74Rewrapped linesArmin Ronacherbefore adoptionafd3c45
lines 75–81why, one step back75We will also add a method that allows for easily connecting to the76specified database. This can be used to open a connection on request and77also from the interactive Python shell or a script. This will come in78handy later. We create a simple database connection through SQLite and79then tell it to use the :class:`sqlite3.Row` object to represent rows.80This allows us to treat the rows as if they were dictionaries instead of81tuples.Set release date for 0.10.1Armin Ronacherbefore adoptione562acf
lines 82–85why, one step back8283::8485 def connect_db():Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 86–89why, one step back86 """Connects to the specific database."""87 rv = sqlite3.connect(app.config['DATABASE'])88 rv.row_factory = sqlite3.Row89 return rvSet release date for 0.10.1Armin Ronacherbefore adoptione562acf
lines 90–93why, one step back9091With that out of the way you should be able to start up the application92without problems. Do this with the following command::93Updated tutorial to the flask scriptArmin Ronacherbefore adoptiona3a5075
lines 94–98why, one step back94 flask --app=flaskr --debug run9596The ``--debug`` flag enables or disables the interactive debugger. *Never97leave debug mode activated in a production system*, because it will allow98users to execute code on the server!Set release date for 0.10.1Armin Ronacherbefore adoptione562acf
lines 99–112why, one step back99100You will see a message telling you that server has started along with101the address at which you can access it.102103When you head over to the server in your browser you will get an 404104page not found error because we don't have any views yet. But we will105focus on that a little later. First we should get the database working.106107.. admonition:: Externally Visible Server108109 Want your server to be publicly available? Check out the110 :ref:`externally visible server <public-server>` section for more111 information.112Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
line 113why, one step back113Continue with :ref:`tutorial-dbcon`.