Skip to content
Myr

setup.rst

This file

Path

docs/tutorial/setup.rst

96 lines · 3.5 KiB

Last changed by

Merge pull request #808 from untitaker/issue782Armin Ronacherbefore adoption1982c20

History of this file

Take it

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

Documented config changesArmin Ronacherbefore adoptionab110d8

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

Documented config changesArmin Ronacherbefore adoptionab110d8

lines 15–27why, 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'27

Merge pull request #808 from untitaker/issue782Armin Ronacherbefore adoption1982c20

lines 28–33why, one step back
28.. admonition:: Windows
2930 If you are on Windows, replace `/tmp/flaskr.db` with a different writeable31 path of your choice, in the configuration and for the rest of this32 tutorial.33

Documented config changesArmin Ronacherbefore adoptionab110d8

line 34why, one step back
34Next we can create our actual application and initialize it with the

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

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

Documented config changesArmin Ronacherbefore adoptionab110d8

lines 36–45why, one step back
36
37 # create our little application :)38 app = Flask(__name__)39 app.config.from_object(__name__)4041:meth:`~flask.Config.from_object` will look at the given object (if it's a42string it will import it) and then look for all uppercase variables43defined there. In our case, the configuration we just wrote a few lines44of code above. You can also move that into a separate file.45

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

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

Documented config changesArmin Ronacherbefore adoptionab110d8

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

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

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

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

line 55why, one step back
55if no such environment key is set.

Documented config changesArmin Ronacherbefore adoptionab110d8

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

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

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

Documented config changesArmin Ronacherbefore adoptionab110d8

lines 61–96why, one step back
61execute code on the server!
6263We also add a method to easily connect to the database specified. That64can be used to open a connection on request and also from the interactive65Python shell or a script. This will come in handy later.6667::6869 def connect_db():70 return sqlite3.connect(app.config['DATABASE'])7172Finally we just add a line to the bottom of the file that fires up the73server if we want to run that file as a standalone application::7475 if __name__ == '__main__':76 app.run()7778With that out of the way you should be able to start up the application79without problems. Do this with the following command::8081 python flaskr.py8283You will see a message telling you that server has started along with84the address at which you can access it.8586When you head over to the server in your browser you will get an 40487page not found error because we don't have any views yet. But we will88focus on that a little later. First we should get the database working.8990.. admonition:: Externally Visible Server9192 Want your server to be publicly available? Check out the93 :ref:`externally visible server <public-server>` section for more94 information.9596Continue with :ref:`tutorial-dbinit`.