2

Is there any way or tips to call a Python file (or functions) winthin a .js file (called on a HTML web page).

Indeed, I did the following tip : - configure my server (it's Apache) to run Python files (CGI) - created a Python file on the following structure :

print("<head>....</head>")
print("<body>....</body>")

But what I want is to call a Python file (a real one, with classes, functions or other things) on the Javascript. Any ways to do it ? (something with Ajax or other stuffs)

Thanks, Clément

EDIT

Here's what I want to achieve :

my structure :

1) HTML/CSS/JS files to create an interactive chat

2) a Python file to process the answers on the chat

My goal

On each post on the chat, a Javascript function of my .js file is called. This function need to run my Python file, and my Python file is supposed to return something. The Python file is a pure Python file, no HTML or other things.

What I've done

I created a server thanks to XAMPP. Moreover and as I said before, I tried the thing with print("...") on each line, but it's by far not what I want.

EDIT 2

I already tried a thing with AJAX like :

$(".mytext").on("keyup", function(e){
    if (e.which == 13){
        var text = $(this).val();
        if (text !== ""){
            insertChat("you", text);              
            $(this).val('');
            $.ajax({
                type: "POST",
                url: "test.py"

    });
        }
    }
});

but I get an error : The error I get is on the HTML console, in French :

Erreur d’analyse XML : mal formé
Emplacement : http://localhost/test/test.py
Numéro de ligne 1, Colonne 2 

so in English:

Error of XML analysis : malformed
Path : http://localhost/test/test.py
Line 1, column 2
Clément B
  • 31
  • 4
  • If you want to use python to generate websites, your best bet would probably be to go with one of the established web frameworks, e.g. web2py or django – ksbg Oct 18 '17 at 07:51
  • I don't want Python to generate websites, but I want to call my Python file (which is a pure python file) thanks to my javascript file. – Clément B Oct 18 '17 at 07:56
  • Ok I understand now I think. I don't want to post this as an answer, but the topics you'll probably want to research are REST APIs and AJAX. What you basically want to do, is build a REST API with python, which you can call via AJAX in javascript. So your Python code will run on a server, which you access with an AJAX call via JavaScript – ksbg Oct 18 '17 at 08:00
  • Ok, I read a tutorial about Django & Ajax. I'll try this on my free time. Thanks treegarden & neuhaus :) – Clément B Oct 18 '17 at 08:16

1 Answers1

2

JavaScript runs in your browser. Python cannot run in your browser. The Javascript code can create requests to a server. On the server you can run all kinds of code, including python. The python script(s) have to reside on the server and the server has to be configured accordingly (see WSGI, CGI, "How to use python on the web").

Edit: Answer to updated question:

What you want is to create XmlHttpRequests from JavaScript to the python chat program running on the server. The data format will be JSON (not XML). This technique is called AJAX ("Asynchronous javascript and XML"). I suggest you read some tutorials on these topics. The web framework for Python is called Django.

neuhaus
  • 3,886
  • 1
  • 10
  • 27