1

I have a program that generates plots and displays them to the user using matplotlib. I'm now adding a feature that involves an HTML that is generated from some data and modified. I can display this modified HTML using webbrowser.open('file://' + htmlFilePath, new=2, autoraise=False), and it would open in the default browser or whatever browser was configured for webbrowser.

BUT I'd really like to display this html within the matplotlib window that I have running instead of opening a separate browser window. Is there any way to display this HTML to the matplotlib window? Either as a rendered HTML or even just a picture of the HTML page that would be rendered by a browser.

I don't have much code for what I've tried, as most of the research on stackoverflow is showing me how to display a matplotlib plot AS an html for flask/django, meaning PLOT to HTML instead of HTML to PLOT. I don't need to convert my plot to HTML, I need to convert my HTML to some sort of image or numpy array or something like that so that I can display it like ax.imshow(htmlFilePath). Thanks.

6
  • What would that HTML bit be showing? Since matplotlib cannot render HTML by itself, this sounds like a workaround which might be best solved differently altogether. Commented Mar 15, 2019 at 14:52
  • I see, as I suspected. The HTML is generated from a searchable PDF that is parsed to html using pdftohtml. It's a one page view, The format of the pdf needs to be preserved, which is why I don't want to just show the text generated from pdftotext. Commented Mar 15, 2019 at 15:22
  • I still don't have a clear view of the expected outcome. Would the text be shown on top/inside the matplotlib figure or next to it? Does the matplotlib plot need to be interactive or is it merely a picture? Is the conversion to HTML necessary, or could you directly convert from pdf to a png image? Commented Mar 15, 2019 at 15:30
  • PDF to HTML is done so that different modifications can be made (e.g. find the tag with a certain element in it and highlight it). The plot does not need to be interactive. The user needs to see the modifications made and verify with their eyes and input the result of verification on the console. I think if it converts to a PNG, I wouldn't be able to find the regions to modify (e.g. find the region with the text "foo" in it and highlight it yellow). Commented Mar 15, 2019 at 16:26
  • Would this be close to what you're after? Commented Mar 15, 2019 at 16:53

1 Answer 1

2

You can show a webbrowser and a matplotlib figure next to each other in the same GUI window.

For example, using PyQt5, the following would create a window and place a webbroser left, and a matplotlib figure to the right.

import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5 import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvas, NavigationToolbar2QT
from matplotlib.figure import Figure

class ApplicationWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self._main = QtWidgets.QWidget()
        self.setCentralWidget(self._main)
        layout = QtWidgets.QHBoxLayout(self._main)

        self.web = QWebEngineView()

        self.fig = Figure(figsize=(5, 3))
        self.canvas = FigureCanvas(self.fig)

        layout.addWidget(self.web)
        layout.addWidget(self.canvas)
        self.addToolBar(NavigationToolbar2QT(self.canvas, self))

        self.fill_content("<html><div>Some Html</div></html>", [1,3,2])

    def fill_content(self, html, data):
        self.web.setHtml(html)

        self.fig.clear()
        ax = self.fig.add_subplot(111)
        ax.plot(data)
        self.canvas.draw_idle()


if __name__ == "__main__":
    qapp = QtWidgets.QApplication(sys.argv)
    app = ApplicationWindow()
    app.show()
    qapp.exec_()

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I do need to wrap the whole program into a GUI soon, and maybe this is the time to do it where it necessitates a GUI in order to make the display smooth. I suppose a GUI solution is the right solution since my problem is inherently a display window problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.