Py.Cafe

kolibril13/

threejs-morph

Customizable "Hello World" Widget using AnyWidget Library

DocsPricing
  • app.py
  • requirements.txt
  • widget.css
  • widget.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import anywidget
import traitlets
from traitlets import List, Any
from pathlib import Path
import numpy as np
import scipy

class HelloWidget(anywidget.AnyWidget):
    _esm = Path("widget.js")

    voxel_data = Any().tag(sync=True)

counter = HelloWidget()

# Create the NumPy array
voxelarray = np.full((11, 11, 11), 0)
voxelarray[5, 3, 5] = 1
voxelarray[5, 7, 5] = 1

# Convert the NumPy array to a nested list
voxel_data_list = voxelarray.tolist()

# Assign the values to the widget
counter.count = 42
counter.voxel_data = voxel_data_list

#page = counter




def ball(radius, dtype=np.uint8):
    n = 2 * radius + 1
    Z, Y, X = np.mgrid[
        -radius: radius: n * 1j,
        -radius: radius: n * 1j,
        -radius: radius: n * 1j
    ]
    s = X ** 2 + Y ** 2 + Z ** 2
    return np.array(s <= radius * radius, dtype=dtype)


# Create the NumPy array
voxelarray = np.full((15, 15, 15), 0)
voxelarray[5, 3, 5] = 1
voxelarray[5, 9, 5] = 1

# apply dilation with scipy
img_morphed = scipy.ndimage.binary_dilation(voxelarray, ball(2)).astype(int)
img_morphed2 = scipy.ndimage.binary_dilation(voxelarray, ball(3)).astype(int)
img_morphed3 = scipy.ndimage.binary_dilation(voxelarray, ball(4)).astype(int)
img_morphed4 = scipy.ndimage.binary_dilation(voxelarray, ball(5)).astype(int)

sequence = []
sequence += [voxelarray.tolist()]
sequence += [img_morphed.tolist()]
sequence += [img_morphed2.tolist()]
sequence += [img_morphed3.tolist()]
sequence += [img_morphed4.tolist()]
 
w = HelloWidget()
w.voxel_data =  sequence
page = w