Python 3.x 使用Dash创建仪表板

Python 3.x 使用Dash创建仪表板,python-3.x,pandas,plotly-dash,Python 3.x,Pandas,Plotly Dash,当我运行下面的代码时,函数程序_exe.update_data会执行两次。避免这种情况的最好方法是什么?该函数执行起来比较耗时,因此运行两次并不理想。如有任何建议,将不胜感激 app = dash.Dash(__name__) server = app.server dict_main = program_exe.update_data() #this creates a nested dictionary rpm = list(dict_main.keys()) channels = dic

当我运行下面的代码时,函数程序_exe.update_data会执行两次。避免这种情况的最好方法是什么?该函数执行起来比较耗时,因此运行两次并不理想。如有任何建议,将不胜感激

app = dash.Dash(__name__)
server = app.server

dict_main = program_exe.update_data() #this creates a nested dictionary
rpm = list(dict_main.keys())
channels = dict_main[rpm[0]]

app.layout = html.Div(
    [
        html.Div([
            dcc.Dropdown(
                id='rpm-dropdown',
                options=[{'label': speed, 'value': speed} for speed in rpm],
                value=list(dict_main.keys())[0],
                # I removed the multi=True because it requires a distinction between the columns in the next dropdown...
                searchable=False
            ),
        ], style={'width': '20%', 'display': 'inline-block'}),
        html.Div([
            dcc.Dropdown(
                id='channel-dropdown',
                multi=True
            ),
        ], style={'width': '20%', 'display': 'inline-block'}
        ),
        html.Div([
            dcc.Graph(
                id='Main-Graph'  # the initial graph is in the callback
            ),
        ], style={'width': '98%', 'display': 'inline-block'}
        )
    ]
)


@app.callback(
    Output('channel-dropdown', 'options'),
    [Input('rpm-dropdown', 'value')])
def update_date_dropdown(speed):
    return [{'label': i, 'value': i} for i in dict_main[speed]]


@app.callback(
    Output('Main-Graph', 'figure'),
    [Input('channel-dropdown', 'value')],
    [State('rpm-dropdown', 'value')])  # This is the way to inform the callback which dataframe is to be charted
def updateGraph(channels, speed):
    if channels:
        # return the entire figure with the different traces
        return go.Figure(data=[go.Scatter(x=dict_main[speed]['Manager'], y=dict_main[speed][i]) for i in channels])
    else:
    # at initialization the graph is returned empty
        return go.Figure(data=[])


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





您可以使用缓存只命中函数一次。有关更多详细信息,请参阅

简短示例:

从缓存导入缓存 cache=Cacheapp.server,配置={ “缓存类型”:“文件系统”, “缓存目录”:“缓存目录”, } @cache.memoizetimeout=6000 def my_cached_函数: 返回程序\u exe.update\u数据
我没有你的func,但当我用占位符在本地测试它时,它最初被调用了两次,但在添加缓存后只调用了一次。

效果很好!非常感谢。