Fix Python – How to print from Flask @app.route to python console


Asked By – Robert Filter

I would like to simply print a “hello world” to the python console after /button is called by the user.

This is my naive approach:

def button_clicked():
    print 'Hello world!'
    return redirect('/')

Background: I would like to execute other python commands from flask (not shell). “print” should be the easiest case.
I believe I have not understood a basic twist here.
Thanks in advance!

Now we will see solution for issue: How to print from Flask @app.route to python console


An easy way to do this is by printing to stderr. You can do that like this:

from __future__ import print_function # In python 2.7
import sys

def button_clicked():
    print('Hello world!', file=sys.stderr)
    return redirect('/')

Flask will display things printed to stderr in the console. For other ways of printing to stderr, see this stackoverflow post

This question is answered By – Gabe

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0