如何从蟒蛇到JavaScript的变量传递 [英] How to pass variable from python to javascript
问题描述
我在蟒蛇处理一个ID(YouTube视频的ID)的服务器,这是code:
I have a server in python to process an id (the id of youtube video), this is the code:
class MessageHandler(tornado.web.RequestHandler):
def get(self, action_id):
print "Message = " + action_id
if action_id == "id":
for ws in opened_ws:
ws.write_message("ID: " + action_id)
return render_template('js/player.js', action_id=json.dumps(action_id))
(...)
我使用返回render_template传的action_id来player.js,这是处理使用从YouTube API膜的功能,code是:
I use the return render_template to pass "action_id" to player.js, which is a function to process films using the API from Youtube, the code is :
var PushPlayer = (function () {
var player = null;
function new_player() {
player = new YT.Player('yt-player', {
videoId: action_id,
}
});
}
(...)
使用的action_id
,我可以有YouTube的视频的一切ID ..但我不知道是谁传的action_id
从Python来javascript..any想法?
With action_id
, i can have everything id of youtube's video.. but i don't know who pass the action_id
from python to javascript..any idea?
谢谢!
推荐答案
它看起来像你使用旋风。您可以在龙卷风模板写入原始的Python。您的player.js文件是在这种情况下,一个旋风模板。你想这样来更新它:
It looks like you're using Tornado. You can write raw Python in a Tornado template. Your player.js file is a Tornado template in this case. You would want to update it like this:
var PushPlayer = (function () {
var player = null;
function new_player() {
player = new YT.Player('yt-player', {
videoId: {{ action_id }},
}
});
}
双花括号内Anthing会像Python由龙卷风进行评估。
Anthing inside the double curly braces will be evaluated as Python by Tornado.
这篇关于如何从蟒蛇到JavaScript的变量传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!