-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.py
More file actions
49 lines (36 loc) · 1.71 KB
/
view.py
File metadata and controls
49 lines (36 loc) · 1.71 KB
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
"""View for Plotly example."""
from typing import Any
from nova.mvvm.trame_binding import TrameBinding
from nova.trame import ThemedApp
from nova.trame.view.components import InputField
from nova.trame.view.layouts import GridLayout
from trame.widgets import plotly
from .model import Model
from .view_model import ViewModel
class App(ThemedApp):
"""View for Plotly example."""
def __init__(self) -> None:
super().__init__()
self.create_vm()
# If you forget to call connect, then the application will crash when you attempt to update the view.
self.view_model.figure_bind.connect(self.update_figure)
self.view_model.plot_data_bind.connect("plot_data")
# Generally, we want to initialize the view state before creating the UI for ease of use. If initialization
# is expensive, then you can defer it. In this case, you must handle the view state potentially being
# uninitialized in the UI via v_if statements.
self.view_model.init_view()
self.create_ui()
def create_ui(self) -> None:
with super().create_ui() as layout:
with layout.content:
with GridLayout(classes="mb-4", columns=2, gap="1em"):
InputField("plot_data.data_points")
InputField("plot_data.function", type="select")
self.figure_view = plotly.Figure(figure=self.view_model.get_updated_figure())
def create_vm(self) -> None:
binding = TrameBinding(self.state)
model = Model()
self.view_model = ViewModel(model, binding)
def update_figure(self, _: Any = None) -> None:
self.figure_view.update(self.view_model.get_updated_figure())
self.state.flush()