Я пытаюсь создать приложение с несколькими вкладками на основе справочного кода из https://dash-bootstrap-components.opensource.faculty.ai/examples/graphs-in-tabs/#sourceCode
Мое требование - иметь кнопку внутри страницы вкладки гистограммы, а не на главной странице. Для этого я изменил ссылочный код. Ниже приведен измененный код.
Закомментировано из app.layout и перемещено dbc.Button внутри вкладки в качестве первого столбца (модификация обратного вызова):
@app.callback(
Output("tab-content", "children"),
[Input("tabs", "active_tab"), Input("store", "data")],
)
def render_tab_content(active_tab, data):
"""
This callback takes the 'active_tab' property as input, as well as the
stored graphs, and renders the tab content depending on what the value of
'active_tab' is.
"""
if active_tab and data is not None:
if active_tab == "scatter":
return dcc.Graph(figure=data["scatter"])
elif active_tab == "histogram":
return dbc.Row(
[
#Modified to add the Button inside the Tab
dbc.Col(dbc.Button(
"Regenerate graphs",
color="primary",
block=True,
id="iButton",
className="mb-3",
), width=4
),
dbc.Col(dcc.Graph(figure=data["hist_1"]), width=4),
dbc.Col(dcc.Graph(figure=data["hist_2"]), width=4),
]
)
return "No tab selected"
2-е изменение, как показано ниже, обновило iButton в качестве входных данных в обратном вызове:
@app.callback(Output("store", "data"), [Input("iButton", "n_clicks")])
def generate_graphs(n):
С этим изменением возникает исключение при запуске приложения.
ID not found in layout
Attempting to connect a callback Input item to component:
"iButton"
but no components with that id exist in the layout.
Я не уверен, как обрабатывать поведение кнопки, когда она находится внутри вкладки.