Last active
May 11, 2023 05:35
-
-
Save jerblack/735b9953ba1ab6234abb43174210d356 to your computer and use it in GitHub Desktop.
Disable ability for Flask to display warning about using a development server in a production environment.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Flask will display a warning everytime you startup your application if you are not using it in behind a separate WSGI server. | |
* Environment: production | |
WARNING: Do not use the development server in a production environment. | |
Use a production WSGI server instead. | |
This was not relevant for my scenario and I wanted the message gone, | |
so using this method will remove their ability to print that message | |
This method replaces the function they use to write the banner with a no-op. | |
Be aware that this prevents the rest of the Flask startup banner from displaying as well. | |
The only output you will see from the method below is: | |
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit) | |
""" | |
from flask import Flask | |
import sys | |
cli = sys.modules['flask.cli'] | |
cli.show_server_banner = lambda *x: None | |
app = Flask(__name__) | |
app.run(host='0.0.0.0', port='80') |
fagci
commented
Jan 10, 2021
•
Another way:
import os
os.environ["WERKZEUG_RUN_MAIN"] = "true"
app.run(...)
@bw2's solution worked for me. Cheers!
Yet another way (work for versions >2.1.x):
def _ansi_style_supressor(func: Callable[..., Any]) -> Callable[..., Any]:
@functools.wraps(func)
def wrapper(*args: Tuple[Any, ...], **kwargs: Dict[str, Any]) -> Any:
if args and isinstance(args[0], str) and args[0].startswith('WARNING: This is a development server.'):
return ''
return func(*args, **kwargs)
return wrapper
...
werkzeug.serving._ansi_style = _ansi_style_supressor(werkzeug.serving._ansi_style)
cli.show_server_banner = lambda *_: None
that works for me
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment