0

I have just started using Flask for my web development awhile ago. I came across numbers of tutorial on how I am able to set up Flask easily. However, I wanted to run a python script upon clicking on an icon. Is there any tutorial out there that I can get linked to?

This is part of the HTML code that I wanted users to click and invoke the python scipt.

<a href="#" class="icon fa-vcard-o"><span class="label">Icon</span></a>

Appreciate any help here.

Thanks in advance!

davidism
  • 121,510
  • 29
  • 395
  • 339
Luke
  • 85
  • 2
  • 2
  • 13

2 Answers2

1

Create an API using flask that accepts a post request, write JavaScript code to make a post request on the API on click of the link, invoke the script through your back-end.

Sushant
  • 3,499
  • 3
  • 17
  • 34
1

You can use JavaScript to create a request to specific route when user clicks icon on your website.

Python:

@app.route('/my-route')
def my_route():
    # Your code here...

HTML:

<a href="#" class="icon fa-vcard-o"><span class="label">Icon</span></a>

JavaScript:

window.onload = function () {
    var xhttp = new XMLHttpRequest();
    var icons = document.getElementsByClassName('icon');
    for (var i = 0; i < icons.length; i++) {
        icons[i].onclick = function () {
            xhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                alert('request successful!');
            }
        };
        xhttp.open("GET", "/my-route", true);
        xhttp.send();
    }
}
vremes
  • 654
  • 2
  • 7
  • 10