I have created a button in HTML , i want to invoke a external python script when that button is clicked.
and also pass on some variables into that python script if possible.
which method would be the simplest and fastest. TIA
I tried flask but that didnt help me, my html code is being hosted on apache. i cannot / shouldn't (maybe) use flask
CodePudding user response:
You can use ajax to call a python script.
<script>
function callPythonScript() {
$.ajax({
url: "script.py",
type: "POST",
data: {
"var1": "value1",
"var2": "value2"
},
success: function(response) {
console.log(response);
},
error: function(xhr) {
console.log(xhr);
}
});
}
</script>
How to recieve values in python script:
import cgi
form = cgi.FieldStorage()
var1 = form.getvalue('var1')
var2 = form.getvalue('var2')
CodePudding user response:
If you are not using Flask, you can use Django. If you don't want to use Django either, you can use ajax to call the script as Fastnlight has shown above.
In case that does not work, you can use "py-script". It is like javascript. It is in experimental phase but can do simple tasks in your browser window.
link to pyscript: https://pyscript.net/