Skip to content

Commit 6318831

Browse files
committed
test and changelog entry
1 parent c691390 commit 6318831

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55
## Unreleased
66

77
### Fixed
8+
- Fix Colab renderer detection to use `COLAB_NOTEBOOK_ID` env var instead of importing `google.colab`, so the `colab` renderer is only set for Colab web notebooks and not for other environments like the Colab VS Code extension [[#5473](https://github.com/plotly/plotly.py/pull/5473)]
89
- Fix issue where user-specified `color_continuous_scale` was ignored when template had `autocolorscale=True` [[#5439](https://github.com/plotly/plotly.py/pull/5439)], with thanks to @antonymilne for the contribution!
910
- Update tests to be compatible with numpy 2.4 [[#5522](https://github.com/plotly/plotly.py/pull/5522)], with thanks to @thunze for the contribution!
1011

tests/test_io/test_renderers.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import os
23
import threading
34
import time
45

@@ -418,3 +419,78 @@ def test_missing_webbrowser_methods(fig1):
418419
finally:
419420
# restore everything after this test
420421
webbrowser.get = removed_webbrowser_get_method
422+
423+
424+
def test_colab_renderer_when_env_var_is_set():
425+
"""
426+
When COLAB_NOTEBOOK_ID is present the default renderer should be 'colab'.
427+
"""
428+
import importlib
429+
import plotly.io._renderers as _renderers_mod
430+
from plotly import optional_imports
431+
432+
fake_ipython = MagicMock()
433+
original_get_module = optional_imports.get_module
434+
435+
def patched_get_module(name, *args, **kwargs):
436+
if name in ("IPython", "IPython.display"):
437+
return fake_ipython
438+
return original_get_module(name, *args, **kwargs)
439+
440+
original_default = pio.renderers.default
441+
try:
442+
with mock.patch.dict(os.environ, {"COLAB_NOTEBOOK_ID": "fake-id"}, clear=True):
443+
with mock.patch.object(
444+
optional_imports, "get_module", side_effect=patched_get_module
445+
):
446+
importlib.reload(_renderers_mod)
447+
assert _renderers_mod.renderers.default == "colab"
448+
finally:
449+
importlib.reload(_renderers_mod)
450+
pio.renderers.default = original_default
451+
452+
453+
def test_colab_renderer_when_env_var_is_absent():
454+
"""
455+
Without COLAB_NOTEBOOK_ID the default renderer must not be 'colab',
456+
even when ``google.colab`` is importable. The old detection used
457+
``import google.colab`` which wrongly activated the colab renderer
458+
in environments that merely had the package installed (e.g. the
459+
Colab VS Code extension).
460+
461+
Regression test for https://github.com/plotly/plotly.py/pull/5473.
462+
"""
463+
import sys
464+
import types
465+
import importlib
466+
import plotly.io._renderers as _renderers_mod
467+
from plotly import optional_imports
468+
469+
fake_ipython = MagicMock()
470+
original_get_module = optional_imports.get_module
471+
472+
def patched_get_module(name, *args, **kwargs):
473+
if name in ("IPython", "IPython.display"):
474+
return fake_ipython
475+
return original_get_module(name, *args, **kwargs)
476+
477+
# Make google.colab importable so the old ``import google.colab``
478+
# approach would have chosen the colab renderer here.
479+
fake_google = types.ModuleType("google")
480+
fake_google_colab = types.ModuleType("google.colab")
481+
482+
original_default = pio.renderers.default
483+
try:
484+
with mock.patch.dict(os.environ, {}, clear=True):
485+
with mock.patch.dict(
486+
sys.modules,
487+
{"google": fake_google, "google.colab": fake_google_colab},
488+
):
489+
with mock.patch.object(
490+
optional_imports, "get_module", side_effect=patched_get_module
491+
):
492+
importlib.reload(_renderers_mod)
493+
assert _renderers_mod.renderers.default != "colab"
494+
finally:
495+
importlib.reload(_renderers_mod)
496+
pio.renderers.default = original_default

0 commit comments

Comments
 (0)