Py.Cafe

maartenbreddels/

solara-ipyreact-voronoi

Voronoi Diagram Demonstration using ipyreact

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import ipyreact
import pathlib
import numpy as np
import solara


def generate(seed, N):
    np.random.seed(seed)
    # Define mean and covariance matrix
    mean = [0, 0]  # Mean of the distribution
    cov = [[1, 0], [0, 1]]  # Covariance matrix (identity matrix for simplicity)

    return np.random.multivariate_normal(mean, cov, N)


class VoronoiWidget(ipyreact.ValueWidget):
    _esm = pathlib.Path("voronoi.tsx")


render_voronoi = solara.reactive(True)
render_delaunay = solara.reactive(True)
seed = solara.reactive(42)
points = solara.reactive(20)

@solara.component
def Page():
    data = solara.use_memo(lambda: generate(seed.value, points.value), [seed.value, points.value])
    print(data.shape)

    with solara.Column():
        with solara.Row():
            solara.Switch(label="Render delaunay", value=render_delaunay)
            solara.Switch(label="Render voronoi", value=render_voronoi)
        solara.SliderInt(label="points", value=points, min=10, max=500)
    VoronoiWidget.element(
        value=data.tolist(),
        props={"width": 600, "height": 400, "render_delaunay": render_delaunay.value, "render_voronoi": render_voronoi.value},
        )
voronoi.tsx
1
Could not load content