Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing JavaScript variable to Python

For example:

#!/usr/bin/python
print "This is python."

print "<script type="text/javascript">
          var pass_to_python = new Number(7)
       </script>"

the_number = pass_to_python???

How do I get the pass_to_python in python?

like image 313
William S. Avatar asked May 24 '10 05:05

William S.


People also ask

How do you pass a JavaScript variable to a Python flask?

Passing a JavaScript Variable to a Python variable in Flask First, we will be forming our HTML page, with a basic input area and JavaScript function to pass the value along. We have an onclick() function that is calling our senduserinfo() JavaScript function.

Can Python interact with JavaScript?

JS2PY works by translating JavaScript directly into Python. It indicates that you may run JS directly from Python code without installing large external engines like V8. To use the module it first has to be installed into the system, since it is not built-in. To use the module it has to be imported.

How do you call a JavaScript function from Python?

Using ajax, send text to a python script on your server. Set up the script to return data in an easy to parse (for js) notation (like JSON) and assign the result to arrOfStrings in the success handler. You can run the official Python interpreter in the browser by compiling it using clang and Emscripten.


1 Answers

With pyv8 you can execute javascript from within Python.

import PyV8

class Global(PyV8.JSClass):
    pass

with PyV8.JSContext(Global()) as ctxt:
    the_number = ctxt.eval("var pass_to_python = new Number(7)")

see http://code.google.com/p/pyv8/

like image 139
Eddy Pronk Avatar answered Oct 02 '22 21:10

Eddy Pronk