gpt4 book ai didi

python - 通过回调更新 Dash 数据表

转载 作者:行者123 更新时间:2023-12-05 02:58:30 48 4
gpt4 key购买 nike

我想根据时间间隔更新我的 Dash data_table。它会运行,但没有任何更新。我不认为这应该很难,但我显然遗漏了一些东西。这是我所拥有的:

import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import dash_table
import time
import pandas as pd

数据组件

def getData():
"""
Only can hit api every 6 seconds for 14 total items,
which is relevant towards the Dash interval
"""
df = pd.DataFrame()
for item in list:
data = *hit api*
df = df.append(data)
time.sleep(7)
return df

破折号组件

app = dash.Dash(__name__)

# passing in just column names to avoid needlessly hitting API
tblcols=[{'name': 'col1', 'id': 'col1'},
{'name': 'col2', 'id': 'col2'},
{'name': 'col3', 'id': 'col3'}
]

app.layout = html.Div([
html.H4('Dashboard Name'),
dcc.Interval('graph-update',interval = 500000, n_intervals = 0),
dash_table.DataTable(
id = 'table',
data = [{}],
columns=tblcols )])

@app.callback(
Output('table','data'),
[Input('graph-update', 'n_intervals')]
)
def updateTable(n):

"""
calling the get data function
"""

updated_data = getData()
return updated_data.to_dict('records')

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

同样,这将运行并返回我传递的列标题,但是当我看到更新已在控制台“POST/_dash-update-component HTTP/1.1”中运行时,没有任何变化

最佳答案

我认为问题在于您返回了一个空的 dataframe。这是因为您执行的是 df.append(data) 而不是 df = df.append(data)

这是一个简化的工作代码 (Dash v1.6.0):

import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table
import pandas as pd
import numpy as np

app = dash.Dash(__name__)

def getData():
df = pd.DataFrame()
for idx in range(10):
data = {'x': np.random.random(1)[0], 'y': np.random.random(1)[0]}
df = df.append(data, ignore_index=True)
return df.to_dict('records')

tblcols=[{'name': 'x', 'id': 'x'},
{'name': 'y', 'id': 'y'}, ]

app.layout = html.Div([
html.H4('Dashboard'),
dcc.Interval('graph-update', interval = 1000, n_intervals = 0),
dash_table.DataTable(
id = 'table',
data = getData(),
columns=tblcols)])

@app.callback(
dash.dependencies.Output('table','data'),
[dash.dependencies.Input('graph-update', 'n_intervals')])
def updateTable(n):
return getData()

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

enter image description here

关于python - 通过回调更新 Dash 数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59002262/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com