0

I've searched this subject already for a bit on the Internet and I couldn't find anything decent:
I was wondering if it possible to call a single Python function from a Python file from JavaScript?

I already came across suggestions such as $ajax-requests, (Call Python function from Javascript code) but this only seems to execute the __main__ method of said Python file. Other suggestions I came across also just execute the entire Python file, without the possibility of just executing a single method of it.

What I want is for example a Python-class containing methods this:

def testMethod(self):
    print "testMethod called from JavaScript"
    return "It worked!"

and that I would be able to call this testMethod() from JavaScript (optionally with parameters etc. of course if the method expects them).

Is this possible in a simple way? I mention the word simple because frameworks like Flask etc. would be my last resort, I'd much more prefer something fast and doesn't change my webproject too much for the rest.

Community
  • 1
  • 1
Fluppe
  • 479
  • 7
  • 22
  • Are you using any particular python web framework ? Using Flask it would be quite simple. – iJade Jul 30 '15 at 11:43
  • See http://stackoverflow.com/questions/21942320/calling-a-local-python-script-from-javascript –  Jul 30 '15 at 11:43
  • 1
    I think you should use Flask and [this](http://codehandbook.org/python-flask-and-jquery-ajax-post/). should help – iJade Jul 30 '15 at 11:44
  • Why? While there are certainly use cases, it would be much easier/better to do it in JavaScript. If you absolutely must do this, I'd recommend looking into spawning a child process. Pass it command line arguments that trigger the specific function you want, with the specified arguments you want – Dan Oberlam Jul 30 '15 at 11:45
  • No. AngularJS is being used as JavaScript framework though. But I have to call certain specific methods from my Python classes that can't be written in JavaScript – Fluppe Jul 30 '15 at 11:45

0 Answers0