Skip to content
Myr

Clean up tutorial docs for installable app pattern with flaskr (#2002)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Clean up tutorial docs for installable app pattern - reading sequentially through the tutorial works. - fixes references to `export FLASK_APP=flaskr.flaskr` * Fixes titles for each section of flaskr tutorial * Revert grammar * Emphasize the Packaging Guide - adds more general packaging resource - removes the emphasis put on setuptools * rephrase and remove note admonitions - expanded on few points - removed note blocks, they are unneccessary * Remove note about reinstalling to update cli - I had mistakenly thought it was necessary to re-install the app to update the cli. - the `--editable` flag detects the change and the cli updates without issue.

authored by Kyle Lawlor · committed by Markus Unterwaditzer · parent b42e43e · Browse files · 9 files+142 added−115 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

9 files+142 added−115 removed

  • other:+142 added−115 removed

9 files+142 added−115 removed

modified:docs/tutorial/css.rst+1 added-1 removed
1.. _tutorial-css:23Step 9: Adding Style3Step 8: Adding Style4====================56Now that everything else works, it's time to add some style to the7application. Just create a stylesheet called :file:`style.css` in the
24 unmodified lines
modified:docs/tutorial/dbcon.rst+1 added-1 removed
1 unmodified line
23Step 4: Database Connections4----------------------------56You now have a function for establishing a database connection with6You currently have a function for establishing a database connection with7`connect_db`, but by itself, it is not particularly useful. Creating and8closing database connections all the time is very inefficient, so you will9need to keep it around for longer. Because database connections10encapsulate a transaction, you will need to make sure that only one
65 unmodified lines
modified:docs/tutorial/index.rst+1 added-1 removed
22 unmodified lines
23 introduction24 folders25 schema26 setup27 setuptools27 packaging28 dbcon29 dbinit30 views31 templates
2 unmodified lines
added:docs/tutorial/packaging.rst+108 added
1.. _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 app12following the `Python Packaging Guide`_. Presently this involves 13creating two new files; :file:`setup.py` and :file:`MANIFEST.in` in the 14projects root directory. You also need to add an :file:`__init__.py` 15file to make the :file:`flaskr/flaskr` directory a package. After these 16changes, your code structure should be::1718 /flaskr19 /flaskr20 __init__.py21 /static22 /templates23 flaskr.py24 schema.sql25 setup.py26 MANIFEST.in2728The content of the ``setup.py`` file for ``flaskr`` is:2930.. sourcecode:: python3132 from setuptools import setup3334 setup(35 name='flaskr',36 packages=['flaskr'],37 include_package_data=True,38 install_requires=[39 'flask',40 ],41 )4243When using setuptools, it is also necessary to specify any special files44that should be included in your package (in the :file:`MANIFEST.in`).45In this case, the static and templates directories need to be included,46as well as the schema. Create the :file:`MANIFEST.in` and add the47following lines::4849 graft flaskr/templates50 graft flaskr/static51 include flaskr/schema.sql5253To simplify locating the application, add the following import statement 54into this file, :file:`flaskr/__init__.py`:5556.. sourcecode:: python5758 from flaskr import app5960This import statement brings the application instance into the top-level 61of the application package. When it is time to run the application, the 62Flask development server needs the location of the app instance. This 63import statement simplifies the location process. Without it the export 64statement a few steps below would need to be 65``export FLASK_APP=flaskr.flaskr``.6667At this point you should be able to install the application. As usual, it68is recommended to install your Flask application within a `virtualenv`_.69With that said, go ahead and install the application with::7071 pip install --editable .7273The above installation command assumes that it is run within the projects 74root directory, `flaskr/`. The `editable` flag allows editing 75source code without having to reinstall the Flask app each time you make 76changes. The flaskr app is now installed in your virtualenv (see output 77of ``pip freeze``).7879With that out of the way, you should be able to start up the application.80Do this with the following commands::8182 export FLASK_APP=flaskr83 export FLASK_DEBUG=true84 flask run8586(In case you are on Windows you need to use `set` instead of `export`).87The :envvar:`FLASK_DEBUG` flag enables or disables the interactive debugger.88*Never leave debug mode activated in a production system*, because it will89allow users to execute code on the server!9091You will see a message telling you that server has started along with92the address at which you can access it.9394When you head over to the server in your browser, you will get a 404 error95because we don't have any views yet. That will be addressed a little later,96but first, you should get the database working.9798.. admonition:: Externally Visible Server99100 Want your server to be publicly available? Check out the101 :ref:`externally visible server <public-server>` section for more102 information.103104Continue with :ref:`tutorial-dbcon`.105106.. _Click: http://click.pocoo.org107.. _Python Packaging Guide: https://packaging.python.org108.. _virtualenv: https://virtualenv.pypa.io
modified:docs/tutorial/setup.rst+1 added-1 removed
92 unmodified lines
93 return rv9495In the next section you will see how to run the application.9697Continue with :ref:`tutorial-setuptools`.97Continue with :ref:`tutorial-packaging`.
deleted:docs/tutorial/setuptools.rst-89 removed
1.. _tutorial-setuptools:23Step 3: Installing flaskr with setuptools4=========================================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 app12using `setuptools`_. This involves creating a :file:`setup.py`13in the projects root directory. You also need to add an empty14:file:`__init__.py` file to make the :file:`flaskr/flaskr` directory15a package. The code structure at this point should be::1617 /flaskr18 /flaskr19 __init__.py20 /static21 /templates22 setup.py2324The content of the ``setup.py`` file for ``flaskr`` is:2526.. sourcecode:: python2728 from setuptools import setup2930 setup(31 name='flaskr',32 packages=['flaskr'],33 include_package_data=True,34 install_requires=[35 'flask',36 ],37 )3839When using setuptools, it is also necessary to specify any special files40that should be included in your package (in the :file:`MANIFEST.in`).41In this case, the static and templates directories need to be included,42as well as the schema. Create the :file:`MANIFEST.in` and add the43following lines::4445 graft flaskr/templates46 graft flaskr/static47 include flaskr/schema.sql4849At this point you should be able to install the application. As usual, it50is recommended to install your Flask application within a `virtualenv`_.51With that said, go ahead and install the application with::5253 pip install --editable .5455.. note:: The above installation command assumes that it is run within the56 projects root directory, `flaskr/`. Also, the `editable` flag allows57 editing source code without having to reinstall the Flask app each time58 you make changes.5960With that out of the way, you should be able to start up the application.61Do this with the following commands::6263 export FLASK_APP=flaskr.flaskr64 export FLASK_DEBUG=165 flask run6667(In case you are on Windows you need to use `set` instead of `export`).68The :envvar:`FLASK_DEBUG` flag enables or disables the interactive debugger.69*Never leave debug mode activated in a production system*, because it will70allow users to execute code on the server!7172You will see a message telling you that server has started along with73the address at which you can access it.7475When you head over to the server in your browser, you will get a 404 error76because we don't have any views yet. That will be addressed a little later,77but first, you should get the database working.7879.. admonition:: Externally Visible Server8081 Want your server to be publicly available? Check out the82 :ref:`externally visible server <public-server>` section for more83 information.8485Continue with :ref:`tutorial-dbcon`.8687.. _Click: http://click.pocoo.org88.. _setuptools: https://setuptools.readthedocs.io89.. _virtualenv: https://virtualenv.pypa.io
modified:docs/tutorial/templates.rst+1 added-1 removed
1.. _tutorial-templates:23Step 8: The Templates3Step 7: The Templates4=====================56Now it is time to start working on the templates. As you may have7noticed, if you make requests with the app running, you will get
105 unmodified lines
modified:docs/tutorial/testing.rst+28 added-20 removed
7 unmodified lines
8modifications in the future. The application above is used as a basic9example of how to perform unit testing in the :ref:`testing` section of the10documentation. Go there to see how easy it is to test Flask applications.1112Adding Tests to flaskr13======================12Adding tests to flaskr13----------------------1415Assuming you have seen the testing section above and have either written15Assuming you have seen the :ref:`testing` section and have either written16your own tests for ``flaskr`` or have followed along with the examples17provided, you might be wondering about ways to organize the project.1819One possible and recommended project structure is::
3 unmodified lines
23 __init__.py24 static/25 templates/26 tests/27 context.py27 test_flaskr.py28 setup.py29 MANIFEST.in3032For now go ahead a create the :file:`tests/` directory as well as the33:file:`context.py` and :file:`test_flaskr.py` files, if you haven't34already. The context file is used as an import helper. The contents35of that file are::31For now go ahead a create the :file:`tests/` directory as well as the 32:file:`test_flaskr.py` file.3337 import sys, os34Running the tests35-----------------3639 basedir = os.path.dirname(os.path.abspath(__file__))40 sys.path.insert(0, basedir + '/../')37At this point you can run the tests. Here ``pytest`` will be used. 3842 from flaskr import flaskr39.. note:: Make sure that ``pytest`` is installed in the same virtualenv 40 as flaskr. Otherwise ``pytest`` test will not be able to import the 41 required components to test the application::4244Testing + Setuptools45====================43 pip install -e .44 pip install pytest 4547One way to handle testing is to integrate it with ``setuptools``. All it48requires is adding a couple of lines to the :file:`setup.py` file and49creating a new file :file:`setup.cfg`. Go ahead and update the50:file:`setup.py` to contain::46Run and watch the tests pass, within the top-level :file:`flaskr/` 47directory as::4849 py.test5051Testing + setuptools52--------------------5354One way to handle testing is to integrate it with ``setuptools``. Here55that requires adding a couple of lines to the :file:`setup.py` file and56creating a new file :file:`setup.cfg`. One benefit of running the tests 57this way is that you do not have to install ``pytest``. Go ahead and 58update the :file:`setup.py` file to contain::5960 from setuptools import setup6162 setup(63 name='flaskr',
1 unmodified line
65 include_package_data=True,66 install_requires=[67 'flask',68 ],61 )69 setup_requires=[70 'pytest-runner',71 ],72 tests_require=[73 'pytest',74 ],75 )7677Now create :file:`setup.cfg` in the project root (alongside78:file:`setup.py`)::7980 [aliases]
11 unmodified lines
9293This is one possible way to run and manage testing. Here ``pytest`` is94used, but there are other options such as ``nose``. Integrating testing95with ``setuptools`` is convenient because it is not necessary to actually88download ``pytest`` or any other testing framework one might use.96download ``pytest`` or any other testing framework one might use.
modified:docs/tutorial/views.rst+1 added-1 removed
1.. _tutorial-views:23Step 7: The View Functions3Step 6: The View Functions4==========================56Now that the database connections are working, you can start writing the7view functions. You will need four of them:
110 unmodified lines