[英]How do I execute a Python script in Django using javascript
I have a python + electronJS + Djangao app.我有一个 python + electronJS + Djangao 应用程序。 I am trying to launch a python script with a button click (from the HTML front end).
我正在尝试通过单击按钮(从 HTML 前端)启动一个 python 脚本。
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).
但是当我单击按钮从 HTML 窗口启动 python 脚本时,没有任何反应(我得到一个空白的 HTML 页面)。
Here is my java script file:这是我的java脚本文件:
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:这是我的 gui 启动器的 HTML 代码:
<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?如何从 html 按钮正确单击运行我的 python 脚本?
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/'.
例如,假设按钮向 url 'execute_script/' 发送一个 post 请求。
Then, urls.py should look like:然后,urls.py 应该如下所示:
# route url to view function:
urlpatterns = [
path('execute_script/', views.ExecuteScript, name='execute_script'),
]
And, view.py can look like:而且,view.py 看起来像:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.