Hi dash community,
I am trying to create a dbc.Button with a href link. I want to keep this button disabled untill a row is selected from table. But as soon as the button become enable, it converts into a hyperlink rather than a button. Can someone help?
df = pd.DataFrame(
{
"Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"],
"Amount": [4, 1, 2, 2, 4, 5],
"City": ["SF", "SF", "SF", "Montreal", "Montreal", "Montreal"],
}
)
dash_app.layout = html.Div(
children=[
dash_table.DataTable(
id="my-table",
data=df.to_dict("records"),
columns=[{"name": i, "id": i} for i in df.columns],
row_selectable="single",
selected_rows=[],
),
dbc.Button(
"Open",
color="primary",
className="me-1",
n_clicks=0,
id="my-button",
external_link=True,
disabled=True,
href = "/",
target="_blank",
),
html.Div(id="my-div"),
]
)
@dash_app.callback(
[Output("my-button", "href"),
Output("my-button","disabled")],
Input("my-table", "selected_rows"),
State("my-table", "data"),
)
def update_button(selected_row,data):
if selected_row:
selected_data = data[selected_row[0]]
new_link = f"/{selected_data['Fruit']}"
return [new_link,False]
raise PreventUpdate