-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.py
286 lines (230 loc) · 9.59 KB
/
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from pathlib import Path
from trame.app import get_server
from trame.ui.vuetify3 import SinglePageWithDrawerLayout
from trame.widgets import vuetify3 as vuetify, simput, html, trame, vtk
from trame_simput import get_simput_manager
from vtk_adaper import VTKAdapter, VTKFactory
from vtk_helpers import View, Representation
# -----------------------------------------------------------------------------
# Trame setup
# -----------------------------------------------------------------------------
server = get_server()
state, ctrl = server.state, server.controller
state.import_file = None
state.export_content = None
# -----------------------------------------------------------------------------
# Simput initialization
# -----------------------------------------------------------------------------
DEF_DIR = Path(__file__).with_name("definitions")
simput_manager = get_simput_manager(
object_factory=VTKFactory(),
object_adapter=VTKAdapter(),
)
simput_manager.load_model(yaml_file=DEF_DIR / "model.yaml")
simput_manager.load_ui(xml_file=DEF_DIR / "ui.xml")
PXM = simput_manager.proxymanager
TYPES = PXM.types()
# -----------------------------------------------------------------------------
# VTK management
# -----------------------------------------------------------------------------
view = View()
def on_change(topic, ids=None, **kwargs):
if topic == "deleted":
update_sources()
if topic == "created":
for obj_id in ids:
obj_vtk = PXM.get(obj_id).object
rep = Representation()
rep.SetView(view)
rep.SetInput(obj_vtk)
update_sources()
ctrl.view_update()
PXM.on(on_change)
# -----------------------------------------------------------------------------
# Data management
# -----------------------------------------------------------------------------
def update_sources(*args, **kwargs):
ids = list(map(lambda p: p.id, PXM.tags("Source")))
state.source_ids = ids
ctrl.view_update()
def create_object(name, type):
obj = PXM.create(type, _name=name)
obj.fetch()
state.active_id = obj.id
# Init source_ids at startup
ctrl.on_server_ready.add(update_sources)
# -----------------------------------------------------------------------------
# Import / Export
# -----------------------------------------------------------------------------
def export_state():
state.export_content = PXM.save()
@state.change("import_file")
def import_file(import_file, **kwargs):
if import_file is None:
return
if import_file:
json_content = import_file.get("content").decode("utf-8")
PXM.load(file_content=json_content)
# reset current import
state.import_file = None
# -----------------------------------------------------------------------------
# Simput container initialization
# -----------------------------------------------------------------------------
simput_widget = simput.Simput(simput_manager, prefix="simput", trame_server=server)
ctrl.simput_apply = simput_widget.apply
ctrl.simput_reset = simput_widget.reset
# -----------------------------------------------------------------------------
# UI
# -----------------------------------------------------------------------------
ICONS = [
"mdi-format-list-bulleted-type",
"mdi-plus",
"mdi-pencil-outline",
]
with SinglePageWithDrawerLayout(server) as layout:
simput_widget.register_layout(layout)
with layout.drawer as drawer:
with vuetify.VTabs(
v_model=("drawer_mode", 0),
dense=True,
center_active=True,
):
for icon in ICONS:
with vuetify.VTab():
vuetify.VIcon(icon)
with vuetify.VWindow(v_model=("drawer_mode", 0), style="height: 100%;"):
with vuetify.VWindowItem(style="height: 100%;"):
with vuetify.VList(dense=True):
with vuetify.VItemGroup(v_model=("active_id", 0), color="primary"):
with vuetify.VListItem(
v_for="(id, i) in source_ids",
key="i",
value=("id",),
):
with vuetify.VListItemTitle():
simput.SimputItem(
"{{ data.name }}",
item_id="id",
no_ui=True,
v_slot="{ data }",
)
with vuetify.VWindowItem():
with vuetify.VCard():
with vuetify.VCardText():
with vuetify.VRow():
with vuetify.VCol():
vuetify.VSelect(
v_model=("obj_type", ""),
items=("obj_types", TYPES),
dense=True,
hide_details=True,
change="obj_name = $event",
)
with vuetify.VCol():
vuetify.VTextField(
v_model=("obj_name", ""),
clearable=True,
dense=True,
hide_details=True,
)
with vuetify.VCardActions():
vuetify.VSpacer()
vuetify.VBtn(
"Create", click=(create_object, "[obj_name, obj_type]")
)
with vuetify.VWindowItem():
simput.SimputItem(item_id="active_id")
with layout.toolbar as toolbar:
layout.title.set_text("VTK")
toolbar.dense = True
vuetify.VSpacer()
vuetify.VDivider(vertical=True, classes="mx-1")
with vuetify.VTooltip(bottom=True):
with vuetify.Template(v_slot_activator="{ on, attrs }"):
with vuetify.VBtn(
v_bind="attrs",
v_on="on",
icon=True,
click="window.document.getElementById('importFile').click();",
):
vuetify.VIcon("mdi-database-import-outline")
html.Input(
id="importFile",
type="file",
style="display: none",
change="import_file=$event.target.files[0]",
__events=["change"],
)
html.Span("Import State")
with vuetify.VTooltip(bottom=True):
with vuetify.Template(v_slot_activator="{ on, attrs }"):
with vuetify.VBtn(
v_bind="attrs",
v_on="on",
icon=True,
click=export_state,
):
vuetify.VIcon("mdi-database-export-outline")
trame.ClientStateChange(
value="export_content",
change="export_content && utils.download('VTKState.json', export_content)",
)
html.Span("Export State")
vuetify.VDivider(vertical=True, classes="mx-1")
with vuetify.VTooltip(bottom=True):
with vuetify.Template(v_slot_activator="{ on, attrs }"):
with vuetify.VBtn(
v_bind="attrs",
v_on="on",
classes="mx-2",
small=True,
outlined=True,
icon=True,
disabled=("!simputChangeSet",),
click=ctrl.simput_apply,
):
with vuetify.VBadge(
content=("simputChangeSet", ""),
offset_x=8,
offset_y=8,
value=("simputChangeSet", ""),
):
vuetify.VIcon("mdi-database-import")
html.Span("Apply Changes")
with vuetify.VTooltip(bottom=True):
with vuetify.Template(v_slot_activator="{ on, attrs }"):
with vuetify.VBtn(
v_bind="attrs",
v_on="on",
classes="mx-2",
small=True,
outlined=True,
icon=True,
disabled=("!simputChangeSet",),
click=ctrl.simput_reset,
):
vuetify.VIcon("mdi-undo-variant")
html.Span("Reset Changes")
with vuetify.VTooltip(bottom=True):
with vuetify.Template(v_slot_activator="{ on, attrs }"):
with vuetify.VBtn(
v_bind="attrs",
v_on="on",
icon=True,
click=ctrl.view_reset_camera,
):
vuetify.VIcon("mdi-crop-free")
html.Span("Reset Camera")
with layout.content:
with vuetify.VContainer(fluid=True, classes="fill-height pa-0"):
html_view = vtk.VtkLocalView(
view.render_window,
on_ready=ctrl.view_reset_camera,
)
ctrl.view_update = html_view.update
ctrl.view_reset_camera = html_view.reset_camera
# -----------------------------------------------------------------------------
# Start server
# -----------------------------------------------------------------------------
if __name__ == "__main__":
server.start()