|
| 1 | +# Stub ipywidgets for headless/CI execution. |
| 2 | +# Replaces blocking widget calls with no-ops so notebooks execute without hanging. |
| 3 | +# In Colab/Jupyter with a real frontend, the real ipywidgets is used instead. |
| 4 | +# |
| 5 | +# Installed into ~/.ipython/profile_default/startup/ by the setup-ci-tools action |
| 6 | +# so it runs automatically before any notebook cell when nbconvert spawns a kernel. |
| 7 | +import sys |
| 8 | +import types |
| 9 | +import inspect |
| 10 | + |
| 11 | + |
| 12 | +class _NoOpWidget: |
| 13 | + """A no-op stand-in for any ipywidgets widget class.""" |
| 14 | + |
| 15 | + children = [] |
| 16 | + |
| 17 | + def __init__(self, *args, **kwargs): |
| 18 | + # Preserve value/options so _Interact can extract call defaults |
| 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 len(args) == 1 and callable(args[0]) and not kwargs: |
| 45 | + # Bare @widgets.interact — extract defaults from widget params |
| 46 | + return self._call_with_defaults(args[0]) |
| 47 | + # @widgets.interact(param=slider) — return decorator |
| 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 | +stub = _StubModule("ipywidgets") |
| 90 | +stub.widgets = stub # support: from ipywidgets import widgets |
| 91 | +sys.modules["ipywidgets"] = stub |
| 92 | +sys.modules["ipywidgets.widgets"] = stub |
| 93 | + |
| 94 | +print("ipywidgets stubbed for headless CI execution") |
0 commit comments