Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jerblack/735b9953ba1ab6234abb43174210d356 to your computer and use it in GitHub Desktop.
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.
"""
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
Copy link

fagci commented Jan 10, 2021

from flask import cli
cli.show_server_banner = lambda *_: None

@bw2
Copy link

bw2 commented Apr 5, 2021

Another way:

import os
os.environ["WERKZEUG_RUN_MAIN"] = "true"
app.run(...)

@TyLaneTech
Copy link

@bw2's solution worked for me. Cheers!

@hemnstill
Copy link

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)

@SamarthaMJ
Copy link

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