From 83ee7cdee8cda7ae39cd71a262592cbdc8adea5b Mon Sep 17 00:00:00 2001 From: xhlulu Date: Tue, 13 Apr 2021 19:50:04 -0400 Subject: [PATCH 1/2] Refactor view loading to be in a callback at initialization only --- apps/dash-vehicle-geometry/app.py | 51 ++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/apps/dash-vehicle-geometry/app.py b/apps/dash-vehicle-geometry/app.py index d08eac10c..5e0b0ac89 100644 --- a/apps/dash-vehicle-geometry/app.py +++ b/apps/dash-vehicle-geometry/app.py @@ -32,7 +32,7 @@ def _load_vtp(filepath, fieldname=None, point_arrays=[], cell_arrays=[]): # GUI setup # ----------------------------------------------------------------------------- -app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP]) +app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP], suppress_callback_exceptions=True) server = app.server # ----------------------------------------------------------------------------- @@ -41,6 +41,9 @@ def _load_vtp(filepath, fieldname=None, point_arrays=[], cell_arrays=[]): # vehicle geometry vehicle_vtk = [] +vehicle_mesh_ids = [] +vehicle_meshes = [] + for filename in glob.glob(os.path.join(DATA_PATH, "vehicle") + "/*.vtp"): mesh = _load_vtp(filename, point_arrays=["U", "p"]) part_name = filename.split("/")[-1].replace(".vtp", "") @@ -51,11 +54,18 @@ def _load_vtp(filepath, fieldname=None, point_arrays=[], cell_arrays=[]): actor={"visibility": 1}, mapper={"scalarVisibility": False}, children=[dash_vtk.Mesh(id=f"{part_name}-mesh", state=mesh,)], + # children=[dash_vtk.Mesh(id=f"{part_name}-mesh")], ) vehicle_vtk.append(child) + vehicle_mesh_ids.append(f"{part_name}-mesh") + vehicle_meshes.append(mesh) + # isosurfaces isosurfs_vtk = [] +isomesh_ids = [] +isosurfs_meshes = [] + for filename in glob.glob(os.path.join(DATA_PATH, "isosurfaces") + "/*.vtp"): mesh = _load_vtp(filename) @@ -65,15 +75,21 @@ def _load_vtp(filepath, fieldname=None, point_arrays=[], cell_arrays=[]): property={"color": [1, 0, 0]}, actor={"visibility": 0}, children=[dash_vtk.Mesh(id=f"{surf_name}-mesh", state=mesh,)], + # children=[dash_vtk.Mesh(id=f"{surf_name}-mesh")], ) isosurfs_vtk.append(child) + isomesh_ids.append(f"{surf_name}-mesh") + isosurfs_meshes.append(mesh) + + # ----------------------------------------------------------------------------- # 3D Viz # ----------------------------------------------------------------------------- -vtk_view = dash_vtk.View(id="vtk-view", children=vehicle_vtk + isosurfs_vtk) +# vtk_view = dash_vtk.View(id="vtk-view", children=vehicle_vtk + isosurfs_vtk) +vtk_view = dash_vtk.View(id="vtk-view") # ----------------------------------------------------------------------------- # Control UI @@ -152,8 +168,18 @@ def _load_vtp(filepath, fieldname=None, point_arrays=[], cell_arrays=[]): dbc.Col( width=8, children=[ - html.Div(vtk_view, style={"height": "100%", "width": "100%"}) + html.Div( + dbc.Spinner( + html.Div( + id="vtk-view-container", + style={"height": "calc(100vh - 230px)", "width": "100%"} + ), + color='light' + ), + style={'background-color': '#334c66'} + ) ], + ), ], style={"margin-top": "15px", "height": "calc(100vh - 230px)"}, @@ -172,6 +198,20 @@ def _load_vtp(filepath, fieldname=None, point_arrays=[], cell_arrays=[]): } + +@app.callback( + Output('vtk-view-container', 'children'), + [Input('geometry', 'value')] +) +def initial_loading(geometry): + triggered = dash.callback_context.triggered + if triggered: + return dash.no_update + + return dash_vtk.View(id="vtk-view", children=vehicle_vtk + isosurfs_vtk) + + + @app.callback( [Output("vtk-view", "triggerRender")] + [Output(item.id, "mapper") for item in vehicle_vtk] @@ -183,6 +223,7 @@ def _load_vtp(filepath, fieldname=None, point_arrays=[], cell_arrays=[]): Input("isosurfaces", "value"), Input("surfcolor", "value"), ], + prevent_initial_call=True ) def update_scene(geometry, isosurfaces, surfcolor): triggered = dash.callback_context.triggered @@ -223,8 +264,8 @@ def update_scene(geometry, isosurfaces, surfcolor): surf_state = [mapper for item in vehicle_vtk] color_ranges = [color_range for item in vehicle_vtk] else: - surf_state = [dash.no_update for item in vehicle_vtk] - color_ranges = [dash.no_update for item in vehicle_vtk] + surf_state = [dash.no_update] * len(vehicle_vtk) + color_ranges = [dash.no_update] * len(vehicle_vtk) return [random.random()] + surf_state + geo_viz + color_ranges + [iso_viz] From 6c1678f79e86dec443aa57e5d7485116a9dde428 Mon Sep 17 00:00:00 2001 From: xhlulu Date: Tue, 13 Apr 2021 19:50:20 -0400 Subject: [PATCH 2/2] apply black --- apps/dash-vehicle-geometry/app.py | 43 ++++++++++++++----------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/apps/dash-vehicle-geometry/app.py b/apps/dash-vehicle-geometry/app.py index 5e0b0ac89..75aaca02d 100644 --- a/apps/dash-vehicle-geometry/app.py +++ b/apps/dash-vehicle-geometry/app.py @@ -32,7 +32,11 @@ def _load_vtp(filepath, fieldname=None, point_arrays=[], cell_arrays=[]): # GUI setup # ----------------------------------------------------------------------------- -app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP], suppress_callback_exceptions=True) +app = dash.Dash( + __name__, + external_stylesheets=[dbc.themes.BOOTSTRAP], + suppress_callback_exceptions=True, +) server = app.server # ----------------------------------------------------------------------------- @@ -53,7 +57,7 @@ def _load_vtp(filepath, fieldname=None, point_arrays=[], cell_arrays=[]): colorDataRange=[0, 100], actor={"visibility": 1}, mapper={"scalarVisibility": False}, - children=[dash_vtk.Mesh(id=f"{part_name}-mesh", state=mesh,)], + children=[dash_vtk.Mesh(id=f"{part_name}-mesh", state=mesh)], # children=[dash_vtk.Mesh(id=f"{part_name}-mesh")], ) vehicle_vtk.append(child) @@ -74,7 +78,7 @@ def _load_vtp(filepath, fieldname=None, point_arrays=[], cell_arrays=[]): id=f"{surf_name}-rep", property={"color": [1, 0, 0]}, actor={"visibility": 0}, - children=[dash_vtk.Mesh(id=f"{surf_name}-mesh", state=mesh,)], + children=[dash_vtk.Mesh(id=f"{surf_name}-mesh", state=mesh)], # children=[dash_vtk.Mesh(id=f"{surf_name}-mesh")], ) @@ -111,7 +115,7 @@ def _load_vtp(filepath, fieldname=None, point_arrays=[], cell_arrays=[]): ], labelStyle={"display": "block"}, value=["body", "drive-train", "front-wing", "rear-wing"], - ), + ) ] ), ] @@ -130,7 +134,7 @@ def _load_vtp(filepath, fieldname=None, point_arrays=[], cell_arrays=[]): {"label": "p", "value": "p"}, ], value="solid", - ), + ) ] ), ] @@ -146,7 +150,7 @@ def _load_vtp(filepath, fieldname=None, point_arrays=[], cell_arrays=[]): options=[{"label": " Cp", "value": "cp"}], labelStyle={"display": "block"}, value=[], - ), + ) ] ), ] @@ -172,14 +176,16 @@ def _load_vtp(filepath, fieldname=None, point_arrays=[], cell_arrays=[]): dbc.Spinner( html.Div( id="vtk-view-container", - style={"height": "calc(100vh - 230px)", "width": "100%"} + style={ + "height": "calc(100vh - 230px)", + "width": "100%", + }, ), - color='light' + color="light", ), - style={'background-color': '#334c66'} + style={"background-color": "#334c66"}, ) ], - ), ], style={"margin-top": "15px", "height": "calc(100vh - 230px)"}, @@ -191,25 +197,16 @@ def _load_vtp(filepath, fieldname=None, point_arrays=[], cell_arrays=[]): # This Handle controls # ----------------------------------------------------------------------------- -COLOR_RANGES = { - "solid": [0, 1], - "U": [0, 100], - "p": [-4464, 1700], -} - +COLOR_RANGES = {"solid": [0, 1], "U": [0, 100], "p": [-4464, 1700]} -@app.callback( - Output('vtk-view-container', 'children'), - [Input('geometry', 'value')] -) +@app.callback(Output("vtk-view-container", "children"), [Input("geometry", "value")]) def initial_loading(geometry): triggered = dash.callback_context.triggered if triggered: return dash.no_update - - return dash_vtk.View(id="vtk-view", children=vehicle_vtk + isosurfs_vtk) + return dash_vtk.View(id="vtk-view", children=vehicle_vtk + isosurfs_vtk) @app.callback( @@ -223,7 +220,7 @@ def initial_loading(geometry): Input("isosurfaces", "value"), Input("surfcolor", "value"), ], - prevent_initial_call=True + prevent_initial_call=True, ) def update_scene(geometry, isosurfaces, surfcolor): triggered = dash.callback_context.triggered