Skip to content
Myr

Convert Flask.run into a noop when run from the 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 42fbbb4 · Browse files · 4 files+30 added−0 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+30 added−0 removed

  • other:+30 added−0 removed

4 files+30 added−0 removed

modified:flask/app.py+7 added
822 unmodified lines
823 Werkzeug server. See824 :func:`werkzeug.serving.run_simple` for more825 information.826 """827 # Change this into a no-op if the server is invoked from the828 # command line. Have a look at cli.py for more information.829 if os.environ.get('FLASK_RUN_FROM_CLI_SERVER') == '1':830 from .debughelpers import explain_ignored_app_run831 explain_ignored_app_run()832 return833834 from werkzeug.serving import run_simple835 _host = '127.0.0.1'836 _port = 5000837 server_name = self.config.get("SERVER_NAME")
1170 unmodified lines
modified:flask/cli.py+7 added
410 unmodified lines
411 Flask is enabled and disabled otherwise.412 """413 from werkzeug.serving import run_simple414415 # Set a global flag that indicates that we were invoked from the416 # command line interface provided server command. This is detected417 # by Flask.run to make the call into a no-op. This is necessary to418 # avoid ugly errors when the script that is loaded here also attempts419 # to start a server.420 os.environ['FLASK_RUN_FROM_CLI_SERVER'] = '1'421422 debug = get_debug_flag()423 if reload is None:424 reload = bool(debug)425 if debugger is None:
99 unmodified lines
modified:flask/debughelpers.py+12 added
6 unmodified lines
78 :copyright: (c) 2015 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""11import os12from warnings import warn1314from ._compat import implements_to_string, text_type15from .app import Flask16from .blueprints import Blueprint17from .globals import _request_ctx_stack
137 unmodified lines
155 info.append(' Maybe you did not place a template in the right folder?')156 info.append(' See http://flask.pocoo.org/docs/blueprints/#templates')157158 app.logger.info('\n'.join(info))159160161def explain_ignored_app_run():162 if os.environ.get('WERKZEUG_RUN_MAIN') != 'true':163 warn(Warning('Silently ignoring app.run() because the '164 'application is run from the flask command line '165 'executable. Consider putting app.run() behind an '166 'if __name__ == "__main__" guard to silence this '167 'warning.'), stacklevel=3)
modified:CHANGES+4 added
6 unmodified lines
7------------89Major release, unreleased1011- Make `app.run()` into a noop if a Flask application is run from the12 development server on the command line. This avoids some behavior that13 was confusing to debug for newcomers.1415Version 0.12.116--------------1718Bugfix release, unreleased
604 unmodified lines