使用达世币和普洛利实时更新表值



我正在尝试用Python构建一个dash应用程序来模拟Q-Learning问题。在实现算法之前,我只专注于使表格随机递增值并在每次递增之间等待 1 秒。

Q 是这里的熊猫数据帧:

table = ff.create_table(Q,  height_constant=20)
table.layout.width=300
def update_Q(Q):
for i in range(len(Q)):
for j in range(1, len(Q.columns)):        
Q.iloc[i,j] += np.random.choice([0,1,2])
print(Q)
return Q

我能够使其与该 print 语句一起使用,控制台上表的值确实正在更新。

但是,在浏览器中,它只是第一次更新,但随后保持静态。这是代码:

# Browser visualization
app.layout = html.Div([
html.H1(children='Frozen Lake: Q-Learning Demo'),
dcc.Graph(id='table', figure=table),
dcc.Interval(
id='time',
interval=1*1000, # in milliseconds
n_intervals=0)
]
)

@app.callback(Output(component_id = 'table', component_property='figure'),
[Input(component_id = 'time', component_property='n_intervals')])    
def update_table(n):   
# Update values
new_table = ff.create_table(update_Q(Q))
time.sleep(1)
return new_table

if __name__ == '__main__':
app.run_server()

我错过了什么?

解决了。没有什么比早上的咖啡馆更像了;)

最好将表的创建包装到一个函数中,并在每个时间间隔为每次更新调用它。此外,以前的语法不会保留在创建的第一个表中定义的样式。

# Helper functions to draw and update values of the table
def draw_Table(Q):
table = ff.create_table(Q, index=True, height_constant=20)
table.layout.width=300
return table
def update_Q(Q):
for i in range(len(Q)):
for j in range(1, len(Q.columns)):        
Q.iloc[i,j] += np.random.choice([0,1,2])
return Q

然后

@app.callback(Output(component_id = 'table', component_property='figure'),
[Input(component_id = 'time', component_property='n_intervals')])    
def update_table(n):   
# Update values
new_table = draw_Table(update_Q(Q))
time.sleep(1)
return new_table

希望它对某人有所帮助!

最新更新