3

It is possible to run JavaScript with Python? There are any library that makes this possible?

I need to execute some JavaScript, I know that this is possible with some Java libraries, but I prefer Python.

Can someone give me a clue on this?

Best Regards,

André
  • 24,706
  • 43
  • 121
  • 178

5 Answers5

5

You can check spidermonkey

rubik
  • 8,814
  • 9
  • 58
  • 88
  • Hi, thanks for the reply. It is not possible to call functions. I need to call some Javascript code that call some functions. There are no more options? Best Regards, – André Mar 04 '11 at 16:27
  • From the project summary: "This Python module allows for the implementation of Javascript classes, objects and functions in Python, as well as the *evaluation and calling of Javascript scripts and functions*". – rsenna Mar 04 '11 at 16:38
  • of course it can call functions see def bind_callable(self, name, function) in http://code.google.com/p/python-spidermonkey/source/browse/trunk/spidermonkey.pyx – Xavier Combelle Mar 04 '11 at 16:39
1

If you already use PyQt and QWebView in it, displaying custom html, the function evaluateJavaScript of QWebFrame may be useful for you:

# Python
def runJavaScriptText(self, jsText):
   jsText = 'hello()'  # just to fit javascript example
   self.webView.page().currentFrame().evaluateJavaScript(jsText)


// Javascript
function hello() {
    alert('hello');
};
0

Using spidermonkey would give you a tightier integration of your code, but as a workaround, you could make the javascript get run in a browser using Selenium remote-control:

http://seleniumhq.org/projects/remote-control/ (there are ways of doing that without needing a "physical" display for the browser, using VNC servers, for example)

jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • Thanks for the reply. I see also from this article that are not many possibilities: http://blog.motane.lu/2009/06/18/pywebkitgtk-execute-javascript-from-python/ – André Mar 04 '11 at 16:48
0

Does it need to be CPython ?

And does the Javascript need a browser client environment ?

If not, you can probably call Rhino from Jython.

( Note also that Jython release is only at 2.5.2 )

Steven D. Majewski
  • 2,127
  • 15
  • 16
0

Yes you can execute JavaScript from Python. I find the easiest way is through Python's bindings to the webkit library - here is an example. In my experience selenium and spidermonkey are harder to get working.

hoju
  • 28,392
  • 37
  • 134
  • 178