0

I'm currently building a little GUI using PyQt5 and some html/js scripts that have already been built by someone else for a previous project, and wondering if anyone might be able to help me.

I'm comfortable working in python, but my limited knowledge of html, js and jquery are really making this confusing.

I'm in a situation where I need to be able to take information recorded by the javascript/html and pass it back through to my python script so that I can use it to create a variable for output later on.

I've created a reproducible version below. Currently I'm able to know that gender is being assigned a value when I click, using the buttons. But is there a way that I can pass the value of gender back through to the python script so that I am able to use it there too?

I want to be able to have access to it in successive parts of the app sot hat I can create a profile for the user etc.

pyqtwebtest1.html

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<script>
    $(document).ready(function(){
      $("#new_butt").click(function(){
        $("#para").hide();
        var gender=$ (this).attr('value');
        window.alert(gender)
      });
      $("#other_butt").click(function(){
        $("#para").show();
        var gender=$ (this).attr('value');
        window.alert(gender)
      });
    });
    </script>
</head>
<body>

<h1>My First Heading</h1>
<p id="para">My first paragraph.</p>

<button id ="new_butt" for="Gender_1" value=1>  Click me please!</button>
<button id ="other_butt" for="Gender_2" value=25>  Click me please!</button>

</body>
</html>

pyqtwebtest.py

from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import QtWebEngineWidgets
from pathlib import Path
import sys

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        
        #Create Web View
        view = QtWebEngineWidgets.QWebEngineView()

        html = Path('stack overflow_pass_test/pyqtwebtest1.html').read_text(encoding="utf8")

        view.setHtml(html)
    
        self.setCentralWidget(view)

app = QApplication(sys.argv)

window = MainWindow()
window.show()


app.exec()

I'm wanting to bring all of this information from the javascript/html back through to the python app because I'm much more comfortable working in python, and the successive parts of the app I'm building out really don't rely on the javascript elements. It's just this small part I'm trying to solve.

6
  • 1
    In my opinion, the real question is: is the HTML/js input really required? Is it needed because the contents have to interface with some external web API (some remote database, get/post requests, REST/XML/whatever)? Because if you're doing it just because "it is already done", I see very little point in trying to implement all that using two completely different technologies: rewrite that "input dialog" in PyQt, and it would be simpler (and safer), not to mention the fact that, unless you need the QtWebEngine for something else, you can avoid unnecessary memory requirements it brings along. Commented Feb 22, 2023 at 0:39
  • In short, I can't really get rid of the HTML/js input. The actual HTML/js I'm working with (not shown in the question), is a whole survey instrument tool that I can't really completely redevelop in python at the moment (although I would love to). Is the short answer that there's perhaps no easy way to pass output through to PyQt? Just trying to build a proof of concept app at the moment, so I'm not too worried at the minute about what the best option is in the long run as I'll likely get to redevelop the html/js component in python at some point. Commented Feb 22, 2023 at 0:52
  • Ok. For future reference, ensure that you add those details to the questions, so that we can have a more complete context about what you're asking. Do you have access to the HTML/js and can you possibly modify it in order to adapt it to your needs? Commented Feb 22, 2023 at 1:04
  • Will do! Yep, I've got access to everything to be able to run the HTML/js stuff. I think I'd need to do a little rebuilding here and there, but I should be able to get it in a form where I can modify it Commented Feb 22, 2023 at 1:06
  • Ok, see the posts for which I've marked your question as a duplicate. There might be other related posts, just look for anything related to QWebChannel and ensure that you carefully read its documentation. Commented Feb 22, 2023 at 1:16

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.