File: 10_router.py

package info (click to toggle)
python-trame 3.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 101,620 kB
  • sloc: python: 13,515; sh: 183; javascript: 93; makefile: 7
file content (151 lines) | stat: -rw-r--r-- 5,494 bytes parent folder | download
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
# Required for rendering initialization, not necessary for
# local rendering, but doesn't hurt to include it
import vtkmodules.vtkRenderingOpenGL2  # noqa
from vtkmodules.vtkFiltersSources import vtkConeSource

# Required for interactor initialization
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleSwitch  # noqa
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkPolyDataMapper,
    vtkRenderer,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
)

from trame.app import get_server
from trame.ui.router import RouterViewLayout
from trame.ui.vuetify import SinglePageWithDrawerLayout
from trame.widgets import html, router, trame, vtk, vuetify

# -----------------------------------------------------------------------------
# VTK pipeline
# -----------------------------------------------------------------------------

renderer = vtkRenderer()
renderWindow = vtkRenderWindow()
renderWindow.AddRenderer(renderer)

renderWindowInteractor = vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
renderWindowInteractor.GetInteractorStyle().SetCurrentStyleToTrackballCamera()

cone_source = vtkConeSource()
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(cone_source.GetOutputPort())
actor = vtkActor()
actor.SetMapper(mapper)

renderer.AddActor(actor)
renderer.ResetCamera()
renderWindow.Render()


# -----------------------------------------------------------------------------
# Trame setup
# -----------------------------------------------------------------------------

server = get_server(client_type="vue2")
state, ctrl = server.state, server.controller


@state.change("resolution")
def update_resolution(resolution, **kwargs):
    cone_source.SetResolution(resolution)
    ctrl.view_update()


layout = SinglePageWithDrawerLayout(server)

# There are two ways to register a route
with RouterViewLayout(server, "/"):
    trame.LifeCycleMonitor(name="Home", events="['created', 'destroyed']")
    with vuetify.VCard():
        vuetify.VCardTitle("This is home {{ resolution }}")

with RouterViewLayout(server, "/foo"):
    trame.LifeCycleMonitor(name="Foo", events="['created', 'destroyed']")
    with vuetify.VCard():
        vuetify.VCardTitle("This is foo")
        with vuetify.VCardText():
            vuetify.VBtn("Take me back {{ resolution }}", click="$router.back()")
            with html.Div(classes="mt-6", style="height: 400px; min-height: 400px;"):
                view = vtk.VtkLocalView(renderWindow)
                view.update()
                ctrl.view_update.add(view.update)

# or use the contextmanager 'with_route'
with RouterViewLayout(server, "/bar/:id"):
    trame.LifeCycleMonitor(name="Bar", events="['created', 'destroyed']")
    with vuetify.VCard():
        vuetify.VCardTitle(
            "This is bar with ID '{{ $route.params.id }}' with resolution {{ resolution }}"
        )
        with vuetify.VCardText(classes="red lighten-5 pa-0"):
            with html.Div(style="height: 400px; min-height: 400px;"):
                view = vtk.VtkRemoteView(renderWindow)
                view.update()
                ctrl.view_update.add(view.update)
            with html.Div(style="height: 400px; min-height: 400px;"):
                view = vtk.VtkLocalView(renderWindow)
                view.update()
                ctrl.view_update.add(view.update)

# add <router-view />
with layout:
    layout.title.set_text("Multi-Page demo")
    trame.LifeCycleMonitor(name="Root", events="['created', 'destroyed']")

    with layout.toolbar as tb:
        vuetify.VSpacer()
        tb.add_child("{{ resolution }}")
        vuetify.VSlider(
            v_model=("resolution", 6),
            min=3,
            max=60,
            step=1,
            dense=True,
            hide_details=True,
            style="max-width: 300px;",
        )

    with layout.content:
        with vuetify.VContainer():
            router.RouterView()

    # add router buttons to the drawer
    with layout.drawer:
        with vuetify.VList(shaped=True, v_model=("selectedRoute", 0)):
            vuetify.VSubheader("Routes")

            with vuetify.VListItem(to="/"):
                with vuetify.VListItemIcon():
                    vuetify.VIcon("mdi-home")
                with vuetify.VListItemContent():
                    vuetify.VListItemTitle("Home")

            with vuetify.VListItem(to="/foo"):
                with vuetify.VListItemIcon():
                    vuetify.VIcon("mdi-food")
                with vuetify.VListItemContent():
                    vuetify.VListItemTitle("Foo")

            with vuetify.VListGroup(value=("true",), sub_group=True):
                with vuetify.Template(v_slot_activator=True):
                    vuetify.VListItemTitle("Bars")
                with vuetify.VListItemContent():
                    for i in range(5):
                        with vuetify.VListItem(to=f"/bar/{i + 1}"):
                            with vuetify.VListItemIcon():
                                vuetify.VIcon("mdi-peanut-outline")
                            with vuetify.VListItemContent():
                                vuetify.VListItemTitle("Bar {{ resolution }}")
                                vuetify.VListItemSubtitle(f"ID {i + 1}")

# -----------------------------------------------------------------------------
# Main
# -----------------------------------------------------------------------------

if __name__ == "__main__":
    ctrl.view_update()
    server.start()