Usually I’d do something like this in Dash but I just wanted to prototype something for data exploration, and I thought it would be easier…
I just to make a figure and update it when I press a button without the figure appearing or disappearing again: I just want to update the figure data
property.
I tried this approach but a new figure seems to be created every time, and the update is really really slow as well
alpha_widget = widgets.FloatSlider(
value=0.7, min=0, max=1, step=0.1, description="Alpha"
)
beta_widget = widgets.FloatSlider(value=0.3, min=0, max=1, step=0.1, description="Beta")
n_clusters_widget = widgets.IntSlider(
value=80, min=1, max=100, step=10, description="Number of Clusters"
)
update_button = widgets.Button(description="Update Map")
output = widgets.Output()
fig = go.FigureWidget(data={}, layout={})
def update_map(alpha, beta, n_clusters):
representative_routes = make_clustering(
planning, alpha=alpha, beta=beta, n_clusters=n_clusters
)
# Generate the new map figure
fig_data = make_routes_map(representative_routes, color_column="route_cluster")
fig.layout = fig_data.layout
if len(fig.data) > 1:
fig.data = []
for trace in fig_data.data:
fig.add_trace(trace)
# Clear and update the output widget with the new figure
with output:
display(fig) # Display the new figure in the output widget
# Link the button click to the update function
def on_button_click(b):
update_map(alpha_widget.value, beta_widget.value, n_clusters_widget.value)
# Bind the button click to the update action
update_button.on_click(on_button_click)
# Display widgets and output container
display(alpha_widget, beta_widget, n_clusters_widget, update_button, output)
Is this not the correct approach?