setup.rst
This file
Path
docs/tutorial/setup.rst
97 lines · 3.9 KiB
Last changed by
Merge branch '0.11-maintenance'David Lordbefore adoption633b13e
History of this fileTake it
1.. _tutorial-setup:23Step 2: Application Setup Code4==============================56Now that the schema is in place, you can create the application module,7:file:`flaskr.py`. This file should be placed inside of the8:file:`flaskr/flaskr` folder. The first several lines of code in the9application module are the needed import statements. After that there will be a10few lines of configuration code. For small applications like ``flaskr``, it is11possible to drop the configuration directly into the module. However, a cleaner12solution is to create a separate ``.ini`` or ``.py`` file, load that, and13import the values from there.1415Here are the import statements (in :file:`flaskr.py`)::1617 # all the imports18 import os19 import sqlite320 from flask import Flask, request, session, g, redirect, url_for, abort, \21 render_template, flash2223The next couple lines will create the actual application instance and24initialize it with the config from the same file in :file:`flaskr.py`:2526.. sourcecode:: python2728 app = Flask(__name__) # create the application instance :)29 app.config.from_object(__name__) # load config from this file , flaskr.py3031 # Load default config and override config from an environment variable32 app.config.update(dict(33 DATABASE=os.path.join(app.root_path, 'flaskr.db'),34 SECRET_KEY='development key',35 USERNAME='admin',36 PASSWORD='default'37 ))38 app.config.from_envvar('FLASKR_SETTINGS', silent=True)3940The :class:`~flask.Config` object works similarly to a dictionary, so it can be41updated with new values.4243.. admonition:: Database Path4445 Operating systems know the concept of a current working directory for46 each process. Unfortunately, you cannot depend on this in web47 applications because you might have more than one application in the48 same process.4950 For this reason the ``app.root_path`` attribute can be used to51 get the path to the application. Together with the ``os.path`` module,52 files can then easily be found. In this example, we place the53 database right next to it.5455 For a real-world application, it's recommended to use56 :ref:`instance-folders` instead.5758Usually, it is a good idea to load a separate, environment-specific59configuration file. Flask allows you to import multiple configurations and it60will use the setting defined in the last import. This enables robust61configuration setups. :meth:`~flask.Config.from_envvar` can help achieve this.6263.. sourcecode:: python6465 app.config.from_envvar('FLASKR_SETTINGS', silent=True)6667Simply define the environment variable :envvar:`FLASKR_SETTINGS` that points to68a config file to be loaded. The silent switch just tells Flask to not complain69if no such environment key is set.7071In addition to that, you can use the :meth:`~flask.Config.from_object`72method on the config object and provide it with an import name of a73module. Flask will then initialize the variable from that module. Note74that in all cases, only variable names that are uppercase are considered.7576The ``SECRET_KEY`` is needed to keep the client-side sessions secure.77Choose that key wisely and as hard to guess and complex as possible.7879Lastly, you will add a method that allows for easy connections to the80specified database. This can be used to open a connection on request and81also from the interactive Python shell or a script. This will come in82handy later. You can create a simple database connection through SQLite and83then tell it to use the :class:`sqlite3.Row` object to represent rows.84This allows the rows to be treated as if they were dictionaries instead of85tuples.8687.. sourcecode:: python8889 def connect_db():90 """Connects to the specific database."""91 rv = sqlite3.connect(app.config['DATABASE'])92 rv.row_factory = sqlite3.Row93 return rv9495In the next section you will see how to run the application.9697Continue with :ref:`tutorial-setuptools`.