Hi ,
I have an issue with holoviews and datashader in a dash app. I am trying to plot a large dataset in a scatter plot. I also want to have menus for selecting/filtering data which should then trigger a callback which updates the scatter plot.
My problem is that initially, the datashaded scatter plot works as expected: When zooming in and out, the plot is rerendered automatically. If I create a new plot in a callback however, the resulting scatter plot is no longer updating when zooming.
Here is a minimal example:
When first starting this dash app, everything works as expected. When clicking the button to generate a new plot using new data, the plot becomes static when zooming.
How can I create a plot that automatically updates on zoom from within a callback?
For reference, I based my code on this example: HoloViews | Dash for Python Documentation | Plotly
import pandas as pd
import numpy as np
import holoviews as hv
from holoviews.operation.datashader import datashade
from holoviews.plotting.plotly.dash import to_dash
from dash import Dash, html, callback, Input, Output
hv.extension("plotly")
def generate_data():
data = np.random.random((1000000, 2))
return pd.DataFrame(data, columns=["x", "y"])
def create_datashaded_scatterplot(df):
dataset = hv.Dataset(df)
scatter = datashade(
hv.Scatter(dataset, kdims=["x"], vdims=["y"])
).opts(width=800, height=800)
return scatter
def update_plot(df):
scatter = create_datashaded_scatterplot(df)
components = to_dash(app, [scatter])
return components.children
@callback(
Output("plot-div", "children"),
Input("update-data-button", "n_clicks"),
prevent_initial_call=True
)
def update_plot_callback(n_clicks):
df = generate_data()
return update_plot(df)
example_data = generate_data()
app = Dash(__name__)
layout = html.Div(
id="layout-div",
children=[
html.Div(
id="plot-div",
children=update_plot(example_data)
),
html.Button(id="update-data-button", children="Update data", n_clicks=0)
]
)
app.layout = layout
if __name__ == "__main__":
app.run_server(debug=True)