简体   繁体   English

将 JavaScript 变量传递给 Python

[英]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?如何在 python 中获取 pass_to_python?

With pyv8 you can execute javascript from within Python. 使用pyv8,您可以在Python中执行javascript。

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/ 请参阅http://code.google.com/p/pyv8/

You can GET or POST to the Python script. 您可以GETPOST到Python脚本。 If you need to do this dynamically, you can use AJAX. 如果您需要动态执行此操作,则可以使用AJAX。

Here is a good link: How are POST and GET variables handled in Python? 这是一个很好的链接: 如何在Python中处理POST和GET变量?

i am using flask and ajax to pass values from javacript to python 我正在使用flask和ajax将值从javacript传递给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). HTTP是一个简单的请求 - 响应协议,它不允许您暂停中间流并等待来自客户端的更多信息 - 并且因为您的JS在浏览器中运行(JS可以在服务器上运行,但大多数人不会尝试这个,如果他们不需要代码在浏览器中运行,所以我假设使用服务器端JS是不可能的)并且Python在服务器上运行,这就是你的代码所需要的工作(以及修复嵌套在Python代码中的损坏的引用)。

You need to load the complete document, and then issue a new HTTP request. 您需要加载完整的文档,然后发出新的HTTP请求。

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 ) 这可能涉及让JS设置location.href确保你有非JS客户端的后备 ),它可能涉及使用XMLHttpRequest异步加载新数据,最好使用另一种技术(很难肯定地说)因为你的例子简化太多了,不知道X是什么

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 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM