0

I have a problem in calling my python function from my javascript file.

I'm using a simple server with python3 -m http.server, so no actual backend..my javascripts are in the front end.

I have a python file called write.py it has a function that I want to execute from my javascript file

write.py file:

def save_vit(text,name):
    f = open(name + ".xml", "w+")
    f.write(text)
    f.close()

the javascript part that i have tried:(but didn't work)

$.ajax({
        type: "POST",
        url: "write.py",
        data: { text: xmlDoc,name: name}});

xmlDoc is a string that has an xml document.

name is a variable that has the name of the file for the python function.

So my question is, what am I missing to call write.py from my js file or how to call it if this approach is wrong?

Mickael B.
  • 4,755
  • 4
  • 24
  • 48
Abdel-Rahman
  • 539
  • 5
  • 19

3 Answers3

2

You are using http.server to serve the content of a folder, as files, not to execute them, so in the best case scenario, http.server will respond with the content of the write.py file instead of executing it.

You need to create a webserver that has a endpoint where the code of write.py is integrated.

Sebastián Espinosa
  • 2,123
  • 13
  • 23
1

You can't send a POST http request to run a python script.

You need to have the python script running on a server, that is able to take restful API calls, to run the script and return a value.

For example, you can run a small flask app that takes in your POST route, which runs a script and returns the value of the script back to the client that accessed the POST route.

alex067
  • 3,159
  • 1
  • 11
  • 17
  • so, there is no any simpler way to do it without having another server that has my python function on it? – Abdel-Rahman Jan 07 '20 at 18:33
  • the _restful_ part of your answer is not necessary at all. You just need to send an http request. – Ulysse BN Jan 07 '20 at 18:34
  • @Abdel-RahmanEl-Feraly you need to have a server or some micro framework that is able to handle http requests. A python script alone cant interpret a POST request, which is why I recommend flask since its lightweight and easy to setup – alex067 Jan 07 '20 at 18:36
  • i know a server or micro framework would solve it, but i wanted a way(if possible and exist) that solves it without having an actual backend to call the python function @alex067 – Abdel-Rahman Jan 07 '20 at 18:43
  • There are several ways to communicate with services & micro services, and the most common is via api calls, which require a framework able to handle http requests. The other method is via messaging, but this is way too complex for something like this. Basically you would send messages to a message broker, and your python script needs to be able to fetch the message. – alex067 Jan 07 '20 at 18:46
1

Python is running on the server side ... JavaScript is running on the client side.

So, the only way for JavaScript to ask for a Python routine to be run is through an asynchronous AJAX request which you will have to build.

Mike Robinson
  • 8,490
  • 5
  • 28
  • 41
  • the problem is, i'm not using an actual Python backend..i created python server using this line only "python3 -m http.server" , the python file i want to call, is in the directory where i used this line "python3 -m http.server" – Abdel-Rahman Jan 07 '20 at 18:37