Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions bigframes/display/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,33 @@

from __future__ import annotations

try:
import anywidget # noqa
from typing import Any

from bigframes.display.anywidget import TableWidget

__all__ = ["TableWidget"]
except Exception:
pass
def __getattr__(name: str) -> Any:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't typical to do, so could you please add some comments explaining why we defer in this way in the docstring and/or comments as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for suggestion. I add docstring.

"""Lazily import TableWidget to avoid ZMQ port conflicts.

anywidget and traitlets eagerly initialize kernel communication channels on
import. This can lead to race conditions and ZMQ port conflicts when
multiple Jupyter kernels are started in parallel, such as during notebook
tests. By using __getattr__, we defer the import of TableWidget until it is
explicitly accessed, preventing premature initialization and avoiding port
collisions.
"""
if name == "TableWidget":
try:
import anywidget # noqa

from bigframes.display.anywidget import TableWidget

return TableWidget
except Exception:
raise AttributeError(
f"module '{__name__}' has no attribute '{name}'. "
"TableWidget requires anywidget and traitlets to be installed. "
"Please `pip install anywidget traitlets` or `pip install 'bigframes[anywidget]'`."
)
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")


__all__ = ["TableWidget"]