Flask: Difference between revisions
From charlesreid1
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
<!-- | <!-- | ||
Related: [[Making a Chemical Reactor into an Internet of Things Thing]] | Related: [[Making a Chemical Reactor into an Internet of Things Thing]] | ||
--> | |||
=Flask 101= | =Flask 101= | ||
| Line 7: | Line 13: | ||
The Flask documentation is useful for making your first baby step, but it often leaves out the details of what your HTML templates should look like, which is critical information, and therefore a baffling omission. | The Flask documentation is useful for making your first baby step, but it often leaves out the details of what your HTML templates should look like, which is critical information, and therefore a baffling omission. | ||
<!-- | |||
=Flask Forms= | =Flask Forms= | ||
Revision as of 23:32, 30 July 2015
Flask 101
The Flask mega tutorial is really handy: http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
The Flask documentation is useful for making your first baby step, but it often leaves out the details of what your HTML templates should look like, which is critical information, and therefore a baffling omission.
Packaging Flask Apps
Deploying Flask Web App as Submodule
Instructions for deploying a Flask web app as a submodule in a Python module:
First, your directory structure will look something like this:
README.md
setup.py
mymodule/
__init__.py
submodule1/
submodule2/
webapp/
__init__.py
additional_routes.py
templates/
[...]
static/
[...]
Next, your setup.py file will look something like this:
config = {
'description': 'My Module',
'install_requires': ['flask'],
'packages': ['mymodule','mymodule.submodule1','mymodule.submodule2','mymodule.webapp'],
'include_package_data' : True,
'package_data' : {
'templates' : 'mymodule/webapp/templates/*',
'static' : 'mymodule/webapp/static/*'
},
'scripts': [],
'name': 'mulch',
'zip_safe' : False
}
setup(**config)
The key lines here are include_package_data and package_data, which will also install your non-Python template and static files with your module.
Now you can install your module with python setup.py install, and your module is available to use from anywhere.
To create an instance of your module's web app from anywhere, follow these steps:
1. Install the module
2. Import the webapp submodule:
from mymodule.webapp import *
3. Start the webapp:
app.run(debug=True)
Voila!
More information: http://www.plankandwhittle.com/packaging-a-flask-web-app/
Yet more information: http://flask.pocoo.org/docs/patterns/distribute/