0

I saw another question just like this, How to run python script in HTML?, but I did not understand it. I just want to know how to run a super simple input test code in an html page, does anyone know how to do that. I followed a few YouTube videos, but none of them worked. I just want to run this code in a basic html page:

a = input("What is your favorite color?")
print(a)

Is there a simple way to just run the python code? Thanks!

  • The simple answer is no. Python can be used to generate the pages to send to the client, but can't run on the client while the page is being displayed. – Kemp May 19 '21 at 14:37
  • The browser does not support Python. I believe there was a project that interprets and runs Python code via Javascript, but I don't know its current status. It's also somewhat ludicrous to do this except for extremely special circumstances. – deceze May 19 '21 at 14:39

3 Answers3

1

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.

JueK3y
  • 267
  • 9
  • 22
0

Python is a host-side language, running on the server. It does not run on your web browser, "client side", where languages like JavaScript and TypeScript rule the day.

Mike Robinson
  • 8,490
  • 5
  • 28
  • 41
0

I've never used it in production, but I've used brython before and it worked for what I needed.

iLoveTux
  • 3,552
  • 23
  • 31
  • What is bryhton can it get input – Srikar Anumolu May 19 '21 at 15:32
  • Brython is a Python 3 implementation in javascript. You include their js library in your html and then you can use `` as far as input it can query the state of an html input tag using their python library. You should take a quick look at their documentation. – iLoveTux May 19 '21 at 17:58