简体   繁体   English

javascript代码是否有可能获取正在运行的python程序的不断更新的变量?

[英]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 .尝试查看pygletmoviepy 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.

相关问题 不断运行JavaScript? - Constantly running javascript? 是否可以将URL从浏览器上运行的javascript发送到客户端计算机上运行的python程序? - Is it possible to send a URL from javascript running on the browser to the python program running on the client machine? 如何使用Python抓取不断更新的JavaScript后登录信息? - How do I scrape constantly updated JavaScript post-login using Python? Javascript:如何持续监控变量值 - Javascript: How do constantly monitor variables value 如何进行操作(+/-所请求的更改)时变量值不断减小(在javascript:警报框中) - How do I get a variables value to be constantly decreased when operated on (+/- the requested change) (in javascript:alert boxes)) 是否可以使用纯 Javascript 代码从 Azure 获取 ENV 变量? 如果是,如何? - Is it possible to get ENV variables from Azure in plain Javascript code? If yes, how? 是否可以使用 JSCover 或任何其他工具来获得在浏览器中运行 Java Selenium WebDriver 测试的 JavaScript 代码覆盖率? - Is it possible to use JSCover or any other tool to get JavaScript code coverage running Java Selenium WebDriver tests in Browser? 依赖于动态数组的 forEach 无法获取更新值,不可能吗? (JavaScript) - forEach that depends on dynamic array fails to get updated value, is not possible? (JavaScript) 在某些网页上不断执行javascript代码 - Constantly execute javascript code on some webpage 是否可以在运行时或运行后操纵每个Javascript变量,对象? - Is it possible to manipulate every Javascript variables, objects while or after running?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM