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.
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
Provenance
- Commit
e6f9d2b- Tree
9cfebcb- Author
Kyle Lawlor <wgwz@users.noreply.github.com>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent
b42e43e
9 files changed+142 added−115 removed
modified:docs/tutorial/css.rst+1 added-1 removed
1 .. _tutorial-css:2 3-Step 9: Adding Style3+Step 8: Adding Style4 ====================5 6 Now that everything else works, it's time to add some style to the7 application. Just create a stylesheet called :file:`style.css` in the24 unmodified linesmodified:docs/tutorial/dbcon.rst+1 added-1 removed
1 unmodified line2 3 Step 4: Database Connections4 ----------------------------5 6-You now have a function for establishing a database connection with6+You currently have a function for establishing a database connection with7 `connect_db`, but by itself, it is not particularly useful. Creating and8 closing database connections all the time is very inefficient, so you will9 need to keep it around for longer. Because database connections10 encapsulate a transaction, you will need to make sure that only one65 unmodified linesmodified:docs/tutorial/index.rst+1 added-1 removed
22 unmodified lines23 introduction24 folders25 schema26 setup27- setuptools27+ packaging28 dbcon29 dbinit30 views31 templates2 unmodified linesadded:docs/tutorial/packaging.rst+108 added
1+.. _tutorial-packaging:2+3+Step 3: Installing flaskr as a Package4+======================================5+6+Flask is now shipped with built-in support for `Click`_. Click provides7+Flask with enhanced and extensible command line utilities. Later in this8+tutorial you will see exactly how to extend the ``flask`` command line9+interface (CLI).10+11+A useful pattern to manage a Flask application is to install your app12+following the `Python Packaging Guide`_. Presently this involves 13+creating two new files; :file:`setup.py` and :file:`MANIFEST.in` in the 14+projects root directory. You also need to add an :file:`__init__.py` 15+file to make the :file:`flaskr/flaskr` directory a package. After these 16+changes, your code structure should be::17+18+ /flaskr19+ /flaskr20+ __init__.py21+ /static22+ /templates23+ flaskr.py24+ schema.sql25+ setup.py26+ MANIFEST.in27+28+The content of the ``setup.py`` file for ``flaskr`` is:29+30+.. sourcecode:: python31+32+ from setuptools import setup33+34+ setup(35+ name='flaskr',36+ packages=['flaskr'],37+ include_package_data=True,38+ install_requires=[39+ 'flask',40+ ],41+ )42+43+When using setuptools, it is also necessary to specify any special files44+that should be included in your package (in the :file:`MANIFEST.in`).45+In this case, the static and templates directories need to be included,46+as well as the schema. Create the :file:`MANIFEST.in` and add the47+following lines::48+49+ graft flaskr/templates50+ graft flaskr/static51+ include flaskr/schema.sql52+53+To simplify locating the application, add the following import statement 54+into this file, :file:`flaskr/__init__.py`:55+56+.. sourcecode:: python57+58+ from flaskr import app59+60+This import statement brings the application instance into the top-level 61+of the application package. When it is time to run the application, the 62+Flask development server needs the location of the app instance. This 63+import statement simplifies the location process. Without it the export 64+statement a few steps below would need to be 65+``export FLASK_APP=flaskr.flaskr``.66+67+At this point you should be able to install the application. As usual, it68+is recommended to install your Flask application within a `virtualenv`_.69+With that said, go ahead and install the application with::70+71+ pip install --editable .72+73+The above installation command assumes that it is run within the projects 74+root directory, `flaskr/`. The `editable` flag allows editing 75+source code without having to reinstall the Flask app each time you make 76+changes. The flaskr app is now installed in your virtualenv (see output 77+of ``pip freeze``).78+79+With that out of the way, you should be able to start up the application.80+Do this with the following commands::81+82+ export FLASK_APP=flaskr83+ export FLASK_DEBUG=true84+ flask run85+86+(In case you are on Windows you need to use `set` instead of `export`).87+The :envvar:`FLASK_DEBUG` flag enables or disables the interactive debugger.88+*Never leave debug mode activated in a production system*, because it will89+allow users to execute code on the server!90+91+You will see a message telling you that server has started along with92+the address at which you can access it.93+94+When you head over to the server in your browser, you will get a 404 error95+because we don't have any views yet. That will be addressed a little later,96+but first, you should get the database working.97+98+.. admonition:: Externally Visible Server99+100+ Want your server to be publicly available? Check out the101+ :ref:`externally visible server <public-server>` section for more102+ information.103+104+Continue with :ref:`tutorial-dbcon`.105+106+.. _Click: http://click.pocoo.org107+.. _Python Packaging Guide: https://packaging.python.org108+.. _virtualenv: https://virtualenv.pypa.iomodified:docs/tutorial/setup.rst+1 added-1 removed
92 unmodified lines93 return rv94 95 In the next section you will see how to run the application.96 97-Continue with :ref:`tutorial-setuptools`.97+Continue with :ref:`tutorial-packaging`.deleted:docs/tutorial/setuptools.rst-89 removed
1-.. _tutorial-setuptools:2-3-Step 3: Installing flaskr with setuptools4-=========================================5-6-Flask is now shipped with built-in support for `Click`_. Click provides7-Flask with enhanced and extensible command line utilities. Later in this8-tutorial you will see exactly how to extend the ``flask`` command line9-interface (CLI).10-11-A useful pattern to manage a Flask application is to install your app12-using `setuptools`_. This involves creating a :file:`setup.py`13-in the projects root directory. You also need to add an empty14-:file:`__init__.py` file to make the :file:`flaskr/flaskr` directory15-a package. The code structure at this point should be::16-17- /flaskr18- /flaskr19- __init__.py20- /static21- /templates22- setup.py23-24-The content of the ``setup.py`` file for ``flaskr`` is:25-26-.. sourcecode:: python27-28- from setuptools import setup29-30- setup(31- name='flaskr',32- packages=['flaskr'],33- include_package_data=True,34- install_requires=[35- 'flask',36- ],37- )38-39-When using setuptools, it is also necessary to specify any special files40-that should be included in your package (in the :file:`MANIFEST.in`).41-In this case, the static and templates directories need to be included,42-as well as the schema. Create the :file:`MANIFEST.in` and add the43-following lines::44-45- graft flaskr/templates46- graft flaskr/static47- include flaskr/schema.sql48-49-At this point you should be able to install the application. As usual, it50-is recommended to install your Flask application within a `virtualenv`_.51-With that said, go ahead and install the application with::52-53- pip install --editable .54-55-.. 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.59-60-With that out of the way, you should be able to start up the application.61-Do this with the following commands::62-63- export FLASK_APP=flaskr.flaskr64- export FLASK_DEBUG=165- flask run66-67-(In case you are on Windows you need to use `set` instead of `export`).68-The :envvar:`FLASK_DEBUG` flag enables or disables the interactive debugger.69-*Never leave debug mode activated in a production system*, because it will70-allow users to execute code on the server!71-72-You will see a message telling you that server has started along with73-the address at which you can access it.74-75-When you head over to the server in your browser, you will get a 404 error76-because we don't have any views yet. That will be addressed a little later,77-but first, you should get the database working.78-79-.. admonition:: Externally Visible Server80-81- Want your server to be publicly available? Check out the82- :ref:`externally visible server <public-server>` section for more83- information.84-85-Continue with :ref:`tutorial-dbcon`.86-87-.. _Click: http://click.pocoo.org88-.. _setuptools: https://setuptools.readthedocs.io89-.. _virtualenv: https://virtualenv.pypa.iomodified:docs/tutorial/templates.rst+1 added-1 removed
1 .. _tutorial-templates:2 3-Step 8: The Templates3+Step 7: The Templates4 =====================5 6 Now it is time to start working on the templates. As you may have7 noticed, if you make requests with the app running, you will get105 unmodified linesmodified:docs/tutorial/testing.rst+28 added-20 removed
7 unmodified lines8 modifications in the future. The application above is used as a basic9 example of how to perform unit testing in the :ref:`testing` section of the10 documentation. Go there to see how easy it is to test Flask applications.11 12-Adding Tests to flaskr13-======================12+Adding tests to flaskr13+----------------------14 15-Assuming you have seen the testing section above and have either written15+Assuming you have seen the :ref:`testing` section and have either written16 your own tests for ``flaskr`` or have followed along with the examples17 provided, you might be wondering about ways to organize the project.18 19 One possible and recommended project structure is::3 unmodified lines23 __init__.py24 static/25 templates/26 tests/27- context.py27 test_flaskr.py28 setup.py29 MANIFEST.in30 32-For 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't34-already. The context file is used as an import helper. The contents35-of that file are::31+For now go ahead a create the :file:`tests/` directory as well as the 32+:file:`test_flaskr.py` file.33 37- import sys, os34+Running the tests35+-----------------36 39- basedir = os.path.dirname(os.path.abspath(__file__))40- sys.path.insert(0, basedir + '/../')37+At this point you can run the tests. Here ``pytest`` will be used. 38 42- 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::42 44-Testing + Setuptools45-====================43+ pip install -e .44+ pip install pytest 45 47-One way to handle testing is to integrate it with ``setuptools``. All it48-requires is adding a couple of lines to the :file:`setup.py` file and49-creating a new file :file:`setup.cfg`. Go ahead and update the50-:file:`setup.py` to contain::46+Run and watch the tests pass, within the top-level :file:`flaskr/` 47+directory as::48 49+ py.test50+51+Testing + setuptools52+--------------------53+54+One way to handle testing is to integrate it with ``setuptools``. Here55+that requires adding a couple of lines to the :file:`setup.py` file and56+creating a new file :file:`setup.cfg`. One benefit of running the tests 57+this way is that you do not have to install ``pytest``. Go ahead and 58+update the :file:`setup.py` file to contain::59+60 from setuptools import setup61 62 setup(63 name='flaskr',1 unmodified line65 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 )76+77 Now create :file:`setup.cfg` in the project root (alongside78 :file:`setup.py`)::79 80 [aliases]11 unmodified lines92 93 This is one possible way to run and manage testing. Here ``pytest`` is94 used, but there are other options such as ``nose``. Integrating testing95 with ``setuptools`` is convenient because it is not necessary to actually88-download ``pytest`` or any other testing framework one might use.96+download ``pytest`` or any other testing framework one might use.modified:docs/tutorial/views.rst+1 added-1 removed
1 .. _tutorial-views:2 3-Step 7: The View Functions3+Step 6: The View Functions4 ==========================5 6 Now that the database connections are working, you can start writing the7 view functions. You will need four of them:110 unmodified lines