'Flask and PyQt html rendering

I am trying to build a proxy service in Flask where I can pass a url and I get back the result from the Flask server. This is set up and woks well. Issue is I want to add the ability to render javascript in the HTML. I have used PyQt5 WebEngine for a previous project and was really happy with the results.

I've tried to bring the two together as you will see in the snippets below but my understanding now is that I can't run both PyQt and Flask at the same time.. What I want to be able to do is send a url to PyQt for rendering if the Flask route receives render=True.

Flask snippet:

@app.route('/', defaults={'path': '', 'render': False}, methods=['GET', 'POST', 'PUT', 'DELETE'])
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def request_handler(path, headers=None, data=None, render=False):
    if not headers:
        get_headers()

    
    render = request.args.get('render')

    if request.method == 'GET':
        response = get(f'https://{path}', headers=headers)
        if render:
            # https://gist.github.com/brbsix/c71d8f8643edbb23d0a86ec991ac4acc
            resp = Render(f'https://{path}')
            print(resp)
            return resp.html
        else:
            return response.content

Render():

class Render(QWebEnginePage):
    def __init__(self, url):
        self.app = QApplication(sys.argv)
        QWebEnginePage.__init__(self)
        self.html = ''
        self.loadFinished.connect(self._on_load_finished)
        print('url: ', url)
        self.load(QUrl(url))
        self.app.exec_()

    def _on_load_finished(self):
        print('converting to html')
        self.html = self.toHtml(self.Callable)
        print('Load finished')

    def Callable(self, html_str):
        self.html = html_str
        self.app.quit()

So my question is. How can I get this current setup working..?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source