首页 > 解决方案 > Javafx webview在运行时不显示破折号应用程序

问题描述

我正在尝试将 Dash-plotly 应用程序集成到 Java 应用程序中。

我创建了一个看起来像这样的 Plotly-Dash 应用程序:

代码的 Dash app 组件是这样的:

app = dash.Dash(__name__)

##image_filename = 'tmp.png' # replace with your own image
##encoded_image = base64.b64encode(open(image_filename, 'rb').read())

styles = {
    'pre': {
        'border': 'thin lightgrey solid',
        'overflowX': 'scroll'
    }
}


app.layout = html.Div([
    html.Div([
        html.Div([
            dcc.Graph(id='basic-interactions', figure=fig)
        ], className="seven columns"),

        html.Div([
             dcc.Markdown("""
                **Node Details**

                Click node to view details here.
            """),
            html.Pre(id='click-data', style={"maxHeight": "500px", "overflow": "scroll"}),
        ], className="four columns"),
    ], className="row")
])

@app.callback(
    Output('click-data', 'children'),
    Input('basic-interactions', 'clickData'))
def display_click_data(clickData):
    mydict = ast.literal_eval('{}'.format(clickData))
    if mydict!=None:
        print_clicked = mydict['points'][0]['text']
        for i in full_texts:
            if print_clicked == i[0]:
                print_clicked = i[1]

        if '<br>' in print_clicked:
            print_clicked = print_clicked.replace('<br>','\n')
        if '<b>' in print_clicked:
            print_clicked = print_clicked.replace('<b>','**')
        if '</b>' in print_clicked:
            print_clicked = print_clicked.replace('</b>','**')
        return print_clicked


if __name__ == '__main__':
    app.run_server(host='127.0.0.1',port=8050,debug=True)

我还创建了一个 Java 程序,它接受用户输入并使用 javafx WebView 面板在 Java 应用程序中显示 Dash-app。

代码的javafx组件是这样的:

private void setGraph(JPanel pane, String title) throws Exception
{
    pane.removeAll();
    JFXPanel jfxPanel = new JFXPanel();
    pane.setLayout(new BorderLayout());
    pane.add(jfxPanel,BorderLayout.CENTER);
    Platform.runLater(() -> {
        StackPane root = new StackPane();
        Scene scene = new Scene(root);
        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        
        boolean result=false;
        while(result==false) {
            try {
                ServerSocket serverSocket = new ServerSocket(8050);
                result=true;
            } catch (IOException e) {
                System.out.println("Could not listen on port: " + 8050);
                // ...
            }
        }
        if (result ==true) {
             webEngine.load("http://127.0.0.1:8050");
             root.getChildren().add(webView);
             jfxPanel.setScene(scene);
        }
        
    });
    pane.updateUI();
        
}

但是,当我第一次运行 Java 代码时,在http://127.0.0.1:8050创建的 Dash 应用程序不显示。(不过,可以在普通的 chrome 浏览器上看到 Dash 应用程序)

当我第二次运行 Java 代码时,Dash 应用程序出现了,只是因为第一次运行时启动的 dash 应用程序没有终止。

我已验证我的 Java 程序的 Javafx 面板部分在构建 Dash 应用程序的 Python 代码运行之后运行。

我的问题:

  1. 我不确定为什么我第一次运行代码时它不显示 Dash 应用程序以及如何解决它......

  2. 每次执行程序时如何终止 Dash 应用程序?

我已经坚持了太久了......任何建议将不胜感激。

标签: javapythonjavafxplotly-dashjavafx-webengine

解决方案


推荐阅读