如何使用按钮或下拉菜单在破折号 python 中显示 vtk 或网格?

How to display a vtk or a mesh in dash python using a button or a dropdown?

我想在单击 button 后在几何表示 dash_vtk.View 中显示 vtk 文件。有什么办法吗?

这是我的简单代码,因为我知道我还是 dash 的新手 python

import os
import dash
import dash_bootstrap_components as dbc
from dash import html
from dash import dcc
import dash_daq as daq
from dash.dependencies import Input, Output, State
import dash_vtk
from dash_vtk.utils import to_mesh_state, preset_as_options
import vtk

file_name = os.path.join("/home/munther/")
file_Reader = vtk.vtkXMLPolyDataReader()
file_Reader.SetFileName(file_name)
file_Reader.Update()
sample_mesh = to_mesh_state(file_Reader.GetOutput())

BS = "https://cdn.jsdelivr.net/npm/bootswatch@4.5.2/dist/slate/bootstrap.min.css"
app = dash.Dash(external_stylesheets=[BS])
server = app.server

vtk_view = dash_vtk.View(
    id="vtk-view",
    background = [0.2,0.9,0.7],        
    children=[
        dash_vtk.GeometryRepresentation(
            id="mesh-rep",
            property={"edgeVisibility": False, 'color': (39, 66, 204)},
            #children=[dash_vtk.Mesh(id="vtk_mesh", state=sample_mesh)],
        ),
    ],
)

output = html.Div([
    dbc.Row([
        dbc.Col([
            html.Div([
                html.Button('Display VTK', id='submit-VTK', n_clicks=0),
                html.Div(id='VTK-provider')
            ]),
        ]),
    ]),
    html.Div(id='slider-output-container')
])

app.layout = dbc.Container(
    fluid=True,
    style={"marginTop": "15px", "height": "calc(100vh - 30px)"},
    children=[
        dbc.Row([
            dbc.Col(
                width=4,
                children=output
            ),
            dbc.Col(
                width=8,
                children=[html.H1('VTK Visualization', style={'textAlign': 'center', 'color': 'ffffff'}),
                          html.Div(vtk_view, style={"height": "100%", "width": "100%"},)
                          ],
            ),
        ],
            style={"height": "100%"},
        ),
    ],
)

@app.callback(Output('VTK-provider', 'children'),
              Output('vtk-view', 'children'),
              Input('submit-VTK', 'n_clicks'),)
def foam_to_vtk(n_clicks):
    if not n_clicks:
            #raise dash.exceptions.PreventUpdate
        return dash.no_update  
    return dash_vtk.GeometryRepresentation(
        [
            dash_vtk.Mesh(state=sample_mesh)
            ],
        property={"edgeVisibility": True}
    ),

if __name__ == '__main__':
    app.run_server()

我不确定这是您的代码中唯一的问题,但是您的函数 foam_to_vtk 没有 return 一个 dash.html 元素。也许试试 :

@app.callback(Output('VTK-provider', 'children'),
              Output('vtk-view', 'children'),
              Input('submit-VTK', 'n_clicks'),)
def foam_to_vtk(n_clicks):
    if not n_clicks:
        return dash.no_update  
    the_vtk_view = dash_vtk.GeometryRepresentation(
        [
            dash_vtk.Mesh(state=sample_mesh)
            ],
        property={"edgeVisibility": True}
    )
    return html.Div(
                style={"height": "calc(100vh - 50px)", "width": "100%"},
                children=[html.Div(style={"height": "100%", "width": "100%"}, children=the_vtk_view)]
            )