|
|
@ -3,15 +3,15 @@ |
|
|
|
Step 2: Application Setup Code |
|
|
|
Step 2: Application Setup Code |
|
|
|
============================== |
|
|
|
============================== |
|
|
|
|
|
|
|
|
|
|
|
Now that we have the schema in place we can create the application module. |
|
|
|
Now that we have the schema in place, we can create the application module. |
|
|
|
Let's call it flaskr.py. We will place this file inside the flaskr folder. |
|
|
|
Let's call it ``flaskr.py``. We will place this file inside the ``flaskr`` |
|
|
|
We will begin by adding the imports we need and by adding the config |
|
|
|
folder. We will begin by adding the imports we need and by adding the config |
|
|
|
section. For small applications, it is possible to drop the configuration |
|
|
|
section. For small applications, it is possible to drop the configuration |
|
|
|
directly into the module, and this is what we will be doing here. However |
|
|
|
directly into the module, and this is what we will be doing here. However, |
|
|
|
a cleaner solution would be to create a separate ``.ini`` or ``.py`` file and |
|
|
|
a cleaner solution would be to create a separate ``.ini`` or ``.py`` file, |
|
|
|
load that or import the values from there. |
|
|
|
load that, and import the values from there. |
|
|
|
|
|
|
|
|
|
|
|
First we add the imports in :file:`flaskr.py`:: |
|
|
|
First, we add the imports in :file:`flaskr.py`:: |
|
|
|
|
|
|
|
|
|
|
|
# all the imports |
|
|
|
# all the imports |
|
|
|
import os |
|
|
|
import os |
|
|
@ -19,8 +19,8 @@ First we add the imports in :file:`flaskr.py`:: |
|
|
|
from flask import Flask, request, session, g, redirect, url_for, abort, \ |
|
|
|
from flask import Flask, request, session, g, redirect, url_for, abort, \ |
|
|
|
render_template, flash |
|
|
|
render_template, flash |
|
|
|
|
|
|
|
|
|
|
|
Next we can create our actual application and initialize it with the |
|
|
|
Next, we can create our actual application and initialize it with the |
|
|
|
config from the same file, in :file:`flaskr.py`:: |
|
|
|
config from the same file in :file:`flaskr.py`:: |
|
|
|
|
|
|
|
|
|
|
|
# create our little application :) |
|
|
|
# create our little application :) |
|
|
|
app = Flask(__name__) |
|
|
|
app = Flask(__name__) |
|
|
@ -35,44 +35,46 @@ config from the same file, in :file:`flaskr.py`:: |
|
|
|
)) |
|
|
|
)) |
|
|
|
app.config.from_envvar('FLASKR_SETTINGS', silent=True) |
|
|
|
app.config.from_envvar('FLASKR_SETTINGS', silent=True) |
|
|
|
|
|
|
|
|
|
|
|
The :class:`~flask.Config` object works similar to a dictionary so we |
|
|
|
The :class:`~flask.Config` object works similarly to a dictionary so we |
|
|
|
can update it with new values. |
|
|
|
can update it with new values. |
|
|
|
|
|
|
|
|
|
|
|
.. admonition:: Database Path |
|
|
|
.. admonition:: Database Path |
|
|
|
|
|
|
|
|
|
|
|
Operating systems know the concept of a current working directory for |
|
|
|
Operating systems know the concept of a current working directory for |
|
|
|
each process. Unfortunately you cannot depend on this in web |
|
|
|
each process. Unfortunately, you cannot depend on this in web |
|
|
|
applications because you might have more than one application in the |
|
|
|
applications because you might have more than one application in the |
|
|
|
same process. |
|
|
|
same process. |
|
|
|
|
|
|
|
|
|
|
|
For this reason the ``app.root_path`` attribute can be used to |
|
|
|
For this reason the ``app.root_path`` attribute can be used to |
|
|
|
get the path to the application. Together with the ``os.path`` module |
|
|
|
get the path to the application. Together with the ``os.path`` module, |
|
|
|
files can then easily be found. In this example we place the |
|
|
|
files can then easily be found. In this example, we place the |
|
|
|
database right next to it. |
|
|
|
database right next to it. |
|
|
|
|
|
|
|
|
|
|
|
For a real-work application it's recommended to use |
|
|
|
For a real-world application, it's recommended to use |
|
|
|
:ref:`instance-folders` instead. |
|
|
|
:ref:`instance-folders` instead. |
|
|
|
|
|
|
|
|
|
|
|
Usually, it is a good idea to load a separate, environment specific |
|
|
|
Usually, it is a good idea to load a separate, environment-specific |
|
|
|
configuration file. Flask allows you to import multiple configurations and it |
|
|
|
configuration file. Flask allows you to import multiple configurations and it |
|
|
|
will use the setting defined in the last import. This enables robust |
|
|
|
will use the setting defined in the last import. This enables robust |
|
|
|
configuration setups. :meth:`~flask.Config.from_envvar` can help achieve this. |
|
|
|
configuration setups. :meth:`~flask.Config.from_envvar` can help achieve this. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. code-block:: python |
|
|
|
|
|
|
|
|
|
|
|
app.config.from_envvar('FLASKR_SETTINGS', silent=True) |
|
|
|
app.config.from_envvar('FLASKR_SETTINGS', silent=True) |
|
|
|
|
|
|
|
|
|
|
|
Simply define the environment variable :envvar:`FLASKR_SETTINGS` that points to |
|
|
|
Simply define the environment variable :envvar:`FLASKR_SETTINGS` that points to |
|
|
|
a config file to be loaded. The silent switch just tells Flask to not complain |
|
|
|
a config file to be loaded. The silent switch just tells Flask to not complain |
|
|
|
if no such environment key is set. |
|
|
|
if no such environment key is set. |
|
|
|
|
|
|
|
|
|
|
|
In addition to that you can use the :meth:`~flask.Config.from_object` |
|
|
|
In addition to that, you can use the :meth:`~flask.Config.from_object` |
|
|
|
method on the config object and provide it with an import name of a |
|
|
|
method on the config object and provide it with an import name of a |
|
|
|
module. Flask will then initialize the variable from that module. Note |
|
|
|
module. Flask will then initialize the variable from that module. Note |
|
|
|
that in all cases only variable names that are uppercase are considered. |
|
|
|
that in all cases, only variable names that are uppercase are considered. |
|
|
|
|
|
|
|
|
|
|
|
The ``SECRET_KEY`` is needed to keep the client-side sessions secure. |
|
|
|
The ``SECRET_KEY`` is needed to keep the client-side sessions secure. |
|
|
|
Choose that key wisely and as hard to guess and complex as possible. |
|
|
|
Choose that key wisely and as hard to guess and complex as possible. |
|
|
|
|
|
|
|
|
|
|
|
We will also add a method that allows for easily connecting to the |
|
|
|
We will also add a method that allows for easy connections to the |
|
|
|
specified database. This can be used to open a connection on request and |
|
|
|
specified database. This can be used to open a connection on request and |
|
|
|
also from the interactive Python shell or a script. This will come in |
|
|
|
also from the interactive Python shell or a script. This will come in |
|
|
|
handy later. We create a simple database connection through SQLite and |
|
|
|
handy later. We create a simple database connection through SQLite and |
|
|
@ -88,7 +90,7 @@ tuples. |
|
|
|
rv.row_factory = sqlite3.Row |
|
|
|
rv.row_factory = sqlite3.Row |
|
|
|
return rv |
|
|
|
return rv |
|
|
|
|
|
|
|
|
|
|
|
With that out of the way you should be able to start up the application |
|
|
|
With that out of the way, you should be able to start up the application |
|
|
|
without problems. Do this with the following command:: |
|
|
|
without problems. Do this with the following command:: |
|
|
|
|
|
|
|
|
|
|
|
flask --app=flaskr --debug run |
|
|
|
flask --app=flaskr --debug run |
|
|
@ -100,9 +102,9 @@ users to execute code on the server! |
|
|
|
You will see a message telling you that server has started along with |
|
|
|
You will see a message telling you that server has started along with |
|
|
|
the address at which you can access it. |
|
|
|
the address at which you can access it. |
|
|
|
|
|
|
|
|
|
|
|
When you head over to the server in your browser you will get an 404 |
|
|
|
When you head over to the server in your browser, you will get a 404 error |
|
|
|
page not found error because we don't have any views yet. But we will |
|
|
|
because we don't have any views yet. We will focus on that a little later, |
|
|
|
focus on that a little later. First we should get the database working. |
|
|
|
but first, we should get the database working. |
|
|
|
|
|
|
|
|
|
|
|
.. admonition:: Externally Visible Server |
|
|
|
.. admonition:: Externally Visible Server |
|
|
|
|
|
|
|
|
|
|
|