Browse Source

Merge pull request #2591 from pallets/run-behavior

match run command behavior in app.run
pull/2592/head
David Lord 7 years ago committed by GitHub
parent
commit
f2cf4d0449
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 27
      flask/app.py
  2. 70
      flask/cli.py

27
flask/app.py

@ -864,21 +864,34 @@ class Flask(_PackageBoundObject):
If installed, python-dotenv will be used to load environment
variables from :file:`.env` and :file:`.flaskenv` files.
.. versionchanged:: 0.10
The default port is now picked from the ``SERVER_NAME`` variable.
If set, the :envvar:`FLASK_ENV` and :envvar:`FLASK_DEBUG`
environment variables will override :attr:`env` and
:attr:`debug`.
Threaded mode is enabled by default.
.. versionchanged:: 0.10
The default port is now picked from the ``SERVER_NAME``
variable.
"""
# Change this into a no-op if the server is invoked from the
# command line. Have a look at cli.py for more information.
# command line. Have a look at cli.py for more information.
if os.environ.get('FLASK_RUN_FROM_CLI') == 'true':
from .debughelpers import explain_ignored_app_run
explain_ignored_app_run()
return
if load_dotenv:
from flask.cli import load_dotenv
load_dotenv()
cli.load_dotenv()
# if set, let env vars override previous values
if 'FLASK_ENV' in os.environ:
self.env = get_env()
self.debug = get_debug_flag()
elif 'FLASK_DEBUG' in os.environ:
self.debug = get_debug_flag()
# debug passed to method overrides all other sources
if debug is not None:
self.debug = bool(debug)
@ -892,8 +905,12 @@ class Flask(_PackageBoundObject):
host = host or sn_host or _host
port = int(port or sn_port or _port)
options.setdefault('use_reloader', self.debug)
options.setdefault('use_debugger', self.debug)
options.setdefault('threaded', True)
cli.show_server_banner(self.env, self.debug, self.name)
from werkzeug.serving import run_simple

70
flask/cli.py

@ -577,6 +577,28 @@ def load_dotenv(path=None):
return new_dir is not None # at least one file was located and loaded
def show_server_banner(env, debug, app_import_path):
"""Show extra startup messages the first time the server is run,
ignoring the reloader.
"""
if os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
return
if app_import_path is not None:
print(' * Serving Flask app "{0}"'.format(app_import_path))
print(' * Environment: {0}'.format(env))
if env == 'production':
click.secho(
' WARNING: Do not use the development server in a production'
' environment.', fg='red')
click.secho(' Use a production WSGI server instead.', dim=True)
if debug is not None:
print(' * Debug mode: {0}'.format('on' if debug else 'off'))
@click.command('run', short_help='Runs a development server.')
@click.option('--host', '-h', default='127.0.0.1',
help='The interface to bind to.')
@ -596,51 +618,31 @@ def load_dotenv(path=None):
@pass_script_info
def run_command(info, host, port, reload, debugger, eager_loading,
with_threads):
"""Runs a local development server for the Flask application.
"""Run a local development server.
This local server is recommended for development purposes only but it
can also be used for simple intranet deployments. By default it will
not support any sort of concurrency at all to simplify debugging. This
can be changed with the --with-threads option which will enable basic
multithreading.
This server is for development purposes only. It does not provide
the stability, security, or performance of production WSGI servers.
The reloader and debugger are by default enabled if the debug flag of
Flask is enabled and disabled otherwise.
The reloader and debugger are enabled by default if
FLASK_ENV=development or FLASK_DEBUG=1.
"""
from werkzeug.serving import run_simple
if get_env() == 'production':
click.secho('Warning: Detected a production environment. Do not '
'use `flask run` for production use.',
fg='red')
click.secho('Use a production ready WSGI server instead',
dim=True)
debug = get_debug_flag()
if reload is None:
reload = bool(debug)
reload = debug
if debugger is None:
debugger = bool(debug)
debugger = debug
if eager_loading is None:
eager_loading = not reload
show_server_banner(get_env(), debug, info.app_import_path)
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
# Extra startup messages. This depends a bit on Werkzeug internals to
# not double execute when the reloader kicks in.
if os.environ.get('WERKZEUG_RUN_MAIN') != 'true':
# If we have an import path we can print it out now which can help
# people understand what's being served. If we do not have an
# import path because the app was loaded through a callback then
# we won't print anything.
if info.app_import_path is not None:
print(' * Serving Flask app "%s"' % info.app_import_path)
print(' * Env %s' % get_env())
if debug is not None:
print(' * Forcing debug mode %s' % (debug and 'on' or 'off'))
run_simple(host, port, app, use_reloader=reload,
use_debugger=debugger, threaded=with_threads)
from werkzeug.serving import run_simple
run_simple(host, port, app, use_reloader=reload, use_debugger=debugger,
threaded=with_threads)
@click.command('shell', short_help='Runs a shell in the app context.')

Loading…
Cancel
Save