简体   繁体   English

如何使用 javascript 在 Django 中执行 Python 脚本

[英]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.

相关问题 如何使用 selenium 在 python 中执行 JavaScript 代码? - How do I execute a JavaScript code in python using selenium? 如何在 Heroku 上执行 python 脚本? - How do I execute a python script on Heroku? 如何在我的 Django 项目的上下文中执行任意脚本? - How do I execute an arbitrary script in the context of my Django project? 如何从python提示符执行(不导入)python脚本? - How do I execute (not import) a python script from a python prompt? 如何从Javascript执行Python脚本? - How can I execute a Python script from Javascript? 尝试使用子进程执行Python脚本(Django) - Trying to Execute Python Script Using Subprocess (Django) 使用AJAX执行Python Django脚本 - Using AJAX to execute Python Django Script 如何使用自定义命令从命令行执行python脚本? - How do I execute a python script from the command line using custom commands? 如何使用在同一网络上的PC上运行的另一个Python脚本在RPi上执行一段代码或Python脚本? - How do I execute a piece of code or a Python script on an RPi using another Python script running on a PC on the same network? 如何从我的python脚本执行fabfile? - How do I execute a fabfile from my python script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM