setup.rst
This file
Path
docs/tutorial/setup.rst
101 lines · 4.0 KiB
Last changed by
Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
History of this fileTake it
The walk stopped after 200 commits: lines older than da0ebb9 point at it.
Merge pull request #1760 from mattrobenolt/patch-1Markus Unterwaditzerbefore adoptionda0ebb9
lines 1–5why, one step back1.. _tutorial-setup:23Step 2: Application Setup Code4==============================5Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 6–8why, one step back6Next, we will create the application module, :file:`flaskr.py`. Just like the7:file:`schema.sql` file you created in the previous step, this file should be8placed inside of the :file:`flaskr/flaskr` folder.Merge pull request #1760 from mattrobenolt/patch-1Markus Unterwaditzerbefore adoptionda0ebb9
line 9why, one step back9Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 10–20why, one step back10For this tutorial, all the Python code we use will be put into this file11(except for one line in ``__init__.py``, and any testing or optional files you12decide to create).1314The first several lines of code in the application module are the needed import15statements. After that there will be a few lines of configuration code.1617For small applications like ``flaskr``, it is possible to drop the configuration18directly into the module. However, a cleaner solution is to create a separate19``.py`` file, load that, and import the values from there.20Merge branch '0.11-maintenance'David Lordbefore adoption633b13e
line 21why, one step back21Here are the import statements (in :file:`flaskr.py`)::Merge pull request #1760 from mattrobenolt/patch-1Markus Unterwaditzerbefore adoptionda0ebb9
lines 22–25why, one step back2223 import os24 import sqlite325Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 26–28why, one step back26 from flask import (Flask, request, session, g, redirect, url_for, abort,27 render_template, flash)28Merge branch '0.11-maintenance'David Lordbefore adoption633b13e
line 29why, one step back29The next couple lines will create the actual application instance andMerge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 30why, one step back30initialize it with the config from the same file in :file:`flaskr.py`::Merge pull request #1760 from mattrobenolt/patch-1Markus Unterwaditzerbefore adoptionda0ebb9
line 31why, one step back31Merge branch '0.11-maintenance'David Lordbefore adoption633b13e
lines 32–34why, one step back32 app = Flask(__name__) # create the application instance :)33 app.config.from_object(__name__) # load config from this file , flaskr.py34Merge pull request #1760 from mattrobenolt/patch-1Markus Unterwaditzerbefore adoptionda0ebb9
lines 35–43why, one step back35 # Load default config and override config from an environment variable36 app.config.update(dict(37 DATABASE=os.path.join(app.root_path, 'flaskr.db'),38 SECRET_KEY='development key',39 USERNAME='admin',40 PASSWORD='default'41 ))42 app.config.from_envvar('FLASKR_SETTINGS', silent=True)43Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 44–45why, one step back44In the above code, the :class:`~flask.Config` object works similarly to a45dictionary, so it can be updated with new values.Merge pull request #1760 from mattrobenolt/patch-1Markus Unterwaditzerbefore adoptionda0ebb9
lines 46–63why, one step back4647.. admonition:: Database Path4849 Operating systems know the concept of a current working directory for50 each process. Unfortunately, you cannot depend on this in web51 applications because you might have more than one application in the52 same process.5354 For this reason the ``app.root_path`` attribute can be used to55 get the path to the application. Together with the ``os.path`` module,56 files can then easily be found. In this example, we place the57 database right next to it.5859 For a real-world application, it's recommended to use60 :ref:`instance-folders` instead.6162Usually, it is a good idea to load a separate, environment-specific63configuration file. Flask allows you to import multiple configurations and itMerge branch '0.11-maintenance'David Lordbefore adoption633b13e
line 64why, one step back64will use the setting defined in the last import. This enables robustMerge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 65–66why, one step back65configuration setups. :meth:`~flask.Config.from_envvar` can help achieve66this. ::Merge pull request #1760 from mattrobenolt/patch-1Markus Unterwaditzerbefore adoptionda0ebb9
lines 67–69why, one step back6768 app.config.from_envvar('FLASKR_SETTINGS', silent=True)69Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 70–73why, one step back70If you want to do this (not required for this tutorial) simply define the71environment variable :envvar:`FLASKR_SETTINGS` that points to a config file72to be loaded. The silent switch just tells Flask to not complain if no such73environment key is set.Merge pull request #1760 from mattrobenolt/patch-1Markus Unterwaditzerbefore adoptionda0ebb9
lines 74–82why, one step back7475In addition to that, you can use the :meth:`~flask.Config.from_object`76method on the config object and provide it with an import name of a77module. Flask will then initialize the variable from that module. Note78that in all cases, only variable names that are uppercase are considered.7980The ``SECRET_KEY`` is needed to keep the client-side sessions secure.81Choose that key wisely and as hard to guess and complex as possible.82Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 83–84why, one step back83Lastly, add a method that allows for easy connections to the specified84database. ::Merge pull request #1760 from mattrobenolt/patch-1Markus Unterwaditzerbefore adoptionda0ebb9
lines 85–87why, one step back8586 def connect_db():87 """Connects to the specific database."""Merge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
line 88why, one step back88Merge pull request #1760 from mattrobenolt/patch-1Markus Unterwaditzerbefore adoptionda0ebb9
lines 89–91why, one step back89 rv = sqlite3.connect(app.config['DATABASE'])90 rv.row_factory = sqlite3.Row91 return rvMerge branch 'master' into masterKenneth Reitzbefore adoptiond911c89
lines 92–97why, one step back9293This can be used to open a connection on request and also from the94interactive Python shell or a script. This will come in handy later.95You can create a simple database connection through SQLite and then tell96it to use the :class:`sqlite3.Row` object to represent rows. This allows97the rows to be treated as if they were dictionaries instead of tuples.Merge pull request #1760 from mattrobenolt/patch-1Markus Unterwaditzerbefore adoptionda0ebb9
line 98why, one step back98Merge branch '0.11-maintenance'David Lordbefore adoption633b13e
line 99why, one step back99In the next section you will see how to run the application.Merge pull request #1760 from mattrobenolt/patch-1Markus Unterwaditzerbefore adoptionda0ebb9
line 100why, one step back100Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
line 101why, one step back101Continue with :ref:`tutorial-packaging`.