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?

4
  • 3
    You cannot do it like that. I suggest to use a micro web framework in python like Flask. Then you can relate directories to run codes and functions. Commented Jan 7, 2020 at 18:25
  • Does this answer your question? Call Python function from JavaScript code Commented Jan 7, 2020 at 18:36
  • @RobertMoskal i have seen it before i post this question, my problem is a bit different. Commented Jan 7, 2020 at 18:41
  • 1
    Abdel, it's not different! Good luck. Commented Jan 7, 2020 at 19:45

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

5 Comments

so, there is no any simpler way to do it without having another server that has my python function on it?
the restful part of your answer is not necessary at all. You just need to send an http request.
@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
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
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.
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.

1 Comment

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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.