在交互式模式下在qwebview中打开绘图 [英] open plotly in qwebview in interactive mode

查看:134
本文介绍了在交互式模式下在qwebview中打开绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python在脱机模式下使用plotly库,我想做的是创建一些绘图,将它们另存为本地html,然后再次加载到QWebView中.

I'm using plotly library in offline mode with python and what I'm trying to do is to create some plot, save them as local html and load in a second moment into a QWebView.

这是带有虚拟变量的箱形图的代码:

This is the code for a boxplot with a dummy variable:

from PyQt5.QtWebKitWidgets import QWebView
import plotly
import plotly.graph_objs as go

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)

fn = '/home/matteo/plot.html'
plotly.offline.plot(fig, filename = fn, 
auto_open = False)

view = QWebView()
view.load(QUrl.fromLocalFile(fn))
view.show()

我面临两个主要问题:

  1. 如果我按原样保留代码,则QWebView不会显示图像中的任何内容:

  1. if I let the code as it is, the QWebView won't show anything like in the image:

如果我使用标准浏览器(例如Firefox)打开html文件,则可以看到该图并与之交互,这很好.但是,如果我将浏览器中的html页面保存在本地目录中,并尝试将保存的文件加载到QWebView中,我可以看到该图,但无法与它进行交互(也许缺少某些Javascript?!):

if I open the html file with the standard browser (Firefox for example), I can see and interact with the plot, and that's fine. But if I save the html page from the browser in a local directory and try to load the saved file into the QWebView I can see the plot, but cannot interact with it (maybe for some Javascript missing?!):

每个人都有一些想法如何将交互式脱机制作的图表嵌入到QWebView中?

Anybody has some ideas how to embed an interactive offline made chart into a QWebView?

推荐答案

好的,我应该找出问题所在.

Ok, I should have find what the problem is.

似乎QWebView加载本地文件有些困难,因为它太重了(简单绘图大约2mb).

Is seems that QWebView has some difficulties to load the local file because it is too heavy (about 2mb for simple plot).

因此,在保存本地文件时,我使用了包含javascript的选项,并再次感谢javascript加载javascript,如

So I used the option to not include the javascript when saving the local file and to load the javascript in a second moment thanks, as described here.

换句话说,创建初始的html标签,包括通过plotly生成的图形的结果,而不包含整个javascript代码,并包括javascript的链接.

In other words, create the initial html tags, include the result of the figure generated by plotly without the whole javascript code, and include the link of the javascript.

通过这种方式,文件非常轻便,QWebView没有打开文件的问题.

In this way the file is super light and QWebView does not have issue to open it.

# create the initial html code
raw_html = '<head><meta charset="utf-8" /></head>''<head><meta charset="utf-8" /><script src="https://cdn.plot.ly/plotly-latest.min.js"></script></head>'

# call the plot method without all the javascript code
raw_html += plotly.offline.plot(fig, filename = fn, include_plotlyjs=False)

# close the body and html tags
raw_html += '</body></html>'

这篇关于在交互式模式下在qwebview中打开绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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