setup.rst
This file
Path
docs/tutorial/setup.rst
118 lines · 4.6 KiB
Last changed by
Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
History of this fileTake it
The walk stopped after 200 commits: lines older than eca5306 point at it.
Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
lines 1–5why, one step back1.. _tutorial-setup:23Step 2: Application Setup Code4==============================5Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
lines 6–8why, one step back6Now 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 ``flaskr``8folder. We will begin by adding the imports we need and by adding the configMerge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
line 9why, one step back9section. For small applications, it is possible to drop the configurationMerge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
lines 10–12why, one step back10directly into the module, and this is what we will be doing here. However,11a cleaner solution would be to create a separate ``.ini`` or ``.py`` file,12load that, and import the values from there.Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
line 13why, one step back13Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
line 14why, one step back14First, we add the imports in :file:`flaskr.py`::Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
lines 15–21why, one step back1516 # all the imports17 import os18 import sqlite319 from flask import Flask, request, session, g, redirect, url_for, abort, \20 render_template, flash21Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
lines 22–23why, one step back22Next, we can create our actual application and initialize it with the23config from the same file in :file:`flaskr.py`::Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
lines 24–37why, one step back2425 # create our little application :)26 app = Flask(__name__)27 app.config.from_object(__name__)2829 # Load default config and override config from an environment variable30 app.config.update(dict(31 DATABASE=os.path.join(app.root_path, 'flaskr.db'),32 SECRET_KEY='development key',33 USERNAME='admin',34 PASSWORD='default'35 ))36 app.config.from_envvar('FLASKR_SETTINGS', silent=True)37Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
line 38why, one step back38The :class:`~flask.Config` object works similarly to a dictionary so weMerge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
lines 39–43why, one step back39can update it with new values.4041.. admonition:: Database Path4243 Operating systems know the concept of a current working directory forMerge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
line 44why, one step back44 each process. Unfortunately, you cannot depend on this in webMerge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
lines 45–48why, one step back45 applications because you might have more than one application in the46 same process.4748 For this reason the ``app.root_path`` attribute can be used toMerge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
lines 49–50why, one step back49 get the path to the application. Together with the ``os.path`` module,50 files can then easily be found. In this example, we place theMerge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
lines 51–52why, one step back51 database right next to it.52Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
line 53why, one step back53 For a real-world application, it's recommended to useMerge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
lines 54–55why, one step back54 :ref:`instance-folders` instead.55Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
line 56why, one step back56Usually, it is a good idea to load a separate, environment-specificMerge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
lines 57–60why, one step back57configuration file. Flask allows you to import multiple configurations and it58will use the setting defined in the last import. This enables robust59configuration setups. :meth:`~flask.Config.from_envvar` can help achieve this.60Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
line 61why, one step back61.. code-block:: pythonMerge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
line 62why, one step back62Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
lines 63–64why, one step back63 app.config.from_envvar('FLASKR_SETTINGS', silent=True)64Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
lines 65–68why, one step back65Simply define the environment variable :envvar:`FLASKR_SETTINGS` that points to66a config file to be loaded. The silent switch just tells Flask to not complain67if no such environment key is set.68Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
line 69why, one step back69In addition to that, you can use the :meth:`~flask.Config.from_object`Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
lines 70–71why, one step back70method on the config object and provide it with an import name of a71module. Flask will then initialize the variable from that module. NoteMerge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
line 72why, one step back72that in all cases, only variable names that are uppercase are considered.Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
lines 73–76why, one step back7374The ``SECRET_KEY`` is needed to keep the client-side sessions secure.75Choose that key wisely and as hard to guess and complex as possible.76Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
lines 77–78why, one step back77We will also add a method that allows for easy connections to the78specified database. This can be used to open a connection on request andMerge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
lines 79–92why, one step back79also from the interactive Python shell or a script. This will come in80handy later. We create a simple database connection through SQLite and81then tell it to use the :class:`sqlite3.Row` object to represent rows.82This allows us to treat the rows as if they were dictionaries instead of83tuples.8485::8687 def connect_db():88 """Connects to the specific database."""89 rv = sqlite3.connect(app.config['DATABASE'])90 rv.row_factory = sqlite3.Row91 return rv92Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
line 93why, one step back93With that out of the way, you should be able to start up the applicationUpdate docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
line 94why, one step back94without problems. Do this with the following commands::Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
line 95why, one step back95Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
lines 96–98why, one step back96 export FLASK_APP=flaskr97 export FLASK_DEBUG=198 flask runMerge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
line 99why, one step back99Update docs to the new CLI patternsArmin Ronacherbefore adoptiona7d829c
lines 100–103why, one step back100(In case you are on Windows you need to use `set` instead of `export`).101The :envvar:`FLASK_DEBUG` flag enables or disables the interactive debugger.102*Never leave debug mode activated in a production system*, because it will103allow users to execute code on the server!Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
lines 104–107why, one step back104105You will see a message telling you that server has started along with106the address at which you can access it.107Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49
lines 108–110why, one step back108When you head over to the server in your browser, you will get a 404 error109because we don't have any views yet. We will focus on that a little later,110but first, we should get the database working.Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
lines 111–118why, one step back111112.. admonition:: Externally Visible Server113114 Want your server to be publicly available? Check out the115 :ref:`externally visible server <public-server>` section for more116 information.117118Continue with :ref:`tutorial-dbcon`.