Skip to content
Myr

setup.rst

This file

Path

docs/tutorial/setup.rst

90 lines · 3.3 KiB

Last changed by

Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption3249eeb

History of this file

Take it

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

Fall back to imports w/exotic pkg loaders, #380.Ron DuPlainbefore adoption1f20a11

lines 1–13why, 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.7Let's call it `flaskr.py` inside the `flaskr` folder. For starters we8will add the imports we will need as well as the config section. For9small applications it's a possibility to drop the configuration directly10into the module which we will be doing here. However a cleaner solution11would be to create a separate `.ini` or `.py` file and load that or import12the values from there.13

Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption3249eeb

line 14why, one step back
14In `flaskr.py`::

Fall back to imports w/exotic pkg loaders, #380.Ron DuPlainbefore adoption1f20a11

lines 15–28why, one step back
15
16 # all the imports17 import sqlite318 from flask import Flask, request, session, g, redirect, url_for, \19 abort, render_template, flash2021 # configuration22 DATABASE = '/tmp/flaskr.db'23 DEBUG = True24 SECRET_KEY = 'development key'25 USERNAME = 'admin'26 PASSWORD = 'default'2728Next we can create our actual application and initialize it with the

Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption3249eeb

line 29why, one step back
29config from the same file, in `flaskr.py`::

Fall back to imports w/exotic pkg loaders, #380.Ron DuPlainbefore adoption1f20a11

lines 30–39why, one step back
30
31 # create our little application :)32 app = Flask(__name__)33 app.config.from_object(__name__)3435:meth:`~flask.Config.from_object` will look at the given object (if it's a36string it will import it) and then look for all uppercase variables37defined there. In our case, the configuration we just wrote a few lines38of code above. You can also move that into a separate file.39

Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption3249eeb

lines 40–42why, one step back
40Usually, it is a good idea to load a configuration from a configurable
41file. This is what :meth:`~flask.Config.from_envvar` can do, replacing the42:meth:`~flask.Config.from_object` line above::

Fall back to imports w/exotic pkg loaders, #380.Ron DuPlainbefore adoption1f20a11

lines 43–46why, one step back
43
44 app.config.from_envvar('FLASKR_SETTINGS', silent=True)4546That way someone can set an environment variable called

Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption3249eeb

lines 47–49why, one step back
47:envvar:`FLASKR_SETTINGS` to specify a config file to be loaded which will then
48override the default values. The silent switch just tells Flask to not complain49if no such environment key is set.

Fall back to imports w/exotic pkg loaders, #380.Ron DuPlainbefore adoption1f20a11

lines 50–52why, one step back
50
51The `secret_key` is needed to keep the client-side sessions secure.52Choose that key wisely and as hard to guess and complex as possible. The

Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption3249eeb

lines 53–54why, one step back
53debug flag enables or disables the interactive debugger. *Never leave
54debug mode activated in a production system*, because it will allow users to

Fall back to imports w/exotic pkg loaders, #380.Ron DuPlainbefore adoption1f20a11

lines 55–90why, one step back
55execute code on the server!
5657We also add a method to easily connect to the database specified. That58can be used to open a connection on request and also from the interactive59Python shell or a script. This will come in handy later.6061::6263 def connect_db():64 return sqlite3.connect(app.config['DATABASE'])6566Finally we just add a line to the bottom of the file that fires up the67server if we want to run that file as a standalone application::6869 if __name__ == '__main__':70 app.run()7172With that out of the way you should be able to start up the application73without problems. Do this with the following command::7475 python flaskr.py7677You will see a message telling you that server has started along with78the address at which you can access it.7980When you head over to the server in your browser you will get an 40481page not found error because we don't have any views yet. But we will82focus on that a little later. First we should get the database working.8384.. admonition:: Externally Visible Server8586 Want your server to be publicly available? Check out the87 :ref:`externally visible server <public-server>` section for more88 information.8990Continue with :ref:`tutorial-dbinit`.