[英]Is it possible for a javascript code to get the constantly updated variables of a running python program?
I have a python program that tracks the position of a person in a room.我有一个python 程序,可以跟踪房间里一个人的 position 。 This position is saved as a variable (" coordinates "), and constantly updated.
这个position被保存为一个变量(“坐标”),并不断更新。 Based on these coordinates (x and y) I would like a video player to zoom in and out.
基于这些坐标(x 和 y),我希望视频播放器可以放大和缩小。 So if someone is in the front of the room, the movie clip is zoomed in. When he/she is in the back, the footage is zoomed out.
因此,如果有人在房间的前面,则影片剪辑会放大。当他/她在房间的后面时,镜头会缩小。
Now the problem is, I don't really find nice video players in python. I do however see a lot of nice video players in javascript .现在的问题是,我并没有在 python 中找到好的视频播放器。但是我确实在 javascript 中看到了很多不错的视频播放器。 Now my question is: would it be possible for a javascript file to somehow "ask" the python file what the current value of the variable is ?
现在我的问题是: javascript 文件是否有可能以某种方式“询问”python 文件变量的当前值是什么? Is there a regular way to do this?
有没有常规的方法来做到这一点? If this is not the case I will search for a python video module, but really like the javascript one...
如果不是这种情况,我将搜索 python 视频模块,但真的很喜欢 javascript 一个......
Thanks in advance!!!提前致谢!!!
If it's supposed to be vanilla JavaScript, then I'd suggest using sockets and updating the field with an onMessage
event.如果它应该是香草 JavaScript,那么我建议使用 sockets 并使用
onMessage
事件更新该字段。 This would mean that the Python side of the program would be "pushing" the information onto the JavaScript side which would only display the data.这意味着程序的 Python 端会将信息“推送”到仅显示数据的 JavaScript 端。
let yourSocket = new WebSocket("ws://whatever.source");
const yourField = document.getElementById("your_element_id");
yourSocket.onMessage = function(event) {
yourField.innerHTML = event.data;
}
Substitute whatever.source
for your message source (coming from python) and your_element_id
with the ID of the element you want to fill with data.将
whatever.source
替换为您的消息源(来自 python),并将your_element_id
替换为您要填充数据的元素的 ID。
Please refer to https://javascript.info/websocket for the JS part and for the Python side there's https://websockets.readthedocs.io/en/stable/intro.html.请参考https://javascript.info/websocket的 JS 部分和 Python 方面有https://websockets.readthedocs.io/en/stable/intro.html。 .
.
Personally, the way I know of achieving this is writing to a file or a database from python and make Javascript make an ajax call to a handler python (or another format) script in the server, which would return the values in the file to which the variables have been written.就我个人而言,我知道实现此目的的方法是从 python 写入文件或数据库,并使 Javascript 对服务器中的处理程序 python(或其他格式)脚本进行 ajax 调用,该脚本将返回文件中的值变量已经写入。
Although using a video player in python3 would be the best practice.尽管在 python3 中使用视频播放器是最佳实践。 Try looking into pyglet or moviepy .
尝试查看pyglet或moviepy 。 Here is post which focusses on zooming and panning with pyglet.
这是一篇专注于使用 pyglet 进行缩放和平移的帖子。
Also, you can use webSockets (or selenium) to drive the js from python itself.此外,您可以使用 webSockets(或 selenium)从 python 本身驱动 js。 This should help.
这应该有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.