title: How to "render" HTML with PyQt5's QWebEngineView
body: <p>How can I "render" HTML with with PyQt5 v5.6 QWebEngineView?</p>
  
  <p>I have previously performed the task with PyQt5 v5.4.1 QWebPage, but it was <a href="https://stackoverflow.com/q/37711921/4117209">suggested </a> to try the newer QWebEngineView.</p>
  
  <p>Here's that implementation (it generally works as expected, but has a tendency to hang indefinitely for some sites and situations):</p>
  
  <pre><code>def render(source_html):
      """Fully render HTML, JavaScript and all."""
  
      import sys
      from PyQt5.QtWidgets import QApplication
      from PyQt5.QtWebKitWidgets import QWebPage
  
      class Render(QWebPage):
          def __init__(self, html):
              self.html = None
              self.app = QApplication(sys.argv)
              QWebPage.__init__(self)
              self.loadFinished.connect(self._loadFinished)
              self.mainFrame().setHtml(html)
              self.app.exec_()
  
          def _loadFinished(self, result):
              self.html = self.mainFrame().toHtml()
              self.app.quit()
  
      return Render(source_html).html
  
  import requests
  sample_html = requests.get(dummy_url).text
  print(render(sample_html))
  </code></pre>
  
  <p>What follows is my attempt at using QWebEngineView. First, the installation and setup of <a href="https://pypi.python.org/pypi/PyQt5/5.6" rel="noreferrer">PyQt5 v5.6</a> on Ubuntu:</p>
  
  <pre><code># install PyQt5 v5.6 wheel from PyPI
  pip3 install --user pyqt5
  
  # link missing resources
  ln -s ../resources/icudtl.dat ../resources/qtwebengine_resources.pak ../resources/qtwebengine_resources_100p.pak ../resources/qtwebengine_resources_200p.pak ../translations/qtwebengine_locales ~/.local/lib/python3.5/site-packages/PyQt5/Qt/libexec/
  </code></pre>
  
  <p>Now for the Python... The following results in a segmentation fault:</p>
  
  <pre><code>def render(source_html):
      """Fully render HTML, JavaScript and all."""
  
      import sys
      from PyQt5.QtWidgets import QApplication
      from PyQt5.QtWebEngineWidgets import QWebEngineView
  
      class Render(QWebEngineView):
          def __init__(self, html):
              self.html = None
              self.app = QApplication(sys.argv)
              QWebEngineView.__init__(self)
              self.loadFinished.connect(self._loadFinished)
              self.setHtml(html)
              self.app.exec_()
  
          def _loadFinished(self, result):
              # what's going on here? how can I get the HTML from toHtml?
              self.page().toHtml(self.callable)
              self.app.quit()
  
          def callable(self, data):
              self.html = data
  
      return Render(source_html).html
  
  import requests
  sample_html = requests.get(dummy_url).text
  print(render(sample_html))
  </code></pre>
  
  <p>The trouble appears to lie in the call to asynchronous <code>toHtml()</code>. It seems like it should be fairly simple, but I'm at a loss with what to do with it. I see it's been <a href="https://stackoverflow.com/a/37691029/4117209">discussed</a> in the context of C++, but I'm not sure how to translate this to Python. How can I get the HTML out?</p>