Monday, 22 March 2021

How to 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
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 server.py

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def index():
  return render_template('index.html')

@app.route('/my-link/')
def my_link():
  print ('I got clicked!')

  return 'Click.'

if __name__ == '__main__':
  app.run(debug=True)
-2nd create another subfolder inside previous folder and name it as templates file will be your html file
index.html
<!doctype html>
<head><title>Test</title> 
    <meta charset=utf-8> </head>
    <body>
        <h1>My Website</h1>
        <form action="/my-link/">
            <input type="submit" value="Click me" />
        </form>
        
        <button> <a href="/my-link/">Click me</a></button>

    </body>
3.. To run, open command prompt to the New folder directory, type python server.py to run the script, then go to browser type localhost:5000, then you will see button. You can click and route to destination script file you created.

No comments:

Post a Comment