|
| 1 | +# Stub ipywidgets for headless/CI execution. |
| 2 | +# Replaces blocking widget calls with no-ops so notebooks execute without hanging. |
| 3 | +# A guard prevents this stub from replacing real ipywidgets if it is already loaded |
| 4 | +# (e.g. when running inside a Colab/Jupyter environment with a real frontend). |
| 5 | +# |
| 6 | +# Installed into ~/.ipython/profile_default/startup/ by the setup-ci-tools action |
| 7 | +# so it runs automatically before any notebook cell when nbconvert spawns a kernel. |
| 8 | +import sys |
| 9 | +import types |
| 10 | +import inspect |
| 11 | + |
| 12 | + |
| 13 | +class _NoOpWidget: |
| 14 | + """A no-op stand-in for any ipywidgets widget class.""" |
| 15 | + |
| 16 | + def __init__(self, *args, **kwargs): |
| 17 | + # Preserve value/options so _Interact can extract call defaults |
| 18 | + object.__setattr__(self, "children", kwargs.get("children", [])) |
| 19 | + object.__setattr__(self, "value", kwargs.get("value", None)) |
| 20 | + object.__setattr__(self, "options", kwargs.get("options", [])) |
| 21 | + |
| 22 | + def __enter__(self): |
| 23 | + return self |
| 24 | + |
| 25 | + def __exit__(self, *args): |
| 26 | + pass |
| 27 | + |
| 28 | + def __setattr__(self, name, value): |
| 29 | + object.__setattr__(self, name, value) |
| 30 | + |
| 31 | + def __getattr__(self, name): |
| 32 | + # Return a no-op callable for any unknown method/attribute |
| 33 | + return lambda *args, **kwargs: None |
| 34 | + |
| 35 | + |
| 36 | +class _Interact: |
| 37 | + """Stub for widgets.interact / widgets.interactive. |
| 38 | +
|
| 39 | + Calls the wrapped function once with default values extracted from |
| 40 | + widget stubs so that matplotlib outputs are captured by nbconvert. |
| 41 | + """ |
| 42 | + |
| 43 | + def __call__(self, *args, **kwargs): |
| 44 | + if args and callable(args[0]): |
| 45 | + # interact(f) or interact(f, param=value, ...) |
| 46 | + return self._call_with_defaults(args[0], kwargs if kwargs else None) |
| 47 | + # interact(param=value) used as decorator factory |
| 48 | + widget_kwargs = kwargs |
| 49 | + |
| 50 | + def decorator(f): |
| 51 | + return self._call_with_defaults(f, widget_kwargs) |
| 52 | + |
| 53 | + return decorator |
| 54 | + |
| 55 | + def _call_with_defaults(self, f, widget_kwargs=None): |
| 56 | + sig = inspect.signature(f) |
| 57 | + call_kwargs = {} |
| 58 | + for name, param in sig.parameters.items(): |
| 59 | + widget = (widget_kwargs or {}).get(name) |
| 60 | + if widget is None and param.default is not inspect.Parameter.empty: |
| 61 | + widget = param.default |
| 62 | + if isinstance(widget, _NoOpWidget) and widget.value is not None: |
| 63 | + call_kwargs[name] = widget.value |
| 64 | + elif widget is not None and not isinstance(widget, _NoOpWidget): |
| 65 | + call_kwargs[name] = widget |
| 66 | + try: |
| 67 | + f(**call_kwargs) |
| 68 | + except Exception as e: |
| 69 | + print(f"[stub] interact call skipped: {e}") |
| 70 | + return f |
| 71 | + |
| 72 | + |
| 73 | +class _StubModule(types.ModuleType): |
| 74 | + """ipywidgets stub module. |
| 75 | +
|
| 76 | + Any attribute access returns _NoOpWidget so that |
| 77 | + 'from ipywidgets import AnythingAtAll' always succeeds. |
| 78 | + """ |
| 79 | + |
| 80 | + interact = _Interact() |
| 81 | + interactive = _Interact() |
| 82 | + |
| 83 | + def __getattr__(self, name): |
| 84 | + if name.startswith("__"): |
| 85 | + raise AttributeError(name) |
| 86 | + return _NoOpWidget |
| 87 | + |
| 88 | + |
| 89 | +# Only install stub if ipywidgets not already in sys.modules |
| 90 | +if "ipywidgets" not in sys.modules: |
| 91 | + stub = _StubModule("ipywidgets") |
| 92 | + stub.widgets = stub # support: from ipywidgets import widgets |
| 93 | + sys.modules["ipywidgets"] = stub |
| 94 | + sys.modules["ipywidgets.widgets"] = stub |
| 95 | + print("ipywidgets stubbed for headless CI execution") |
| 96 | +else: |
| 97 | + print("ipywidgets already loaded, skipping stub") |
0 commit comments