【发布时间】:2020-10-21 15:18:05
【问题描述】:
我正在关注 dash 教程,但完全不知道 dash 如何调用函数。在此页面上的第二个教程https://dash.plotly.com/basic-callbacks 之后。下面显示了相同的示例。我完全不知道在哪里调用了 update_figure 函数,但图表仍然绘制在仪表板中(即 app.layout 或 app.callback 中的任何地方都没有提到 update_figure() 函数)。
那么关于函数是如何传递的有什么想法吗?
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv')
# initialize
app = dash.Dash()
app.layout = html.Div([
dcc.Graph(id = 'graph-with-slider'),
dcc.Slider(
id = 'year-slider',
min = df['year'].min(),
max = df['year'].max(),
value = df['year'].min(),
marks = {str(year) : str(year) for year in df['year'].unique()},
step = None
)
])
@app.callback(
Output('graph-with-slider','figure'),
[Input('year-slider','value')]
)
def update_figure(selected_year):
filtered_df = df[df.year == selected_year]
fig = px.scatter(filtered_df, x = 'gdpPercap', y = 'lifeExp', size = 'pop', color = 'continent', hover_name = 'country', log_x = True, size_max = 55)
fig.update_layout(transition_duration = 500)
return fig
if __name__ == '__main__':
app.run_server()
【问题讨论】:
-
只是一个旁注,coralvanda 是正确的,但如果你的应用程序中有重要的状态,处理回调是一场噩梦。不幸的是,我不记得解决方案了,我只是想让你知道,如果你遇到这个问题,你并不孤单。
标签: python plotly-dash