通过对Flask的函数调用更新值

update values with function call to Flask

本文关键字:函数调用 更新 Flask      更新时间:2023-09-26

我需要用以下内容更新实时数据,但是index()get_data()在程序中只被调用一次。

我如何多次返回值,以便当我渲染模板时,它每次都接收不同的值

@app.route('/', methods=['GET'])
def index():
    value = get_data()
    print "index", value
    return render_template('index.html', session_value=value)

@app.route('/get_data', methods=['GET'])
def get_data():
    df = sqlio.read_sql(qry, conn)
    value = df['count'][0]
    print value
    return value

当你把@app.route作为装饰器时,它会把它绑定为应用程序中的路由。稍后调用它不会产生您想要的效果——它调用的是装饰器,而不是函数本身。我会把你的代码改成这样:

def get_data():
    df = sqlio.read_sql(qry, conn)
    value = df['count'][0]
    print value
    return value
@app.route('/', methods=['GET'])
def index():
    value = get_data()
    print "index", value
    return render_template('index.html', session_value=value)

@app.route('/get_data', methods=['GET'])
def get_data_route():
    value = get_data()
    # ... display your data somehow (HTML, JSON, etc.) ...