Python 在页面刷新时更新绘图划线图
新的阴谋冲刺。 我正在完成关于我的简单示例的教程。学习如何在向数据框(本例中为两个数据框)添加新数据时更新图形,以及如何将其与仪表板上的下拉列表相连接 我希望在每次页面加载或页面刷新时使用新数据更新我的图表(因为我每天只有几次更新) 这是我正在研究的代码:Python 在页面刷新时更新绘图划线图,python,graph,updates,plotly-dash,Python,Graph,Updates,Plotly Dash,新的阴谋冲刺。 我正在完成关于我的简单示例的教程。学习如何在向数据框(本例中为两个数据框)添加新数据时更新图形,以及如何将其与仪表板上的下拉列表相连接 我希望在每次页面加载或页面刷新时使用新数据更新我的图表(因为我每天只有几次更新) 这是我正在研究的代码: import pandas as pd import plotly.express as px import dash import dash_core_components as dcc import dash_html_component
import pandas as pd
import plotly.express as px
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
app = dash.Dash(__name__)
data = [['Blue', 20], ['Red ', 12], ['Green', 33]]
df = pd.DataFrame(data, columns=['Color', 'Number'])
data1 = [['A', 10, 88], ['B ', 50, 45], ['C', 25, 120]]
df1 = pd.DataFrame(data1, columns=['Letter', 'Column1', 'Column2'])
fig = px.bar(df, x=df['Color'], y=df['Number'])
fig1 = px.line(x=df1['Letter'], y=df1['Column1'], color=px.Constant('Column1'),
labels=dict(x='Letter', y='Column1', color='Letter'))
fig1.add_bar(x=df1['Letter'], y=df1['Column2'], name='Letter')
app.layout = html.Div(children=[
html.H1(children='Colors and Letters', style={'text-align': 'center'}),
html.Div(children='Color', style={'text-align': 'center'}),
html.Div([
html.Label(['Choose a graph:'], style={'font-weight': 'bold'}),
dcc.Dropdown(
id='dropdown',
options=[
{'label': 'Colors', 'value': 'graph1'},
{'label': 'Letters', 'value': 'graph2'},
],
value='graph1',
style={"width": "60%"}),
html.Div(dcc.Graph(id='graph')),
]),
])
@app.callback(
Output('graph', 'figure'),
[Input(component_id='dropdown', component_property='value')]
)
def select_graph(value):
if value == 'graph1':
return fig
else:
return fig1
if __name__ == '__main__':
app.run_server(debug=True)
任何帮助都将不胜感激。提前感谢。如上一节所述,您应该能够通过定义创建布局的函数来实现所需的行为
def layout():
return html.Div(...)
并将此功能指定为应用程序布局
app.layout = layout # note no (), you must assign the function itself, not the layout