Is it possible to run a python script from html code without any limitations
I am trying to create a combo of a webpage and a python script which lets users to scrape a particular website/s and display the information using requests module in python with the python script being executed from my server , in such a way that my webpage being the frontend and the python script being the backend
I tried using php with shell_exec function as shown in this answer but all it shows is a blank screen with the code being unexecuted in my browser
I also tried using pyscript but was unable to import python requests since it’s not in their supported packages
2
Answers
There’s a library called Brython (https://brython.info/), it can help you somewhat, but of course consider that there are limitations.
Here’s the example of how to send request to HTTP site:
The better approach would be to create microservice, which would consist of front-end (user inputs URL to parse) and back-end (python logic – requests, etc.).
Sure, I am no pro by any means but this is how I would do it.
With the above, you have a simple endpoint called
http://localhost/api/name-your-endpoint
for example. This can now be called by anything once it’s running. You could usefetch()
oruseEffect()
depending if you’re going vanilla JS or React.Below is the Dockerfile I have used for one of my API projects, this is likely to be different for you but gives you an idea of what to expect. you can find more info here: https://fastapi.tiangolo.com/deployment/docker/
Please bare in mind that this Dockerfile uses pipenv and not standard requirements.txt
Now for the front end, I would use React if you know it, and build something like this, this will have the user call the end point via a button.
Or you could use something like
useEffect()
where the client will call the endpoint when the component loads.Of if you’re using vanilla JS you can just use
fetch()
like so:This might be over kill for what you need but this is how I have done it in the past and works very well. But you would need a way to host and run a Docker container for this to work.
If you are using React you could look at something like Vite.JS and host it on something like Vercel
Below I have included some links for your reference
useState
HookuseEffect
hookThis is also this SO link How can I run a Python script in HTML?