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=
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.
=Flask Forms=
I had some issues with Flask-WTF for creating forms in Flask, mainly because the documentation did not give any information at all about what goes into the HTML template.
Between [http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iii-web-forms Flask Mega Tutorial: Web Forms] and [http://stackoverflow.com/questions/14589393/how-to-actually-upload-a-file-using-flask-wtf-filefield Stack Overflow: Uploading a file with Flask WTF], I was able to figure out how to get a Flask web form displaying properly.


=Packaging Flask Apps=
=Packaging Flask Apps=

Revision as of 18:26, 15 July 2014

Related: Making a Chemical Reactor into an Internet of Things Thing

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.

Flask Forms

I had some issues with Flask-WTF for creating forms in Flask, mainly because the documentation did not give any information at all about what goes into the HTML template.

Between Flask Mega Tutorial: Web Forms and Stack Overflow: Uploading a file with Flask WTF, I was able to figure out how to get a Flask web form displaying properly.

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/