<legend id='gkxK0'><style id='gkxK0'><dir id='gkxK0'><q id='gkxK0'></q></dir></style></legend>

    <small id='gkxK0'></small><noframes id='gkxK0'>

      <bdo id='gkxK0'></bdo><ul id='gkxK0'></ul>
    <tfoot id='gkxK0'></tfoot>

      1. <i id='gkxK0'><tr id='gkxK0'><dt id='gkxK0'><q id='gkxK0'><span id='gkxK0'><b id='gkxK0'><form id='gkxK0'><ins id='gkxK0'></ins><ul id='gkxK0'></ul><sub id='gkxK0'></sub></form><legend id='gkxK0'></legend><bdo id='gkxK0'><pre id='gkxK0'><center id='gkxK0'></center></pre></bdo></b><th id='gkxK0'></th></span></q></dt></tr></i><div id='gkxK0'><tfoot id='gkxK0'></tfoot><dl id='gkxK0'><fieldset id='gkxK0'></fieldset></dl></div>

        在 html 中为 PyQt5 网络引擎使用本地文件

        时间:2023-08-05
          <tbody id='SkXL5'></tbody>

        <small id='SkXL5'></small><noframes id='SkXL5'>

        • <bdo id='SkXL5'></bdo><ul id='SkXL5'></ul>
              • <tfoot id='SkXL5'></tfoot><legend id='SkXL5'><style id='SkXL5'><dir id='SkXL5'><q id='SkXL5'></q></dir></style></legend>

              • <i id='SkXL5'><tr id='SkXL5'><dt id='SkXL5'><q id='SkXL5'><span id='SkXL5'><b id='SkXL5'><form id='SkXL5'><ins id='SkXL5'></ins><ul id='SkXL5'></ul><sub id='SkXL5'></sub></form><legend id='SkXL5'></legend><bdo id='SkXL5'><pre id='SkXL5'><center id='SkXL5'></center></pre></bdo></b><th id='SkXL5'></th></span></q></dt></tr></i><div id='SkXL5'><tfoot id='SkXL5'></tfoot><dl id='SkXL5'><fieldset id='SkXL5'></fieldset></dl></div>
                  本文介绍了在 html 中为 PyQt5 网络引擎使用本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试将绘图图嵌入到 PyQt5 网络引擎视图中.我可以使用以下方法做到这一点:

                  I am trying to embed a plotly graph into a PyQt5 webengine view. I was able to do so using the following:

                  open plotly in qwebview in interactive mode

                  If you read it, the article explains that you can't directly include the javascript in the HTML when using the webengine view (it has issues loading files above 2 MB). However, I'm trying to make it so that the source for the javascript is a local copy of plotly-min.js (saved in the project folder) so that anyone using the program doesn't need an internet connection to view the graphs it generates. I've tried to follow a few examples on html, but to no avail.

                  The original code that does work with an internet connection is:

                  raw_html = '<html><head><meta charset="utf-8" />'
                  raw_html += '<script src="https://cdn.plot.ly/plotly-latest.min.js"></script></head>'
                  raw_html += '<body>'
                  raw_html += plot(fig, include_plotlyjs=False, output_type='div')
                  raw_html += '</body></html>'
                  
                  temp_view = QWebEngineView()
                  temp_view.setHtml(graph)
                  

                  If I understand it correctly, I need to change this part:

                  <script src="https://cdn.plot.ly/plotly-latest.min.js">
                  

                  I've already tried:

                  <script type="text/javascript" src="file:///C:/Users/UserName/PycharmProjects/ProjectFolder/plotly-latest.min.js"></script>
                  

                  I honestly do not know HTML. This is the only HTML in the code (the rest is in Python 3). Does anyone know why the above might not work and what I need to change? I have a suspicion I might somehow be running into the 2 MB limit the above question referenced, as different variations I've found online for how to reference a local file in HTML don't seem to change anything.

                  解决方案

                  Many browsers for security reason disable the loading of local files, but as noted in the following forum you can enable that capability with:

                  sys.argv.append("--disable-web-security")
                  

                  Assuming the .js is next to your .py file:

                  .
                  ├── main.py
                  └── plotly-latest.min.js
                  

                  you can use the following example:

                  import sys
                  from PyQt5.QtWidgets import QApplication
                  from PyQt5.QtWebEngineWidgets import QWebEngineView
                  from PyQt5.QtCore import QDir, QUrl
                  
                  import plotly
                  import plotly.graph_objs as go
                  
                  sys.argv.append("--disable-web-security")
                  app = QApplication(sys.argv)
                  
                  x1 = [10, 3, 4, 5, 20, 4, 3]
                  trace1 = go.Box(x = x1)
                  layout = go.Layout(showlegend = True)
                  data = [trace1]
                  
                  fig = go.Figure(data=data, layout = layout)
                  
                  path = QDir.current().filePath('plotly-latest.min.js') 
                  local = QUrl.fromLocalFile(path).toString()
                  
                  raw_html = '<html><head><meta charset="utf-8" />'
                  raw_html += '<script src="{}"></script></head>'.format(local)
                  raw_html += '<body>'
                  raw_html += plotly.offline.plot(fig, include_plotlyjs=False, output_type='div')
                  raw_html += '</body></html>'
                  
                  view = QWebEngineView()
                  view.setHtml(raw_html)
                  view.show()
                  
                  sys.exit(app.exec_())
                  

                  这篇关于在 html 中为 PyQt5 网络引擎使用本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将 QChartView 插入 ui 下一篇:如何在 PyQt5 中创建 QWebEngineView 的打印预览?

                  相关文章

                  最新文章

                  <legend id='CBhdv'><style id='CBhdv'><dir id='CBhdv'><q id='CBhdv'></q></dir></style></legend>
                1. <i id='CBhdv'><tr id='CBhdv'><dt id='CBhdv'><q id='CBhdv'><span id='CBhdv'><b id='CBhdv'><form id='CBhdv'><ins id='CBhdv'></ins><ul id='CBhdv'></ul><sub id='CBhdv'></sub></form><legend id='CBhdv'></legend><bdo id='CBhdv'><pre id='CBhdv'><center id='CBhdv'></center></pre></bdo></b><th id='CBhdv'></th></span></q></dt></tr></i><div id='CBhdv'><tfoot id='CBhdv'></tfoot><dl id='CBhdv'><fieldset id='CBhdv'></fieldset></dl></div>

                    • <bdo id='CBhdv'></bdo><ul id='CBhdv'></ul>
                    <tfoot id='CBhdv'></tfoot>

                    <small id='CBhdv'></small><noframes id='CBhdv'>