For the implementation of HTML (& JS) in Python (than presumably exactly the opposing) I use pywebview
.
However, I have always tested it locally and never uploaded it to a server.
Regarding your code:
You don't need Python for this, you can do it in JavaScript.
However, since the question is about Python, I'll show you the flow with PyWebView here:
<!DOCTYPE html>
<html>
<body>
<input id="input_field" />
<button onclick="getAnswer()">
<p>Get an answer</p>
</button>
<div id="response-block">
<p id="response-block-text"></p>
</div>
<script>
function getAnswer() {
var inputField = document.getElementById('input_field').value
pywebview.api.show_response(inputField).then(showResponse)
}
function showResponse(response) {
var showResponseText = document.getElementById('response-block')
showResponseText.innerText = response.message
}
</script>
</body>
</html>
import webview
class Api:
def __init__(self):
self._window = None
def set_window(self, window):
self._window = window
def show_response(self, inputField):
response = {'message': inputField}
return response
if __name__ == '__main__':
api = Api()
window = webview.create_window('Color', 'path/to/html-file.html', js_api=api)
webview.start()
Explanation:
The first thing you do is to create an HTML document.
In this document you create an input field
and give it an arbitrary ID (in this case input_field
). You also need a button to send the input and a text field for the return value.
In the JavaScript part a function is set up, which is executed when the button is pressed. With .value
the value of the input field is searched.
We this pass to our API of PyWebView, so the content of the input field is passed to Python. There we compose in the function show_response(self, inputField)
a response message for our return value - text field.
With return response
the message is passed back to JS (i.e. to function showResponse(response)
). In this function we specify where the return text should be shown. If set, the message can be output with .innerText
.