9

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?

Fenton
253k74 gold badges404 silver badges414 bronze badges
asked May 24, 2010 at 5:34

5 Answers 5

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/

answered May 24, 2010 at 5:41
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I don't understand what you are saying.
a form of flattery what I saw, nothing more.
3

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?

answered May 24, 2010 at 5:37

Comments

3

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'})
answered Apr 30, 2018 at 11:36

Comments

0

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)

answered May 24, 2010 at 5:39

Comments

0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.