最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Passing JavaScript variable to Python - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Nov 7, 2012 at 11:41 Fenton 251k73 gold badges400 silver badges409 bronze badges asked May 24, 2010 at 5:34 William S.William S. 911 gold badge1 silver badge2 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 5

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/

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?

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'})

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)

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

发布评论

评论列表(0)

  1. 暂无评论