From b337de24d944dcddc9a975c36c8aa435e79fb35e Mon Sep 17 00:00:00 2001 From: Sebastien Jourdain Date: Fri, 18 Oct 2024 09:26:25 -0600 Subject: [PATCH] docs(examples): improve some examples --- examples/02_vuetify/01_menu_v3.py | 44 ++++++++++++++++++++++++++++ examples/09_advanced/external-esm.py | 32 ++++++++++++++++++++ examples/blogs/vtk-9.4/pipeline.py | 37 +++++++++++------------ 3 files changed, 95 insertions(+), 18 deletions(-) create mode 100644 examples/02_vuetify/01_menu_v3.py create mode 100644 examples/09_advanced/external-esm.py diff --git a/examples/02_vuetify/01_menu_v3.py b/examples/02_vuetify/01_menu_v3.py new file mode 100644 index 00000000..95c0a096 --- /dev/null +++ b/examples/02_vuetify/01_menu_v3.py @@ -0,0 +1,44 @@ +from trame.app import get_server +from trame.ui.vuetify3 import SinglePageLayout +from trame.widgets import vuetify3 as vuetify + +# ----------------------------------------------------------------------------- +# Trame setup +# ----------------------------------------------------------------------------- + +server = get_server() +state, ctrl = server.state, server.controller + +state.menu_items = ["one", "two", "three"] + + +def print_item(item): + print("Clicked on", item) + + +# ----------------------------------------------------------------------------- +# GUI +# ----------------------------------------------------------------------------- + +state.trame__title = "Menu example" + +with SinglePageLayout(server) as layout: + with layout.toolbar: + vuetify.VSpacer() + with vuetify.VMenu(): + with vuetify.Template(v_slot_activator="{ on, attrs }"): + with vuetify.VBtn(icon=True, v_bind="attrs", v_on="on"): + vuetify.VIcon("mdi-dots-vertical") + with vuetify.VList(): + with vuetify.VListItem( + v_for="(item, i) in menu_items", + key="i", + value=["item"], + ): + vuetify.VBtn( + "{{ item }}", + click=(print_item, "[item]"), + ) + +if __name__ == "__main__": + server.start() diff --git a/examples/09_advanced/external-esm.py b/examples/09_advanced/external-esm.py new file mode 100644 index 00000000..93ebab01 --- /dev/null +++ b/examples/09_advanced/external-esm.py @@ -0,0 +1,32 @@ +# + +from trame.app import get_server +from trame.widgets import html +from trame.ui.html import DivLayout + + +class ExternalJS: + def __init__(self, server=None): + self.server = get_server(server) + + self.server.enable_module( + { + "module_scripts": [ + "https://esm.sh/canvas-confetti@1.9.3", + ], + } + ) + self.ui = self._build_ui() + + def _build_ui(self): + with DivLayout(self.server): + html.Button("Click Me") + + +def main(): + app = ExternalJS() + app.server.start() + + +if __name__ == "__main__": + main() diff --git a/examples/blogs/vtk-9.4/pipeline.py b/examples/blogs/vtk-9.4/pipeline.py index 91825586..168b0bac 100644 --- a/examples/blogs/vtk-9.4/pipeline.py +++ b/examples/blogs/vtk-9.4/pipeline.py @@ -8,13 +8,13 @@ def setup_vtk(): renderer = vtk.vtkRenderer() - render_window = vtk.vtkRenderWindow() + render_window = vtk.vtkRenderWindow(off_screen_rendering=True) render_window.AddRenderer(renderer) - render_window.off_screen_rendering = True - render_window_interactor = vtk.vtkRenderWindowInteractor() - render_window_interactor.SetRenderWindow(render_window) - render_window_interactor.GetInteractorStyle().SetCurrentStyleToTrackballCamera() + render_window_interactor = vtk.vtkRenderWindowInteractor( + render_window=render_window + ) + render_window_interactor.interactor_style.SetCurrentStyleToTrackballCamera() # Pipeline sphere_source = vtk.vtkSphereSource( @@ -28,23 +28,24 @@ def setup_vtk(): resolution=30, ) - normals = vtk.vtkPolyDataNormals() - normals.compute_cell_normals = 1 - normals.SetInputConnection(sphere_source.GetOutputPort()) + normals = vtk.vtkPolyDataNormals( + compute_cell_normals=1, + input_connection=sphere_source.output_port, + ) - cell_centers = vtk.vtkCellCenters() - cell_centers.SetInputConnection(normals.GetOutputPort()) + cell_centers = vtk.vtkCellCenters( + input_connection=normals.output_port, + ) - glyph_mapper = vtk.vtkGlyph3DMapper() - glyph_mapper.SetInputConnection(0, cell_centers.GetOutputPort()) - glyph_mapper.SetInputConnection(1, cone_source.GetOutputPort()) - glyph_mapper.orient = True - glyph_mapper.orientation_array = "Normals" + glyph_mapper = vtk.vtkGlyph3DMapper( + orient=True, + orientation_array="Normals", + input_connection=cell_centers.output_port, + source_connection=cone_source.output_port, + ) # Rendering - actor = vtk.vtkActor() - actor.SetMapper(glyph_mapper) - renderer.AddActor(actor) + renderer.AddActor(vtk.vtkActor(mapper=glyph_mapper)) renderer.ResetCamera() render_window.Render()