Skip to content
Myr

Updated documentation once more for new cli.

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

authored by Armin Ronacher · committed by Armin Ronacher · parent a3a5075 · Browse files · 4 files+41 added−6 removed
This commit

State

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

Diff stats

4 files+41 added−6 removed

  • other:+41 added−6 removed

4 files+41 added−6 removed

modified:docs/patterns/appfactories.rst+7 added-2 removed
88 unmodified lines
89Using Applications90------------------9192So to use such an application you then have to create the application93first. Here an example `run.py` file that runs such an application::93first in a separate file otherwise the ``flask`` command won't be able94to find it. Here an example `exampleapp.py` file that creates such95an application::9697 from yourapplication import create_app98 app = create_app('/path/to/config.cfg')97 app.run()99100It can then be used with the ``flask`` command::101102 flask --app=exampleapp run103104Factory Improvements105--------------------106107The factory function from above is not very clever so far, you can improve
8 unmodified lines
modified:docs/patterns/flashing.rst-4 removed
36 unmodified lines
37 flash('You were successfully logged in')38 return redirect(url_for('index'))39 return render_template('login.html', error=error)4041 if __name__ == "__main__":42 app.run()434441And here the ``layout.html`` template which does the magic:4243.. sourcecode:: html+jinja44
103 unmodified lines
modified:docs/cli.rst+29 added
97 unmodified lines
98The command will then show up on the command line::99100 $ flask -a hello.py initdb101 Init the db102103Factory Functions104-----------------105106In case you are using factory functions to create your application (see107:ref:`app-factories`) you will discover that the ``flask`` command cannot108work with them directly. Flask won't be able to figure out how to109instanciate your application properly by itself. Because of this reason110the recommendation is to create a separate file that instanciates111applications.112113For instance if you have a factory function that creates an application114from a filename you could make a separate file that creates such an115application from an environment variable.116117For instance this could be a file named ``autoapp.py`` with these118contents::119120 import os121 from yourapplication import create_app122 app = create_app(os.environ['YOURAPPLICATION_CONFIG'])123124Once this has happened you can make the flask command automatically pick125it up::126127 export YOURAPPLICATION_CONFIG=/path/to/config.cfg128 export FLASK_APP=/path/to/autoapp.py129130From this point onwards ``flask`` will find your application.
modified:flask/cli.py+5 added
61 unmodified lines
62 if filename.endswith('.py'):63 filename = filename[:-3]64 elif os.path.split(filename)[1] == '__init__.py':65 filename = os.path.dirname(filename)66 else:67 raise NoAppException('The file provided (%s) does exist but is not a '68 'valid Python file. This means that it cannot '69 'be used as application. Please change the '70 'extension to .py' % filename)71 filename = os.path.realpath(filename)7273 dirpath = filename74 while 1:
282 unmodified lines