如何从Flask @ app.route打印到python控制台 [英] How to print from Flask @app.route to python console

查看:2568
本文介绍了如何从Flask @ app.route打印到python控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的天真方法:

p>

This is my naive approach:

@app.route('/button/')
def button_clicked():
    print 'Hello world!'
    return redirect('/')

背景:我想执行其他python命令从瓶(不壳)。 打印应该是最简单的情况。
我相信我还没有理解这里的一个基本的转折点。
在此先感谢!

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!

推荐答案

好像你已经解决了这个问题,一个简单的方法来做到这一点是通过印刷到stderr。你可以这样做:

It seems like you have it worked out, but for others looking for this answer, 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将在控制台中显示打印到stderr的东西。有关打印到标准错误的其他方法,请参阅此计算器上的帖子

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

这篇关于如何从Flask @ app.route打印到python控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆