In Python I am working with Dash and Dash bootstrap components. I am having trouble with the bootstrap Modal component. My most basic implementation of the modal, using the first example here, works fine. However, If I try to replace their single callback with two separate callbacks for the buttons, it stops working. Can anybody explain to me why this is and help me get it working? The callbacks don’t seem difficult at all.
Most basic implementation of their code (this works for me):
from dash import Dash, html
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc
from dash_bootstrap_components.themes import LUMEN
app = Dash(external_stylesheets=[LUMEN])
app.title = "Test"
app.layout = html.Div(
[
dbc.Button("Open modal", id="open", n_clicks=0),
dbc.Modal(
[
dbc.ModalHeader(dbc.ModalTitle("Header")),
dbc.ModalBody("This is the content of the modal"),
dbc.ModalFooter(
dbc.Button(
"Close", id="close", className="ms-auto", n_clicks=0
)
),
],
id="modal",
is_open=False,
),
]
)
@app.callback(
Output("modal", "is_open"),
[Input("open", "n_clicks"), Input("close", "n_clicks")],
[State("modal", "is_open")],
)
def toggle_modal(n1, n2, is_open):
print(is_open)
print(type(is_open))
if n1 or n2:
return not is_open
return is_open
app.run()
Now I’ve replaced the callback above with the following two callbacks, which breaks
@app.callback(
Output("modal", "is_open"),
Input("open", "n_clicks")
)
def toggle_modal_open(n1):
if n1:
return True
@app.callback(
Output("modal", "is_open"),
Input("close", "n_clicks")
)
def toggle_modal_close(n1):
if n1:
return False