简体   繁体   English

python flask变量不会更新

[英]python flask variables won't update

My flask program (simulation in the view) runs in the following order (detailed code is also attached): 我的flask程序(视图中的仿真)按以下顺序运行(还附有详细代码):

1> read my variable 'tx_list' from session. 1>从会话中读取我的变量“ tx_list”。 tx_list = session.get('tx_list', None) tx_list = session.get('tx_list',无)

2> for t in tx_list: do someting with t. 2> tx_list中的t:用t做点事。

3> store tx_list in session: session['tx_list'] = tx_list 3>将tx_list存储在会话中:session ['tx_list'] = tx_list

The reason I use session is because I want to change 'tx_list' every time I invoke this 'simulation' function. 我使用会话的原因是因为我想在每次调用此“模拟”功能时更改“ tx_list”。

The problem now is that if I print (console.log(tx_list)) in the front-end, it only updates itself a few times. 现在的问题是,如果我在前端打印(console.log(tx_list)),它只会自我更新几次。 But in the same time, when I print the values in the simulation function, it always updates. 但是同时,当我在模拟函数中打印值时,它总是更新。 So I suspect the problem is because of the session??? 所以我怀疑问题是因为会议造成的???

I've tried to add another 'time_now' variable in the simulation function, which is independent of session. 我试图在模拟函数中添加另一个“ time_now”变量,该变量与会话无关。 Then in the front-end (html) always updates 'time_now'. 然后在前端(html)中始终更新“ time_now”。 So the problem must be because of the usage of session??? 所以问题一定是因为会话的使用??? How can update my 'tx_list' if session is not the best way to do it? 如果会话不是最好的方式,如何更新我的“ tx_list”?

-------------------code is below---------------------------- -------------------代码如下----------------------------

My view is like below: In my view, I simply read my var 'tx_list' from session, do something with it, then store it back to the session. 我的看法如下:在我看来,我只是从会话中读取var'tx_list',对其进行处理,然后将其存储回会话中。

@app.route('/simulation/<param>')
def simulation(param):
    tx_list = session.get('tx_list', None)
    today = date.today()
    if t0 == '0':
        time_now = today.strftime("%Y-%m-%d %H")
    else:
        time_now = (today + relativedelta(hours=int(param))).strftime("%Y-%m-%d %H")

    return_val = jsonify({'time':time_now, 'tx_list':tx_list_0})
    for t in tx_list:
        ###########I have my code here to change t.
        print(t)
    session['tx_list'] = tx_list
    return return_val

problem solved once I installed Flask-Session and initilize it. 我安装Flask-Session并将其初始化后,问题就解决了。

I feel puzzled why it updates OK for only a few times without installing the module. 我感到困惑,为什么在不安装模块的情况下它只能更新几次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM