edit some cli messages

dev server message doesn't show one of the lines in grey
app.run message uses click.secho instead of warning
This commit is contained in:
David Lord 2022-06-15 12:45:22 -07:00
parent 97298e06fe
commit 4f03a769d4
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
3 changed files with 17 additions and 29 deletions

View file

@ -10,6 +10,7 @@ from itertools import chain
from threading import Lock from threading import Lock
from types import TracebackType from types import TracebackType
import click
from werkzeug.datastructures import Headers from werkzeug.datastructures import Headers
from werkzeug.datastructures import ImmutableDict from werkzeug.datastructures import ImmutableDict
from werkzeug.exceptions import Aborter from werkzeug.exceptions import Aborter
@ -23,6 +24,7 @@ from werkzeug.routing import MapAdapter
from werkzeug.routing import RequestRedirect from werkzeug.routing import RequestRedirect
from werkzeug.routing import RoutingException from werkzeug.routing import RoutingException
from werkzeug.routing import Rule from werkzeug.routing import Rule
from werkzeug.serving import is_running_from_reloader
from werkzeug.urls import url_quote from werkzeug.urls import url_quote
from werkzeug.utils import redirect as _wz_redirect from werkzeug.utils import redirect as _wz_redirect
from werkzeug.wrappers import Response as BaseResponse from werkzeug.wrappers import Response as BaseResponse
@ -908,12 +910,18 @@ class Flask(Scaffold):
The default port is now picked from the ``SERVER_NAME`` The default port is now picked from the ``SERVER_NAME``
variable. variable.
""" """
# Change this into a no-op if the server is invoked from the # Ignore this call so that it doesn't start another server if
# command line. Have a look at cli.py for more information. # the 'flask run' command is used.
if os.environ.get("FLASK_RUN_FROM_CLI") == "true": if os.environ.get("FLASK_RUN_FROM_CLI") == "true":
from .debughelpers import explain_ignored_app_run if not is_running_from_reloader():
click.secho(
" * Ignoring a call to 'app.run()', the server is"
" already being run with the 'flask run' command.\n"
" Only call 'app.run()' in an 'if __name__ =="
' "__main__"\' guard.',
fg="red",
)
explain_ignored_app_run()
return return
if get_load_dotenv(load_dotenv): if get_load_dotenv(load_dotenv):

View file

@ -11,6 +11,7 @@ from threading import Lock
from threading import Thread from threading import Thread
import click import click
from werkzeug.serving import is_running_from_reloader
from werkzeug.utils import import_string from werkzeug.utils import import_string
from .globals import current_app from .globals import current_app
@ -273,7 +274,7 @@ class DispatchingApp:
self._bg_loading_exc = None self._bg_loading_exc = None
if use_eager_loading is None: if use_eager_loading is None:
use_eager_loading = os.environ.get("WERKZEUG_RUN_MAIN") != "true" use_eager_loading = not is_running_from_reloader()
if use_eager_loading: if use_eager_loading:
self._load_unlocked() self._load_unlocked()
@ -546,12 +547,6 @@ class FlaskGroup(AppGroup):
return sorted(rv) return sorted(rv)
def main(self, *args, **kwargs): def main(self, *args, **kwargs):
# Set a global flag that indicates that we were invoked from the
# command line interface. This is detected by Flask.run to make the
# call into a no-op. This is necessary to avoid ugly errors when the
# script that is loaded here also attempts to start a server.
os.environ["FLASK_RUN_FROM_CLI"] = "true"
if get_load_dotenv(self.load_dotenv): if get_load_dotenv(self.load_dotenv):
load_dotenv() load_dotenv()
@ -637,7 +632,7 @@ def show_server_banner(env, debug, app_import_path, eager_loading):
"""Show extra startup messages the first time the server is run, """Show extra startup messages the first time the server is run,
ignoring the reloader. ignoring the reloader.
""" """
if os.environ.get("WERKZEUG_RUN_MAIN") == "true": if is_running_from_reloader():
return return
if app_import_path is not None: if app_import_path is not None:
@ -653,10 +648,10 @@ def show_server_banner(env, debug, app_import_path, eager_loading):
if env == "production": if env == "production":
click.secho( click.secho(
" WARNING: This is a development server. Do not use it in" " WARNING: This is a development server. Do not use it in"
" a production deployment.", " a production deployment.\n Use a production WSGI server"
" instead.",
fg="red", fg="red",
) )
click.secho(" Use a production WSGI server instead.", dim=True)
if debug is not None: if debug is not None:
click.echo(f" * Debug mode: {'on' if debug else 'off'}") click.echo(f" * Debug mode: {'on' if debug else 'off'}")

View file

@ -1,6 +1,4 @@
import os
import typing as t import typing as t
from warnings import warn
from .app import Flask from .app import Flask
from .blueprints import Blueprint from .blueprints import Blueprint
@ -159,16 +157,3 @@ def explain_template_loading_attempts(app: Flask, template, attempts) -> None:
info.append(" See https://flask.palletsprojects.com/blueprints/#templates") info.append(" See https://flask.palletsprojects.com/blueprints/#templates")
app.logger.info("\n".join(info)) app.logger.info("\n".join(info))
def explain_ignored_app_run() -> None:
if os.environ.get("WERKZEUG_RUN_MAIN") != "true":
warn(
Warning(
"Silently ignoring app.run() because the application is"
" run from the flask command line executable. Consider"
' putting app.run() behind an if __name__ == "__main__"'
" guard to silence this warning."
),
stacklevel=3,
)