Всякий раз, когда я запускаю скрипт с использованием библиотеки vpython, визуализация открывается на вкладке Google Chrome.
Я хочу знать, почему это так, и каков результат визуализации vpython, который заставит его открыться на вкладке Chrome.
Всякий раз, когда я запускаю скрипт с использованием библиотеки vpython, визуализация открывается на вкладке Google Chrome.
Я хочу знать, почему это так, и каков результат визуализации vpython, который заставит его открыться на вкладке Chrome.
Итак, если вы откроете site-packages/vpython/no_notebook.py
, вы увидите:
import webbrowser as _webbrowser
Так вот что.
Под капотом то, что он делает в «режиме ноутбука без jupyter», заключается в том, что он запускает многопоточный локальный HTTP-сервер, который обслуживает некоторый javascript, а затем открывает страницу в вашем веб-браузере для подключения к этому серверу. Остальное — это просто обмен данными между сервером и клиентом, как и любое другое веб-приложение.
Более подробное объяснение обмена данными можно найти в 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 (во-первых, ее неудобно использовать из обычной интерактивной оболочки), но я думаю, что она работает.