Calling a Python script from Javascript, both local files

24,388

Solution 1

In order for the python script to execute it has to be deployed by a web server that supports it via CGI or WSGI, etc.

Check out the docs here: webservers

Solution 2

There are three problems with your code.

First, when you call $.ajax(), it tries to parse the response as either JSON or HTML. To prevent it, use dataType: "text".

$.ajax({
    type: "POST",
    url: "111212.py",
    data: { param: " "}, 
    dataType: "text"
    }).done(function( o ) {
        alert("OK");
});

Second, fetching a local file from javascript may violate the Same Origin Policy, depending on the browser. See: Origin null is not allowed by Access-Control-Allow-Origin

An most important, fetching does not execute a file, it just reads it and returns as a string.

Solution 3

So apparently, as has been pointed out, this can't be done, not like this. So I'm going to start a simple CGI python sever to server the HTML file, and execute the script. I've tested it and it works great!

Share:
24,388
Asgeir
Author by

Asgeir

Updated on April 11, 2020

Comments

  • Asgeir
    Asgeir about 4 years

    I'm trying to run a python script from a local javascript file (part of a locally running HTML/javascript program).

    I've been googling this for hours and found a lot of solutions, none of which actually work for me.

    Here is the javascript:

    $.ajax({
    
        type: "POST",
        url: "test.py",
        data: { param: " "}
        }).done(function( o ) {
            alert("OK");
    });
    

    The test.py file is in the same folder as the script/html file.

    here is the script:

    #!/usr/bin/python
    import os
    filepath = os.getcwd()
    def MakeFile(file_name):
        temp_path = filepath + file_name
        with open(file_name, 'w') as f:
        f.write('''\
    def print_success():
        print "sucesss"        
    ''')
        print 'Execution completed.'
    
    MakeFile("bla.txt");
    

    It works fine when run normally.

    On my Firefox console I get a "not well formed" error and the script doesn't create a file. However, I can see that Firefox does fetch the script, as I can view it in my browser by clicking the file name.

    • jgitter
      jgitter about 10 years
      Did you properly indent the f.write() call after the with open()... line in your actual file? Have you tested the python script independent from the web?
    • Asgeir
      Asgeir about 10 years
      Yup, I've tested it simply by double clicking the script file. Works fine and creates the bla.txt file.
    • Anton
      Anton about 10 years
      Are you running a local HTTP server, or just opening a local HTML file in a browser?
    • Asgeir
      Asgeir about 10 years
      I'm opening a local HTML file in a browser.
    • jgitter
      jgitter about 10 years
      In order for the python script to execute it has to be deployed by a web server that supports it via CGI or WSGI, etc. Check out the docs here: docs.python.org/2/howto/webservers.html
    • Asgeir
      Asgeir about 10 years
      Damn, this is a very small project and having a server is not really an option. Any other way to do this?
    • Asgeir
      Asgeir about 10 years
      I started a simple server with CGI and it works. Hopefully it will be acceptable to launch a lightweight localhost to help with the task.