packaging.rst
This file
Path
docs/tutorial/packaging.rst
106 lines · 3.8 KiB
Last changed by
Minor edits to tutorial for clarity. (#2330)Dave Morsebefore adoption50c6df7
History of this fileTake it
Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
lines 1–11why, one step back1.. _tutorial-packaging:23Step 3: Installing flaskr as a Package4======================================56Flask is now shipped with built-in support for `Click`_. Click provides7Flask with enhanced and extensible command line utilities. Later in this8tutorial you will see exactly how to extend the ``flask`` command line9interface (CLI).1011A useful pattern to manage a Flask application is to install your appMinor edits to tutorial for clarity. (#2330)Dave Morsebefore adoption50c6df7
lines 12–15why, one step back12following the `Python Packaging Guide`_. Presently this involves13creating two new files; :file:`setup.py` and :file:`MANIFEST.in` in the14projects root directory. You also need to add an :file:`__init__.py`15file to make the :file:`flaskr/flaskr` directory a package. After theseMerge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
lines 16–27why, one step back16changes, your code structure should be::1718 /flaskr19 /flaskr20 __init__.py21 /static22 /templates23 flaskr.py24 schema.sql25 setup.py26 MANIFEST.in27Minor edits to tutorial for clarity. (#2330)Dave Morsebefore adoption50c6df7
line 28why, one step back28Create the ``setup.py`` file for ``flaskr`` with the following content::Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
lines 29–43why, one step back2930 from setuptools import setup3132 setup(33 name='flaskr',34 packages=['flaskr'],35 include_package_data=True,36 install_requires=[37 'flask',38 ],39 )4041When using setuptools, it is also necessary to specify any special files42that should be included in your package (in the :file:`MANIFEST.in`).43In this case, the static and templates directories need to be included,Minor edits to tutorial for clarity. (#2330)Dave Morsebefore adoption50c6df7
line 44why, one step back44as well as the schema.Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
line 45why, one step back45Minor edits to tutorial for clarity. (#2330)Dave Morsebefore adoption50c6df7
lines 46–47why, one step back46Create the :file:`MANIFEST.in` and add the following lines::47Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
lines 48–51why, one step back48 graft flaskr/templates49 graft flaskr/static50 include flaskr/schema.sql51Minor edits to tutorial for clarity. (#2330)Dave Morsebefore adoption50c6df7
lines 52–53why, one step back52Next, to simplify locating the application, create the file,53:file:`flaskr/__init__.py` containing only the following import statement::Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
line 54why, one step back54Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption2b8fef4
line 55why, one step back55 from .flaskr import appMerge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
line 56why, one step back56Minor edits to tutorial for clarity. (#2330)Dave Morsebefore adoption50c6df7
lines 57–61why, one step back57This import statement brings the application instance into the top-level58of the application package. When it is time to run the application, the59Flask development server needs the location of the app instance. This60import statement simplifies the location process. Without the above61import statement, the export statement a few steps below would need to beMerge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
lines 62–65why, one step back62``export FLASK_APP=flaskr.flaskr``.6364At this point you should be able to install the application. As usual, it65is recommended to install your Flask application within a `virtualenv`_.Minor edits to tutorial for clarity. (#2330)Dave Morsebefore adoption50c6df7
lines 66–67why, one step back66With that said, from the ``flaskr/`` directory, go ahead and install the67application with::Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
lines 68–70why, one step back6869 pip install --editable .70Minor edits to tutorial for clarity. (#2330)Dave Morsebefore adoption50c6df7
lines 71–74why, one step back71The above installation command assumes that it is run within the projects72root directory, ``flaskr/``. The ``editable`` flag allows editing73source code without having to reinstall the Flask app each time you make74changes. The flaskr app is now installed in your virtualenv (see outputMerge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
lines 75–77why, one step back75of ``pip freeze``).7677With that out of the way, you should be able to start up the application.Minor edits to tutorial for clarity. (#2330)Dave Morsebefore adoption50c6df7
line 78why, one step back78Do this on Mac or Linux with the following commands in ``flaskr/``::Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
lines 79–83why, one step back7980 export FLASK_APP=flaskr81 export FLASK_DEBUG=true82 flask run83Minor edits to tutorial for clarity. (#2330)Dave Morsebefore adoption50c6df7
line 84why, one step back84(In case you are on Windows you need to use ``set`` instead of ``export``).Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
lines 85–89why, one step back85The :envvar:`FLASK_DEBUG` flag enables or disables the interactive debugger.86*Never leave debug mode activated in a production system*, because it will87allow users to execute code on the server!8889You will see a message telling you that server has started along withMinor edits to tutorial for clarity. (#2330)Dave Morsebefore adoption50c6df7
line 90why, one step back90the address at which you can access it in a browser.Merge branch '0.11-maintenance'Markus Unterwaditzerbefore adoption7132feb
lines 91–106why, one step back9192When you head over to the server in your browser, you will get a 404 error93because we don't have any views yet. That will be addressed a little later,94but first, you should get the database working.9596.. admonition:: Externally Visible Server9798 Want your server to be publicly available? Check out the99 :ref:`externally visible server <public-server>` section for more100 information.101102Continue with :ref:`tutorial-dbcon`.103104.. _Click: http://click.pocoo.org105.. _Python Packaging Guide: https://packaging.python.org106.. _virtualenv: https://virtualenv.pypa.io