setup.rst
This file
Path
docs/tutorial/setup.rst
109 lines · 4.2 KiB
Last changed by
Rewrapped linesArmin Ronacherbefore adoptionafd3c45
History of this fileTake it
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. 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.1314First we add the imports in `flaskr.py`::1516 # all the imports17 import sqlite318 from flask import Flask, request, session, g, redirect, url_for, abort, \19 render_template, flash2021Next 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__)2728 # 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)3738The :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.4647Usually, 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 52 app.config.from_envvar('FLASKR_SETTINGS', silent=True)5354Simply 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 56if no such environment key is set.5758In 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.64Choose that key wisely and as hard to guess and complex as possible. The65debug flag enables or disables the interactive debugger. *Never leave66debug mode activated in a production system*, because it will allow users to67execute code on the server!6869We will also add a method that allows for easily connecting to the70specified database. This can be used to open a connection on request and71also from the interactive Python shell or a script. This will come in72handy later. We create a simple database connection through SQLite and73then tell it to use the :class:`sqlite3.Row` object to represent rows.74This allows us to treat the rows as if they were dictionaries instead of75tuples.7677::7879 def connect_db():80 """Connects to the specific database."""81 rv = sqlite3.connect(app.config['DATABASE'])82 rv.row_factory = sqlite3.Row83 return rv8485Finally we just add a line to the bottom of the file that fires up the86server if we want to run that file as a standalone application::8788 if __name__ == '__main__':89 app.run()9091With that out of the way you should be able to start up the application92without problems. Do this with the following command::9394 python flaskr.py9596You will see a message telling you that server has started along with97the address at which you can access it.9899When you head over to the server in your browser you will get an 404100page not found error because we don't have any views yet. But we will101focus on that a little later. First we should get the database working.102103.. admonition:: Externally Visible Server104105 Want your server to be publicly available? Check out the106 :ref:`externally visible server <public-server>` section for more107 information.108109Continue with :ref:`tutorial-dbcon`.