如何使用Flask显示打印数据

时间:2017-10-20 19:24:27

标签: python flask

我有这个python代码,我试图在静态页面上显示数据 用烧瓶代替控制台。

from __future__ import print_function
from private import private, private, private
optimizeit = get_optimizeit(web.site, private.private)
optimizeit.load_data_from_CSV("/path.../to..cvs.csv")
data = optimizeit.get_data_by_name('somename')
data = optimizeit.data[0]  
data.max_exposure = 0.5
generatedata = optimizeit.optimizeit(4)
for datafield in generatedata:
    print (datafield)

在哪里打印我想将其打印到简单的烧瓶页面。我尝试了很少的东西,我想不出最好的方式。

编辑:我尝试了什么

from __future__ import print_function
import flask
from private import private, private, private
import time
app = flask.Flask(__name__)

@app.route('/sitea')
def index():
    def inner():
        optimizeit = get_optimizit(website.site12, private.someprivate)
        optimizer.load_players_from_CSV("/mypath to csv.../.csv") #import csv 
        data = optimizeit.datas[0]  #optimize that data
        data.max_exposure = 0.5 #set some exposure to that data 
        data_generator = optimizeit.optimizeit(4)
        for datalive in datalive_generator:
            return datalive
    return flask.Response(inner(), mimetype='text/html')  # text/html is required for most browsers to show the partial page immediately

app.run(debug=True)

编辑2:这工作了!

from __future__ import print_function
import flask
from private import private, private, private
import time
app = flask.Flask(__name__)

@app.route('/sitea')
def index():
    def inner():
        optimizeit = get_optimizit(website.site12, private.someprivate)
        optimizer.load_players_from_CSV("/mypath to csv.../.csv") #import csv 
        data = optimizeit.datas[0]  #optimize that data
        data.max_exposure = 0.5 #set some exposure to that data 
        data_generator = optimizeit.optimizeit(4)
        for datalive in datalive_generator:
            yield '%s<br/>\n' % datalive
    return flask.Response(inner(), mimetype='text/html')  # text/html is required for most browsers to show the partial page immediately

1 个答案:

答案 0 :(得分:0)

下面

    for datalive in datalive_generator:
        return datalive

这只返回datalive_generator中的第一项,然后退出该函数,永不返回。你可能意味着yield datalive。这样它将继续将输出流输出到响应。与此同时,您希望在Python中搜索生成器和普通函数之间的区别。