close

How to print from Flask @app.route to python console

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to print from Flask @app.route to python console in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to print from Flask @app.route to python console?

  1. 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

  2. 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

Method 1

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

@app.route('/button/')
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

Method 2

We can also use logging to print data on the console.

Example:

import logging
from flask import Flask

app = Flask(__name__)

@app.route('/print')
def printMsg():
    app.logger.warning('testing warning log')
    app.logger.error('testing error log')
    app.logger.info('testing info log')
    return "Check your console"

if __name__ == '__main__':
    app.run(debug=True)

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read