【问题标题】:update values with function call to Flask使用对 Flask 的函数调用来更新值
【发布时间】:2014-08-12 16:49:38
【问题描述】:

我需要使用以下内容更新实时数据,但 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

【问题讨论】:

  • 题主无需重复。最好包含有机地 使用的技术,而不是作为特定前缀。主标记(此处为javascript)已包含在 Google 使用的页面标题中。
  • 您的意思是,您希望每次请求时都获得一个随机值?

标签: javascript python ajax flask jinja2


【解决方案1】:

当您将@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.) ...

【讨论】:

  • 仍然得到相同的值。
  • 在我看来,您每次都在发送相同的查询。如果您想获得不同的结果,则需要更改查询。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-31
  • 1970-01-01
  • 2015-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
相关资源
最近更新 更多