Skip to content Skip to sidebar Skip to footer

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. Because it's just a small project, so I don't want to be over

Solution 1:

To extend @Liongold's comment, The full workflow goes like this:

Overview of how this happens

  • The javascript code for a button click gets executed. 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.
  • The python code creates a response, and formats it for sending back to the client.
  • The javascript reads the response as plain text, and decides what it means and how to use it. JSON is a very popular format for exchanging data via AJAX

What you need to do

Either:

  1. Learn a server-side python framework. Flask is lightweight and will probably do what you want. The largest obstacle I've found here is dealing with Cross-origin (CORS) problems. Get started at http://flask.pocoo.org/docs/0.10/quickstart/

OR

  1. See if you can port the python script INTO the browser. 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. If the language difference is your only problem, have a look at http://www.skulpt.org/

Solution 2:

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.

<script>
functionrunScript() {
    var request = newXMLHttpRequest();
    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);
    returnfalse;  
};

document.getElementById('script-button').onclick = runScript;
</script>
This goes to your html file
-----------------------------
<buttontype="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/<pathtoyourprojectonthewebserver>">
    AllowOverride All
    Options Indexes FollowSymLinks Includes ExecCGI
    AddHandler cgi-script .py .pyc
    Order allow,deny
    Allow from all
    Require all granted
</Directory>

Post a Comment for "Run Python Script By Clicking Button In Javascript"