Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I have a number of python scripts that I have saved on my computer, out of curiosity I have created a html file that just has one button. Instead on going into the terminal and running python <path to script>, I would like to make it so when that button is clicked, is kicks off one of my python scripts. Is this possible/how can it be done?

for example purposes we'll call the script to be run MYSCRIPT.py. I've done a little research but have come up with nothing promising. I'm aware the following code is incorrect but just for a starting point here is my html file.

<!DOCTYPE html> <html>     <body>         <head>             <input type="button" id='script' name="scriptbutton" value=" Run Script " onclick="exec('python MYSCRIPT.py');">          </head>     </body> </html> 
like image 731
Justin Smith Avatar asked Jan 31 '18 22:01

Justin Smith


People also ask

Can you run a Python script from an HTML page?

We can use PHP or Hypertext Preprocessor to run Python scripts in HTML.

How do you call a .py file in HTML?

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. Hope this helpful. thank you.


2 Answers

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.

Hope this helpful. thank you.

like image 138
Phok Chanrithisak Avatar answered Sep 18 '22 21:09

Phok Chanrithisak


Since you asked for a way to complete this within an HTML page I am answering this. I feel there is no need to mention the severe warnings and implications that would go along with this .. I trust you know the security of your .py script better than I do :-)

I would use the .ajax() function in the jQuery library. This will allow you to call your Python script as long as the script is in the publicly accessible html directory ... That said this is the part where I tell you to heed security precautions ...

<!DOCTYPE html> <html>   <head>       </head>   <body>     <input type="button" id='script' name="scriptbutton" value=" Run Script " onclick="goPython()">      <script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>      <script>         function goPython(){             $.ajax({               url: "MYSCRIPT.py",              context: document.body             }).done(function() {              alert('finished python script');;             });         }     </script>   </body> </html> 

In addition .. It's worth noting that your script is going to have to have proper permissions for, say, the www-data user to be able to run it ... A chmod, and/or a chown may be necessary.

like image 43
Zak Avatar answered Sep 19 '22 21:09

Zak