How can I execute a python script from an html button?

There are various ways to make it done, very simple technique with security peace in mind, here might help you

1. First you need to install Flask
pip install flask or pip3 install flask
in your command prompt, which is a python microframework, don’t be afraid that you need to have another prior knowledge to learn that, it’s really simple and just a few line of code.
If you wish you learn Flask quickly for complete novice here is the tutorial that I also learn from Flask Tutorial for beginner (YouTube)

2. Create a new folder

  • 1st file will be
from flask import Flask, render_template
app = Flask(__name__)

def index():
  return render_template('index.html')

def my_link():
  print ('I got clicked!')

  return 'Click.'

if __name__ == '__main__':
  • 2nd create another subfolder inside previous folder and name it as templates and create index.html file inside it
<!doctype html>

    <meta charset=utf-8> </head>
        <h1>My Website</h1>
        <form action="">
            <input type="submit" value="Click me" />
        <button> <a href="">Click me</a></button>


3. To run, open command prompt to the New Folder you created earlier, type python or python3 to run the script, then you will get response in your terminal/command prompt that server is running on or any other ports. After that you go to browser type localhost:5000 or, then you will see button. You can click and route to destination script file you created.

Hope this helpful. thank you.

Leave a Comment