Global state synchronization failure between JavaScript window and Python namespace
19.5K reputation · 18 Apr 2025, 08:16 UTC
PyScript utilizes Pyodide to bridge the Python interpreter with the browser's JavaScript DOM API. A core requirement for complex web applications is the ability to maintain a consistent global state across both environments, specifically when modifying the JavaScript window object from within a PyScript execution block.
While the runtime provides mechanisms for DOM manipulation, there is uncertainty regarding the deterministic synchronization of global variables. Depending on the PyScript version and the method of assignment, changes made to the JavaScript global scope from Python may not consistently reflect in the Python global namespace during subsequent asynchronous execution cycles.
Technical Constraints
- Execution relies on the browser's event loop to prevent UI blocking.
- The proxy layer between Python and JavaScript introduces potential latency or state mismatch.
What is the documented behavior for ensuring that updates to the window object are immediately and reliably visible to the Python interpreter? Does the current runtime support a bidirectional synchronization mechanism for global state, or must developers implement a manual polling/callback system?