VPython脚本的输出是什么?为什么它会在Chrome浏览器中为我打开?

时间:2019-06-24 08:16:29

标签: python vpython

每当我使用vpython库运行脚本时,可视化效果都会在google chrome标签中打开。

我想知道为什么会这样,vpython可视化的输出是什么,使其在Chrome标签中打开。

1 个答案:

答案 0 :(得分:0)

好吧,如果您打开site-packages/vpython/no_notebook.py,则会看到:

import webbrowser as _webbrowser

就是这样。

在后台,它在“无jupyter笔记本模式”下的作用是启动了一个线程化的本地HTTP服务器,该服务器提供一些JavaScript,然后在Web浏览器中打开一个页面以连接到该服务器。剩下的就是像其他Web应用程序一样,在服务器和客户端之间交换数据。

有关数据交换的更详细说明,请参见site-packages/vpython/vpython.py

# Now there is no threading in Jupyter VPython. Data is sent to the
# browser from the trigger() function, which is called by a
# canvas_update event sent to Python from the browser (glowcomm.js), currently
# every 33 milliseconds. When trigger() is called, it immediately signals
# the browser to set a timeout of 33 ms to send another signal to Python.
# Note that a typical VPython program starts out by creating objects (constructors) and
# specifying their attributes. The 33 ms signal from the browser is adequate to ensure
# prompt data transmissions to the browser.

# The situation with non-notebook use is similar, but the http server is threaded,
# in order to serve glowcomm.html, jpg texture files, and font files, and the
# websocket is also threaded.

# In both the notebook and non-notebook cases output is buffered in baseObj.updates
# and sent as a block to the browser at render times.

老实说,我认为vpython模型不是一个好的API模型(一方面,在普通的交互式shell中使用它很尴尬),但是我想它可以工作。