From charlesreid1

Packaging Flask in a Python Package

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!


Resources

Official flask site: patterns for distributing flask apps: http://flask.pocoo.org/docs/patterns/distribute/

Packaging a flask app: http://www.plankandwhittle.com/packaging-a-flask-web-app/

Flags