Running a Python Program in the backend on an HTML button click

Viewed 292

I am developing a location based web app/website. The website involves an HTML webpage, which has a button which when clicked by the user should:

  1. Extract the user location coordinates
  2. Run a python Program whose input will be the extracted user location, perform some manipulation.
  3. Finally display real-time results back to the html webpage/ i.e. user computer screen.

The python Program is currently accepting the user location by using geocoder function. The program is successfully running on my system/PC.

I have an AWS EC2 Ubuntu server, to which the location of the vehicle GPS device is being sent.

Finally once everything is set up, I will be hosting/deploying the website so that individual users can test it on their system. I am aware of the hosting part.

Can someone please tell me how do I get along with the task of running the python program on an HTML button click and sending back real time results (which is the output of the python program) back to the html webpage?

2 Answers

To run python code on a buttonClick, you can set a href attribute to a link, which you catch in your flask backend. For example <a href="http://myapp/runthiscode"/> in the HTML and app.rout("/runthiscode") in flask. After that, the manipulation, you can give the updated variables to the page, by redirecting with redirect("/", args=args). args are the updated variables, which you can use in the HTML. For example like this: <a> {{ args }} </a>

The simplest way I can think of doing this is creating a basic API endpoint to do this data processing and return it to your frontend.

The process will look like:

  1. User clicks button
  2. Grab the user's longitutde and latitude (most likely using this, though from the sounds you might be able to do this already)
  3. Submit a POST request to your Flask backend
  4. Render the response in the frontend

Jonas answer gives you a rough sense of what this would look like; the only thing I would add is a tutorial. This one from FreeCodeCamp looks great.

Related