widgetti / reacton

A pure Python port of React for ipywidgets
https://reacton.solara.dev/
MIT License
298 stars 18 forks source link

Add bqplot_image_gl #28

Open swelborn opened 9 months ago

swelborn commented 9 months ago

Hey @maartenbreddels, I'm not sure if bqplot_image_gl will be upstreamed to bqplot_gl eventually. I generated this code, and it appears to work:

import numpy as np
import solara
from reacton.bqplot import Axis, ColorAxis, ColorScale, Figure, HeatMap, LinearScale
from reacton.bqplot_image_gl import ImageGL
from reacton.core import use_state

num_images = 20  
x = np.linspace(-5, 5, 500)
y = np.linspace(-5, 5, 500)
X, Y = np.meshgrid(x, y)

image_data = np.array(
    [np.cos(X**2 + Y**2 + phase) for phase in np.linspace(0, 2 * np.pi, num_images)]
)

@solara.component
def Page():
    current_image_index, set_current_image_index = use_state(0)

    x_sc, y_sc, col_sc = (
        LinearScale(),
        LinearScale(),
        ColorScale(min=0, max=1, scheme="RdYlBu"),
    )

    heat = ImageGL(
        image=image_data[current_image_index],
        scales={"x": x_sc, "y": y_sc, "image": col_sc},
    )
    ax_x = Axis(scale=x_sc)
    ax_y = Axis(scale=y_sc, orientation="vertical")
    ax_c = ColorAxis(scale=col_sc)

    fig = Figure(
        marks=[heat],
        axes=[ax_x, ax_y, ax_c],
        title="Cosine",
        min_aspect_ratio=1,
        max_aspect_ratio=1,
        padding_y=0,
    )

    def on_slider_change(value):
        set_current_image_index(value)

    slider = solara.SliderInt(
        label="Image Index",
        min=0,
        max=len(image_data) - 1,
        step=1,
        value=current_image_index,
        on_value=on_slider_change,
    )

    return solara.VBox([slider, fig])

I do not know if I am using that code generation tool correctly. Would be great if I could have imagegl. Its performance is way better than anything else that I have seen when re-rendering stuff (like example above). plotly is really nice, but performance isn't there.