setup.rst
This file
Path
docs/tutorial/setup.rst
108 lines · 4.1 KiB
Last changed by
Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
History of this fileTake it
The walk stopped after 200 commits: lines older than ab110d8 point at it.
Documented config changesArmin Ronacherbefore adoptionab110d8
lines 1–7why, one step back1.. _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 weRewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 8–12why, one step back8will add the imports and create the application object. For small9applications it's a possibility to drop the configuration directly into10the module which we will be doing here. However a cleaner solution would11be to create a separate `.ini` or `.py` file and load that or import the12values from there.Documented config changesArmin Ronacherbefore adoptionab110d8
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`::Documented config changesArmin Ronacherbefore adoptionab110d8
lines 15–17why, one step back1516 # all the imports17 import sqlite3Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 18–19why, one step back18 from flask import Flask, request, session, g, redirect, url_for, abort, \19 render_template, flashDocumented config changesArmin Ronacherbefore adoptionab110d8
lines 20–21why, one step back2021Next we can create our actual application and initialize it with theMerge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption3249eeb
line 22why, one step back22config from the same file, in `flaskr.py`::Documented config changesArmin Ronacherbefore adoptionab110d8
lines 23–27why, one step back2324 # create our little application :)25 app = Flask(__name__)26 app.config.from_object(__name__)27Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 28–36why, one step back28 # Load default config and override config from an environment variable29 app.config.update(dict(30 DATABASE='/tmp/flaskr.db',31 DEBUG=True,32 SECRET_KEY='development key',33 USERNAME='admin',34 PASSWORD='default'35 ))36 app.config.from_envvar('FLASKR_SETTINGS', silent=True)Documented config changesArmin Ronacherbefore adoptionab110d8
line 37why, one step back37Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 38–46why, one step back38The :class:`~flask.Config` object works similar to a dictionary so we39can update it with new values.4041.. admonition:: Windows4243 If you are on Windows, replace `/tmp/flaskr.db` with a different writeable44 path of your choice, in the configuration and for the rest of this45 tutorial.46Merge pull request #800 from nZac/issue#783Armin Ronacherbefore adoptionafa33e3
lines 47–51why, one step back47Usually, it is a good idea to load a separate, environment specific48configuration file. Flask allows you to import multiple configurations and it 49will use the setting defined in the last import. This enables robust 50configuration setups. :meth:`~flask.Config.from_envvar` can help achieve this. 51 Documented config changesArmin Ronacherbefore adoptionab110d8
lines 52–53why, one step back52 app.config.from_envvar('FLASKR_SETTINGS', silent=True)53Merge pull request #800 from nZac/issue#783Armin Ronacherbefore adoptionafa33e3
lines 54–55why, one step back54Simply define the environment variable :envvar:`FLASKR_SETTINGS` that points to 55a 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 56why, one step back56if no such environment key is set.Documented config changesArmin Ronacherbefore adoptionab110d8
line 57why, one step back57Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 58–63why, one step back58In addition to that you can use the :meth:`~flask.Config.from_object`59method on the config object and provide it with an import name of a60module. Flask will the initialize the variable from that module. Note61that in all cases only variable names that are uppercase are considered.6263The ``SECRET_KEY`` is needed to keep the client-side sessions secure.Documented config changesArmin Ronacherbefore adoptionab110d8
line 64why, one step back64Choose that key wisely and as hard to guess and complex as possible. TheMerge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption3249eeb
lines 65–66why, one step back65debug flag enables or disables the interactive debugger. *Never leave66debug mode activated in a production system*, because it will allow users toDocumented config changesArmin Ronacherbefore adoptionab110d8
lines 67–70why, one step back67execute code on the server!6869We also add a method to easily connect to the database specified. That70can be used to open a connection on request and also from the interactiveRewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 71–74why, one step back71Python shell or a script. This will come in handy later. We create a72simple database connection through SQLite and then tell it to use the73:class:`sqlite3.Row` object to represent rows. This allows us to treat74the rows as if they were dictionaries instead of tuples.Documented config changesArmin Ronacherbefore adoptionab110d8
lines 75–78why, one step back7576::7778 def connect_db():Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
lines 79–82why, one step back79 """Connects to the specific database."""80 rv = sqlite3.connect(app.config['DATABASE'])81 rv.row_factory = sqlite3.Row82 return rvDocumented config changesArmin Ronacherbefore adoptionab110d8
lines 83–107why, one step back8384Finally we just add a line to the bottom of the file that fires up the85server if we want to run that file as a standalone application::8687 if __name__ == '__main__':88 app.run()8990With that out of the way you should be able to start up the application91without problems. Do this with the following command::9293 python flaskr.py9495You will see a message telling you that server has started along with96the address at which you can access it.9798When you head over to the server in your browser you will get an 40499page not found error because we don't have any views yet. But we will100focus on that a little later. First we should get the database working.101102.. admonition:: Externally Visible Server103104 Want your server to be publicly available? Check out the105 :ref:`externally visible server <public-server>` section for more106 information.107Rewrote tutorial to use the g based appcontext objectArmin Ronacherbefore adoption05161d3
line 108why, one step back108Continue with :ref:`tutorial-dbcon`.