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
Provenance
- Commit
a3ad540- Tree
b08b003- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
a3a5075
4 files changed+41 added−6 removed
modified:docs/patterns/appfactories.rst+7 added-2 removed
88 unmodified lines89 Using Applications90 ------------------91 92 So to use such an application you then have to create the application93-first. Here an example `run.py` file that runs such an application::93+first in a separate file otherwise the ``flask`` command won't be able94+to find it. Here an example `exampleapp.py` file that creates such95+an application::96 97 from yourapplication import create_app98 app = create_app('/path/to/config.cfg')97- app.run()99 100+It can then be used with the ``flask`` command::101+102+ flask --app=exampleapp run103+104 Factory Improvements105 --------------------106 107 The factory function from above is not very clever so far, you can improve8 unmodified linesmodified:docs/patterns/flashing.rst-4 removed
36 unmodified lines37 flash('You were successfully logged in')38 return redirect(url_for('index'))39 return render_template('login.html', error=error)40 41- if __name__ == "__main__":42- app.run()43-44-41 And here the ``layout.html`` template which does the magic:42 43 .. sourcecode:: html+jinja44 103 unmodified linesmodified:docs/cli.rst+29 added
97 unmodified lines98 The command will then show up on the command line::99 100 $ flask -a hello.py initdb101 Init the db102+103+Factory Functions104+-----------------105+106+In case you are using factory functions to create your application (see107+:ref:`app-factories`) you will discover that the ``flask`` command cannot108+work with them directly. Flask won't be able to figure out how to109+instanciate your application properly by itself. Because of this reason110+the recommendation is to create a separate file that instanciates111+applications.112+113+For instance if you have a factory function that creates an application114+from a filename you could make a separate file that creates such an115+application from an environment variable.116+117+For instance this could be a file named ``autoapp.py`` with these118+contents::119+120+ import os121+ from yourapplication import create_app122+ app = create_app(os.environ['YOURAPPLICATION_CONFIG'])123+124+Once this has happened you can make the flask command automatically pick125+it up::126+127+ export YOURAPPLICATION_CONFIG=/path/to/config.cfg128+ export FLASK_APP=/path/to/autoapp.py129+130+From this point onwards ``flask`` will find your application.modified:flask/cli.py+5 added
61 unmodified lines62 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)72 73 dirpath = filename74 while 1:282 unmodified lines