Py.Cafe

maartenbreddels/

dash-vtk-cone-visualization

Interactive VTK Cone Visualization using vtk-js and dask

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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import random

import dash
import dash_vtk
import dash_bootstrap_components as dbc
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output, State

random.seed(42)

app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
server = app.server

vtk_view = dash_vtk.View(
    id="geometry-view",
    children=[
        dash_vtk.GeometryRepresentation(
            [
                dash_vtk.Algorithm(
                    id="vtk-algorithm",
                    vtkClass="vtkConeSource",
                    state={"capping": False, "resolution": 60},
                )
            ]
        )
    ],
)

controls = dbc.Card(
    [
        dbc.CardHeader("Controls"),
        dbc.CardBody(
            [
                html.P("Resolution:"),
                dcc.Slider(
                    id="slider-resolution",
                    min=10,
                    max=100,
                    step=1,
                    value=60,
                    marks={10: "10", 100: "100"},
                ),
                html.Br(),
                dbc.Checklist(
                    options=[{"label": "Capping", "value": "capping"}],
                    value=[],
                    id="capping-checklist",
                    switch=True,
                ),
            ]
        ),
    ]
)

app.layout = dbc.Container(
    fluid=True,
    style={"marginTop": "15px", "height": "calc(100vh - 30px)"},
    children=[
        dbc.Row(
            [
                dbc.Col(width=4, children=controls),
                dbc.Col(
                    width=8,
                    children=[
                        html.Div(
                            vtk_view,
                            style={"height": "100%", "width": "100%"},
                        )
                    ],
                ),
            ],
            style={"height": "100%"},
        ),
    ],
)


@app.callback(
    [Output("vtk-algorithm", "state"), Output("geometry-view", "triggerResetCamera")],
    [Input("slider-resolution", "value"), Input("capping-checklist", "value")],
)
def update_cone(slider_val, checked_values):
    new_state = {"resolution": slider_val, "capping": "capping" in checked_values}
    return new_state, random.random()


if __name__ == "__main__":
    app.run_server(debug=True)
requirements.txt
1
2
3
4
5
dash
dash_bootstrap_components
dash_vtk
# vtk is not really a hard dependency of dask_vtk, so we install a mock
vtk # mock