re-add flag to skip unguarded app.run from CLI

This commit is contained in:
David Lord 2022-06-29 21:11:58 -07:00
parent ab36542260
commit 5544d09477
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
3 changed files with 9 additions and 7 deletions

View file

@ -916,8 +916,8 @@ class Flask(Scaffold):
if os.environ.get("FLASK_RUN_FROM_CLI") == "true":
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"
" * Ignoring a call to 'app.run()' that would block"
" the current 'flask' CLI command.\n"
" Only call 'app.run()' in an 'if __name__ =="
' "__main__"\' guard.',
fg="red",

View file

@ -715,6 +715,11 @@ class FlaskGroup(AppGroup):
parent: click.Context | None = None,
**extra: t.Any,
) -> click.Context:
# Set a flag to tell app.run to become a no-op. If app.run was
# not in a __name__ == __main__ guard, it would start the server
# when importing, blocking whatever command is being called.
os.environ["FLASK_RUN_FROM_CLI"] = "true"
# Attempt to load .env and .flask env files. The --env-file
# option can cause another file to be loaded.
if get_load_dotenv(self.load_dotenv):