Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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')
@renmak
Copy link

renmak commented Jul 2, 2020

Thank you very much for this. :)

@rgdevx
Copy link

rgdevx commented Aug 24, 2020

The above didn't work for me, but this did:

$ pip install uwsgi
$ uwsgi --http :5000 --callable app --wsgi-file myapp.py

@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