Plotly.py: Embedd offline plots in PyQt WebView (python)

Created on 18 Mar 2017  路  5Comments  路  Source: plotly/plotly.py

Hi all,
It seems that there are some troubles when embedding an offline plot in a QWebView.
Here the code http://pastebin.com/MSKnCExp

  1. if directly loading the plot in a QWebView then the plot is not visible at all:

browser_001

  1. if the plot is opened in a default browser and then saved as html file from the browser, then the plot can be loaded and it is visible in the QWebView but no interactions are possible:

browser_002

Most helpful comment

In case anyone here is still interested, here's a quick proof-of-concept of how to display a plotly figure in a pyqt5 context. This uses QWebEngineView, which is a full chromium based browser and it seems to support everything plotly.js needs (including WebGL).

import plotly.offline as po
import plotly.graph_objs as go

from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5 import QtCore, QtWidgets
import sys


def show_qt(fig):
    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 += po.plot(fig, include_plotlyjs=False, output_type='div')
    raw_html += '</body></html>'

    fig_view = QWebEngineView()
    # setHtml has a 2MB size limit, need to switch to setUrl on tmp file
    # for large figures.
    fig_view.setHtml(raw_html)
    fig_view.show()
    fig_view.raise_()
    return fig_view


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)

    fig = go.Figure(data=[{'type': 'scattergl', 'y': [2, 1, 3, 1]}])
    fig_view = show_qt(fig)
    sys.exit(app.exec_())

All 5 comments

up...
someone has had similar issues? I can help to find a way to solve it

You found a way? I had the same issue

In case anyone here is still interested, here's a quick proof-of-concept of how to display a plotly figure in a pyqt5 context. This uses QWebEngineView, which is a full chromium based browser and it seems to support everything plotly.js needs (including WebGL).

import plotly.offline as po
import plotly.graph_objs as go

from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5 import QtCore, QtWidgets
import sys


def show_qt(fig):
    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 += po.plot(fig, include_plotlyjs=False, output_type='div')
    raw_html += '</body></html>'

    fig_view = QWebEngineView()
    # setHtml has a 2MB size limit, need to switch to setUrl on tmp file
    # for large figures.
    fig_view.setHtml(raw_html)
    fig_view.show()
    fig_view.raise_()
    return fig_view


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)

    fig = go.Figure(data=[{'type': 'scattergl', 'y': [2, 1, 3, 1]}])
    fig_view = show_qt(fig)
    sys.exit(app.exec_())

Hi @jonmmease thanks for the code snippet. Actually on Linux OS (I'm on Debian Sid, but I'm sure the same problem happens also with Ubuntu's, https://askubuntu.com/questions/763612/importerror-no-module-named-pyqt5-qtwebenginewidgets), QWebEngineView is not available by default and, AFAIK you have to compile it by yourself.

Yeah, as I understand it QWebEngineView is a bear to package. I installed Qt/pyqt using conda on OS X and it was included. I believe the same would be true when using conda on Linux. Not sure about windows though.

Was this page helpful?
0 / 5 - 0 ratings