Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Plotly charts with many points won't load in QWebEngine, but will in Notebook



  • I have an application using PyQt5 that allows users to plot spectra using Plotly (scatter traces) in a QWebEngineView. I noticed that sometimes when I try to plot new spectra, the plot won't update (I verified the code is actually running those lines, though). I tracked it down to the number of points being plotted. Some spectra are much higher resolution than others, so it it wasn't always clear that a huge number of points were being plotted. But then I noticed that the exact same charts that couldn't be plotted in QWebEngineView COULD be plotted directly in a Jupyter Notebook. Some simple test cases (mashed from learnpyqt.com and Plotly site) below illustrate the issue:

    This code opens a simple QWebEngineView and creates a plot. If I make it exceed ~50,000 points (num_trace * point_num), then I just get a blank window.

    from PyQt5.QtCore import *
    from PyQt5.QtWidgets import *
    from PyQt5.QtGui import *
    from PyQt5.QtWebEngineWidgets import *
    import plotly.graph_objects as go
    import numpy as np
    
    
    import sys
    
    class MainWindow(QMainWindow):
    
        def __init__(self, *args, **kwargs):
            super(MainWindow,self).__init__(*args, **kwargs)
    
            self.fig = go.Figure()
    
            trace_num = 10
            point_num = 4000
            for i in range(trace_num):
                self.fig.add_trace(
                    go.Scatter(
                            x = np.linspace(0, 1, point_num),
                            y = np.random.randn(point_num)+(i*5)
                    )
                )
    
            self.fig.update_layout(showlegend=False)
    
            self.browser = QWebEngineView()
            self.browser.setHtml(self.fig.to_html(include_plotlyjs='cdn'))
    
            self.setCentralWidget(self.browser)
    
            self.show()
    
    app = QApplication(sys.argv)
    window = MainWindow()
    
    app.exec_()
    

    Now, if I just take the exact same plotting code (minus the PyQt stuff), and have it plot directly in Jupyter Notebook, I can easily plot more than 50,000 points (it may take a few seconds to render).

    import plotly.graph_objects as go
    
    import numpy as np
    
    fig = go.Figure()
    
    trace_num = 10
    point_num = 5000
    for i in range(trace_num):
        fig.add_trace(
            go.Scatter(
                    x = np.linspace(0, 1, point_num),
                    y = np.random.randn(point_num)+(i*5)
            )
        )
    
    fig.update_layout(showlegend=False)
    
    fig.show() 
    

    I have found references online to Plotly having issues rendering scatterplots with more than 50,000 points, in which case Scattergl should be used. But that's opened another can of worms for me. Yesterday afternoon when I tried this, the PyQt app would launch, but it'd be a blank window with a spinning icon til I killed it. This is the exact same issue I've had before trying to use Qt Designer (it'd hang if I tried to insert one) and anything with QWebEngineView, but that "magically" resolved itself. I got frustrated yesterday evening, and put my laptop to sleep for the night. This morning, I woke up my laptop, and the Scattergl calls worked just fine. No hanging windows. But I still can't plot more than 50,000 points (to try yourself, just replace Scatter with Scattergl in the code).

    So, seems to be two (maybe related?) problems:

    1. I can plot more than 50,000 points directly in Notebook, but not in a QWebEngineView. Why not?
    2. I have some crazy, intermittent issue that seems to be related to OpenGL (but I'm not sure) that causes apps with QWebEngineView to hang when launched.

    #1 is my more pressing issue, but any insight on #2 greatly appreciated as well.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Which version of PyQt5 are you using ?
    How did you install it ?
    On which OS ?
    Do you have the same issue if using PySide2 ?
    What graphics hardware do you have ?



  • Great questions!

    PyQt 5.9.2, installed using conda
    Windows 10 Pro (19041.746)
    NVIDIA GeForce RTX 2060 (Game Ready Driver, Version 457.09)

    Have never used PySide2 before, am pretty new at building Python apps. Will give that a try and report back what happens.


Log in to reply