Could you please show us some example code of how you create the card?
The modal itself will be shown regardless of where the corresponding code is placed.
EDIT: A quick mock-up:
from dash import Dash, Input, Output, State, html, callback, MATCH
import dash_bootstrap_components as dbc
app = Dash(
__name__,
external_stylesheets=[dbc.themes.BOOTSTRAP]
)
def create_modal(idx):
return dbc.Modal(
[
dbc.ModalHeader(
dbc.ModalTitle(f"Modal {idx}"),
),
dbc.ModalBody(
html.Div('Modal body')
),
dbc.ModalFooter(
dbc.Button(
"Cancel",
id={"type": "btn_cancel_modal", "index": idx},
className="ms-auto",
n_clicks=0
)
)
],
id={"type": "modal", "index": idx},
is_open=False,
)
app.layout = dbc.Container(
[
dbc.Button(
id="button_id",
children="Create card"
),
dbc.Container(
id='output_container',
children=[]
)
],
fluid=True
)
@callback(
Output("output_container", "children"),
Input("button_id", "n_clicks"),
State("output_container", "children"),
prevent_initial_call=True
)
def update_children(n_clicks, current_children):
card = dbc.Card(
[
dbc.CardHeader("This is the header"),
dbc.CardBody(
[
html.H4("Card title", className="card-title"),
html.P("This is some card text", className="card-text"),
]
),
dbc.CardFooter(
children=[
"This is the footer",
dbc.Button(
children="Show modal",
id={"type": "btn_show_modal", "index": n_clicks}),
create_modal(n_clicks)
]
)
],
id={"type": "card", "index": n_clicks},
style={"width": "18rem"},
)
current_children.append(card)
return current_children
@callback(
Output({"type": "modal", "index": MATCH}, "is_open"),
Input({"type": "btn_show_modal", "index": MATCH}, "n_clicks"),
prevent_initial_call=True
)
def show(_):
return True
@callback(
Output({"type": "modal", "index": MATCH}, "is_open", allow_duplicate=True),
Input({"type": "btn_cancel_modal", "index": MATCH}, "n_clicks"),
prevent_initial_call=True
)
def show(_):
return False
if __name__ == "__main__":
app.run(debug=True)
mred pmcb