I am currently trying to create a website with Flask that contains just one button (for now). If I press that button I want to run a python script from a specific path or just the project folder. I already saw some posts about the same topic but none of them could really help me.
I already have some code. This is my Flask app.py
from flask import Flask, render_template, jsonify import test app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') if __name__ == '__main__': app.run(debug=True) And that's my index.html
<!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({ type: 'POST', url: "/test.py", //or some other path like /scripts/test.py dataType: "text", success: function (data) { alert(data) }, error: function (data) { alert(data) } }); } </script> </body> </html> If I run my flask website and click the button I want to execute my test.py saved in my project folder or somewhere else. I don't know if it is possible that way or if there is a better solution. Right now I am still trying with flask but I can't get my test.py to run. When I press the button it just shows me an alert with [object Object]
Basically what I am trying to build is a website with buttons, like a service that runs my scripts in the background that can sometimes take more time to finish. I am not sure if I am misunderstanding the use of ajax in this case. Any help would be nice.
/tasks.pyurl of the flask server and then you need to handle this request in your code , to run the script you want to execute