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

What is the output of a VPython Script? Why does it open in Chrome for me?

每当我 运行 使用 vpython 库的脚本时,可视化会在 google chrome 选项卡中打开。

我想知道为什么会这样,以及使它在 Chrome 选项卡中打开的 vpython 可视化输出是什么。

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

import webbrowser as _webbrowser

就是这样。

在幕后,它在 "no jupyter notebook mode" 中的作用是启动一个线程化的本地 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),但我想它可以工作.