破折号-破折号呼叫如何起作用

时间:2020-07-01 08:07:25

标签: python plotly-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()

1 个答案:

答案 0 :(得分:1)

Dash使用@app.callback装饰器在后台进行操作。每当Inputs更改时,该函数都会被调用,Dash将从回调中获取返回的值以更新输出。