William S.
William S.

Reputation: 91

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?

Upvotes: 9

Views: 39417

Answers (5)

Johnny Gracious
Johnny Gracious

Reputation: 1

I think using JSON is the best way.you can create a JSON file as intermidiary between JavaScript and Python, both languages can access and modify JSON file

Upvotes: 0

user9501250
user9501250

Reputation:

i am using flask and ajax to pass values from javacript to python

function pass_values() {
   var pass_to_python = new Number(7)

                $.ajax(
                {
                    type:'POST',
                    contentType:'application/json;charset-utf-08',
                    dataType:'json',
                    url:'http://127.0.0.1:5000/pass_val?value='+pass_to_python ,
                    success:function (data) {
                        var reply=data.reply;
                        if (reply=="success")
                        {
                            return;
                        }
                        else
                            {
                            alert("some error ocured in session agent")
                            }

                    }
                }
            );
}

python:

@app.route('/pass_val',methods=['POST'])
def pass_val():
    name=request.args.get('value')
    print('name',name)
    return jsonify({'reply':'success'})

Upvotes: 3

Quentin
Quentin

Reputation: 943142

HTTP is a simple request-response protocol, it doesn't let you pause mid-stream and wait for more information from the client — and since your JS runs in the browser (JS can run on the server, but most people wouldn't be attempting this if they didn't need the code to run in the browser, so I'm assuming that using server side JS is out of the question) and the Python runs on the server, that is what you need for your code to work (as well as fixing your broken quote nesting in the Python code).

You need to load the complete document, and then issue a new HTTP request.

This might involve having the JS set location.href (making sure you have a fallback for non-JS clients), it might involve using XMLHttpRequest to load new data asynchronously, it might be best using another technique (it is hard to say for sure as your example simplifies too much to tell what X is)

Upvotes: 0

Eddy Pronk
Eddy Pronk

Reputation: 6695

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/

Upvotes: 5

chris12892
chris12892

Reputation: 1644

You can GET or POST to the Python script. If you need to do this dynamically, you can use AJAX.

Here is a good link: How are POST and GET variables handled in Python?

Upvotes: 3

Related Questions