Plotly Dash 中的回调错误更新图 - 下拉列表和输出图

Callback Error updating figure in Plotly Dash - Dropdown and Output Figure

所以今天,我正在尝试使用来自多个数据帧的数据创建一个简单的仪表板。我使用下拉菜单 select 数据框显示在图中。但是,当我 运行 这段代码时,我在网页中收到回调错误

Callback error updating loglog.figure
Traceback (most recent call last):
  File "C:\Users\nahar\AppData\Local\Temp/ipykernel_1556/982666652.py", line 63, in build_graph
    
TypeError: string indices must be integers

我不明白为什么会这样,这是代码

logs = ['CALI','RDEP','GR','RHOB','NPHI','SP','DTC']
colors = ['black','firebrick','green','mediumaquamarine','royalblue','goldenrod','lightcoral']
log_cols = np.arange(1,8)


app = dash.Dash(__name__, external_stylesheets=[dbc.themes.SANDSTONE], meta_tags=[
        {"name": "viewport", "content": "width=device-width, initial-scale=1"}
    ])

server = app.server
app.config.suppress_callback_exceptions = True

app.layout = dbc.Container([
    dbc.Row([
        dbc.Col(html.H1('FORCE 2020 Well Log Challange Dashboard',
                        className='text-center mb-4'),
               width=12)
    ]),

    dbc.Row([
        dbc.Col([
            dcc.Dropdown(id='droplog',
                        options=[
                            {'label':'15/9-13','value':'well1'},
                            {'label':'15/9-15','value':'well2'},
                            {'label':'15/9-17','value':'well3'},
                            {'label':'16/1-2','value':'well4'},
                            {'label':'16/1-6 A','value':'well5'},
                            {'label':'16/10-1','value':'well6'},
                            {'label':'16/10-2','value':'well7'},
                            {'label':'16/10-3','value':'well8'},
                            {'label':'16/10-5','value':'well9'},
                            {'label':'16/11-1 ST3','value':'well10'},
                            {'label':'16/2-11 A','value':'well11'},
                            {'label':'16/2-16','value':'well12'}
                        ], multi=False, value='well1'),
            dcc.Graph(id='loglog',figure={})
        ])
        
    ]),

    dbc.Row([
        
    ])
])

@app.callback(
    Output(component_id='loglog',component_property='figure'),
    [Input(component_id='droplog',component_property='value')]
)

def build_graph(plot_chosen):
    logplot = make_subplots(rows=1, cols=len(logs), shared_yaxes=True)
    for i in range (len(logs)):
        
        if i == 1:
            logplot.add_trace(go.Scatter(x=plot_chosen[logs[i]], y=plot_chosen['DEPTH_MD'], 
                                         name=logs[i], line_color=colors[i]),row=1, col=log_cols[i])
            logplot.update_xaxes(type='log',row=1, col=log_cols[i], title_text=logs[i], 
                                 tickfont_size=12, linecolor='#585858')
        
        else:
            logplot.add_trace(go.Scatter(x=plot_chosen[logs[i]], y=plot_chosen['DEPTH_MD'], 
                                         name=logs[i], line_color=colors[i]),row=1, col=log_cols[i])
            logplot.update_xaxes(col=log_cols[i], title_text=logs[i],  linecolor='#585858')

    logplot.update_xaxes(showline=True, linewidth=2, linecolor='black', mirror=True, ticks='inside', tickangle= 0)
    logplot.update_yaxes(tickmode='linear', tick0=0,dtick=250, showline=True, linewidth=2, linecolor='black', 
                         mirror=True, ticks='outside')
    logplot.update_yaxes(row=1, col=1, autorange='reversed')
    logplot.update_layout(height=750, width=1200, showlegend=False)   
    logplot.update_layout(plot_bgcolor = "rgba(0,0,0,0)", paper_bgcolor = "rgba(0,0,0,0)")
    
    return logplot

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

并且使用的输入数据是很多数据帧的形式,其中一个是这样的 for well 1

我尝试 运行 仅定义函数,build_graph,它的效果非常好。结果如图here

我想我知道问题出在哪里了:变量 plot_chosen 是函数 build_graph 中的参数,它是一个字符串。因此您无法键入 y=plot_chosen['DEPTH_MD']。虽然在下拉菜单中选择 15/9-13 后它的值为 well1,但它并不代表数据帧,而是一个简单的字符串。尝试使用数据框

创建一个字典
dataframes = {'well1': well1, 'well2': well2, ...}

然后从字典中选择合适的DataFrame。
因此,例如,当用户在下拉列表中选择 15/9-13 时,您将获得 plot_chosen='well1',您可以使用 dataframes[plot_chosen] 简单地获取数据框 well1