I have a python + electronJS + Djangao app. I am trying to launch a python script with a button click (from the HTML front end).
The app launches fine. But when I click the button to launch the python script from the HTML window, nothing happens (I get a blank HTML page).
Here is my java script file:
let {PythonShell} = require('python-shell')
var path = require("path")
function get_weather() {
let pyshell = new PythonShell('Emotion-recognition-master/real_time_video.py', options);
//let python = spawn('python', [path.join(__dirname, '/home/ironmantis7x/Documents/BSSTLLC/electronJS/electronpydemo1/python-app-with-electron-gui/engine/Emotion-recognition-master', 'real_time_video.py'), obsFilePath, navFilePath]);
pyshell.on('message', function(message) {
swal(message);
})
//document.getElementById("city").value = "";
}
Here is my HTML code for the gui launcher:
<body>
<div class="container">
<div class="jumbotron">
<h1><b><center>MAVERICK Sentry System</center></b></h1>
<h3><i><center>by Maverick AI - a Saudi Company</center></i></h3>
<br>
<br>
<center>
<div class="row">
<div class="col-xs-4">
<img style="width:40%;padding:5px" src="images/weather-icon.png"/>
<br>
<button class="btn btn-info"><a style="color:white" href="weather.html">Weather</a></button>
<div class="col-xs-4">
<img style="width:40%;padding:5px" src="images/emotion_recognition.png"/>
<br>
<button class="btn btn-info"><a style="color:white;" href="real_time_video.py">Emotion Recognition</a></button>
</div>
<div class="col-xs-4">
<img style="width:40%;padding:5px" src="images/text_recognition.png"/>
<br>
<button class="btn btn-info"><a style="color:white;" href="http://127.0.0.1:5000/detect">Text Recognition</a></button>
</div>
</center>
</div>
</div>
<body>
How can I run my python script from html button click properly?
1 Answer 1
First, you need to make sure the button sends a post request when clicked. For example, let's assume the button sends a post request to a url 'execute_script/'.
Then, urls.py should look like:
# route url to view function:
urlpatterns = [
path('execute_script/', views.ExecuteScript, name='execute_script'),
]
And, view.py can look like:
def ExecuteScript(request):
# unpack request if needed:
some_data = request.POST['some_data']
# execute some script here...
# pack context:
context = json.dumps({
'status' : 'Success',
})
# return an HTTP response with context:
return HttpResponse(context)
4 Comments
'execute_script' - then the ExecuteScript python function will be called. You can put any python script/code in this function.