简体   繁体   English

通过单击javascript中的按钮运行python脚本

[英]Run python script by clicking button in javascript

What I want is run python script just click on the button in the html page and show the python code result on my page. 我想要的是运行python脚本,只需单击html页面中的按钮,然后在我的页面上显示python代码结果。

Because it's just a small project, so I don't want to be overkill learning Django or other web frames even though I know it will work. 因为这只是一个小项目,所以即使我知道它可以工作,我也不想过分学习Django或其他网络框架。

I made some searches, ajax seems the right solution for me, but I don't know how to execute python code by ajax. 我进行了一些搜索,ajax似乎是我的正确解决方案,但我不知道如何通过ajax执行python代码。 I know I can get some string back via ajax using following code: 我知道我可以使用以下代码通过ajax返回一些字符串:

function loadXMLDoc()
{
    var xmlhttp;
    xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","test1.txt",true);
    xmlhttp.send();
}

Thanks in advance for anyone who can help. 预先感谢任何可以提供帮助的人。

To extend @Liongold's comment, The full workflow goes like this: 为了扩展@Liongold的评论,完整的工作流程如下:

Overview of how this happens 概述如何发生

  • The javascript code for a button click gets executed. 按钮单击的javascript代码将执行。 This code is running on the client from a browser. 此代码从浏览器在客户端上运行。
  • The AJAX request gets sent over the internet just like an HTTP request, which is interpreted by a web application running on the computer that will run the Python code. AJAX请求就像HTTP请求一样通过Internet发送,该请求由运行Python代码的计算机上运行的Web应用程序解释。
  • The python code creates a response, and formats it for sending back to the client. python代码创建响应,并将其格式化以发送回客户端。
  • The javascript reads the response as plain text, and decides what it means and how to use it. javascript将响应作为纯文本读取,并确定其含义以及使用方法。 JSON is a very popular format for exchanging data via AJAX JSON是一种非常流行的格式,用于通过AJAX交换数据

What you need to do 你需要做什么

Either: 要么:

  1. Learn a server-side python framework. 了解服务器端python框架。 Flask is lightweight and will probably do what you want. Flask很轻巧,可以满足您的需求。 The largest obstacle I've found here is dealing with Cross-origin (CORS) problems. 我在这里发现的最大障碍是处理跨域(CORS)问题。 Get started at http://flask.pocoo.org/docs/0.10/quickstart/ http://flask.pocoo.org/docs/0.10/quickstart/开始使用

OR 要么

  1. See if you can port the python script INTO the browser. 看看是否可以将python脚本移植到浏览器中。 Does the code need to be run on a specific computer ( the server ) or could it theoretically be converted into javascript and run within the webpage. 该代码是否需要在特定的计算机(服务器)上运行,或者理论上可以将其转换为javascript并在网页内运行。 If the language difference is your only problem, have a look at http://www.skulpt.org/ 如果唯一的语言差异是问题,请访问http://www.skulpt.org/

I ran into a similar problem, and after searching for several hours, this is how i solved it. 我遇到了类似的问题,经过几个小时的搜索,这就是我解决的方法。 Assuming that the html file and the python file are the same folder. 假设html文件和python文件是同一文件夹。

 <script> function runScript() { var request = new XMLHttpRequest(); request.onreadystatechange = function() { if (request.readyState === 4) { if (request.status === 200) { alert('Successful .... ' + request.responseText); } else { alert('Something went wrong, status was ' + request.status); } } }; request.open('POST', 'test.py', true); request.send(null); return false; }; document.getElementById('script-button').onclick = runScript; </script> 
 This goes to your html file ----------------------------- <button type="button" id="script-button"> add this line at the top of your python file --------------------------------------------- test.py ------------ #!C:\\Python34\\python.exe -u print("Testing 123") add this directive to httpd.conf (Apache) ----------------------------------------- # "C:/xampp/cgi-bin" should be changed to whatever your ScriptAliased # CGI directory exists, if you have that configured. # <Directory "C:/xampp/<path to your project on the web server>"> AllowOverride All Options Indexes FollowSymLinks Includes ExecCGI AddHandler cgi-script .py .pyc Order allow,deny Allow from all Require all granted </Directory> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM