|
|
|
@ -1,7 +1,7 @@
|
|
|
|
|
.. _tutorial-setuptools: |
|
|
|
|
.. _tutorial-packaging: |
|
|
|
|
|
|
|
|
|
Step 3: Installing flaskr with setuptools |
|
|
|
|
========================================= |
|
|
|
|
Step 3: Installing flaskr as a Package |
|
|
|
|
====================================== |
|
|
|
|
|
|
|
|
|
Flask is now shipped with built-in support for `Click`_. Click provides |
|
|
|
|
Flask with enhanced and extensible command line utilities. Later in this |
|
|
|
@ -9,17 +9,21 @@ tutorial you will see exactly how to extend the ``flask`` command line
|
|
|
|
|
interface (CLI). |
|
|
|
|
|
|
|
|
|
A useful pattern to manage a Flask application is to install your app |
|
|
|
|
using `setuptools`_. This involves creating a :file:`setup.py` |
|
|
|
|
in the projects root directory. You also need to add an empty |
|
|
|
|
:file:`__init__.py` file to make the :file:`flaskr/flaskr` directory |
|
|
|
|
a package. The code structure at this point should be:: |
|
|
|
|
following the `Python Packaging Guide`_. Presently this involves |
|
|
|
|
creating two new files; :file:`setup.py` and :file:`MANIFEST.in` in the |
|
|
|
|
projects root directory. You also need to add an :file:`__init__.py` |
|
|
|
|
file to make the :file:`flaskr/flaskr` directory a package. After these |
|
|
|
|
changes, your code structure should be:: |
|
|
|
|
|
|
|
|
|
/flaskr |
|
|
|
|
/flaskr |
|
|
|
|
__init__.py |
|
|
|
|
/static |
|
|
|
|
/templates |
|
|
|
|
flaskr.py |
|
|
|
|
schema.sql |
|
|
|
|
setup.py |
|
|
|
|
MANIFEST.in |
|
|
|
|
|
|
|
|
|
The content of the ``setup.py`` file for ``flaskr`` is: |
|
|
|
|
|
|
|
|
@ -46,22 +50,37 @@ following lines::
|
|
|
|
|
graft flaskr/static |
|
|
|
|
include flaskr/schema.sql |
|
|
|
|
|
|
|
|
|
To simplify locating the application, add the following import statement |
|
|
|
|
into this file, :file:`flaskr/__init__.py`: |
|
|
|
|
|
|
|
|
|
.. sourcecode:: python |
|
|
|
|
|
|
|
|
|
from flaskr import app |
|
|
|
|
|
|
|
|
|
This import statement brings the application instance into the top-level |
|
|
|
|
of the application package. When it is time to run the application, the |
|
|
|
|
Flask development server needs the location of the app instance. This |
|
|
|
|
import statement simplifies the location process. Without it the export |
|
|
|
|
statement a few steps below would need to be |
|
|
|
|
``export FLASK_APP=flaskr.flaskr``. |
|
|
|
|
|
|
|
|
|
At this point you should be able to install the application. As usual, it |
|
|
|
|
is recommended to install your Flask application within a `virtualenv`_. |
|
|
|
|
With that said, go ahead and install the application with:: |
|
|
|
|
|
|
|
|
|
pip install --editable . |
|
|
|
|
|
|
|
|
|
.. note:: The above installation command assumes that it is run within the |
|
|
|
|
projects root directory, `flaskr/`. Also, the `editable` flag allows |
|
|
|
|
editing source code without having to reinstall the Flask app each time |
|
|
|
|
you make changes. |
|
|
|
|
The above installation command assumes that it is run within the projects |
|
|
|
|
root directory, `flaskr/`. The `editable` flag allows editing |
|
|
|
|
source code without having to reinstall the Flask app each time you make |
|
|
|
|
changes. The flaskr app is now installed in your virtualenv (see output |
|
|
|
|
of ``pip freeze``). |
|
|
|
|
|
|
|
|
|
With that out of the way, you should be able to start up the application. |
|
|
|
|
Do this with the following commands:: |
|
|
|
|
|
|
|
|
|
export FLASK_APP=flaskr.flaskr |
|
|
|
|
export FLASK_DEBUG=1 |
|
|
|
|
export FLASK_APP=flaskr |
|
|
|
|
export FLASK_DEBUG=true |
|
|
|
|
flask run |
|
|
|
|
|
|
|
|
|
(In case you are on Windows you need to use `set` instead of `export`). |
|
|
|
@ -85,5 +104,5 @@ but first, you should get the database working.
|
|
|
|
|
Continue with :ref:`tutorial-dbcon`. |
|
|
|
|
|
|
|
|
|
.. _Click: http://click.pocoo.org |
|
|
|
|
.. _setuptools: https://setuptools.readthedocs.io |
|
|
|
|
.. _Python Packaging Guide: https://packaging.python.org |
|
|
|
|
.. _virtualenv: https://virtualenv.pypa.io |