在PyQt5 Webengine中使用html中的本地文件 [英] Using a local file in html for a PyQt5 webengine

查看:1802
本文介绍了在PyQt5 Webengine中使用html中的本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

以交互模式在qwebview中以绘图方式打开

如果您阅读了该文章,则说明该文章说明您在使用Webengine视图时无法直接在HTML中包含javascript(加载2 MB以上的文件时会出现问题).但是,我正在尝试使其成为javascript的源是plotly-min.js的本地副本(保存在项目文件夹中),以便使用该程序的任何人都不需要互联网连接即可查看图形它产生.我尝试在html上遵循一些示例,但无济于事.

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.

适用于Internet连接的原始代码是:

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">

我已经尝试过:

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

老实说我不懂HTML.这是代码中唯一的HTML(其余的在Python 3中).有谁知道为什么以上这些可能行不通以及我需要更改什么?我怀疑我可能会以某种方式遇到上述问题的2 MB限制,因为我在网上找到了有关如何引用HTML本地文件的不同变体,似乎没有任何改变.

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")

假设.js文件位于您的.py文件旁边:

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

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

您可以使用以下示例:

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_())

这篇关于在PyQt5 Webengine中使用html中的本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆