Home > Net >  how to run python code on html page? How?
how to run python code on html page? How?

Time:10-14

I have working python code to read webcam video and do some processing. I want to call this from HTML page once user clicks button and return value from python code back to a text field on html.

CodePudding user response:

You might need to look into having a backend server and connecting your website to it using websockets. Then you can send individual frames through the websocket to the backend server, process them and return the result.

Look into Socket.io JavaScript client library and Python library. For the backend you could use Flask/Django/FastAPI.

You also have to make sure your OpenCV script is fast enough that it won't lag behind frames, which might prove difficult.

CodePudding user response:

You cannot do this directly from the Javascript code of your website page since it is executed on the computer of the user, and the python script would be executed by your server.

However, when the user clicks the button, you can make a request to another part of your website (or even a separate API). Then it executes your python code, do the required job, and respond to the request.

For example, using your website, you can call Python scripts from PHP.

CodePudding user response:

If you want to run Python code client side, you can compile CPython to WebAssembly using something like emscripten.

  • Related