File tree Expand file tree Collapse file tree 1 file changed +28
-6
lines changed
Expand file tree Collapse file tree 1 file changed +28
-6
lines changed Original file line number Diff line number Diff line change 1616
1717from __future__ import annotations
1818
19- try :
20- import anywidget # noqa
19+ from typing import Any
2120
22- from bigframes .display .anywidget import TableWidget
2321
24- __all__ = ["TableWidget" ]
25- except Exception :
26- pass
22+ def __getattr__ (name : str ) -> Any :
23+ """Lazily import TableWidget to avoid ZMQ port conflicts.
24+
25+ anywidget and traitlets eagerly initialize kernel communication channels on
26+ import. This can lead to race conditions and ZMQ port conflicts when
27+ multiple Jupyter kernels are started in parallel, such as during notebook
28+ tests. By using __getattr__, we defer the import of TableWidget until it is
29+ explicitly accessed, preventing premature initialization and avoiding port
30+ collisions.
31+ """
32+ if name == "TableWidget" :
33+ try :
34+ import anywidget # noqa
35+
36+ from bigframes .display .anywidget import TableWidget
37+
38+ return TableWidget
39+ except Exception :
40+ raise AttributeError (
41+ f"module '{ __name__ } ' has no attribute '{ name } '. "
42+ "TableWidget requires anywidget and traitlets to be installed. "
43+ "Please `pip install anywidget traitlets` or `pip install 'bigframes[anywidget]'`."
44+ )
45+ raise AttributeError (f"module '{ __name__ } ' has no attribute '{ name } '" )
46+
47+
48+ __all__ = ["TableWidget" ]
You can’t perform that action at this time.
0 commit comments