我需要使用以下代码更新实时数据,但 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
作为装饰器时,它会将其绑定(bind)为应用程序内的路由。稍后调用它不会达到您想要的效果 - 它调用装饰器,而不是函数本身。我会将您的代码更改为如下所示:
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.) ...
关于javascript - 通过对 Flask 的函数调用更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25269807/