Skip to content

Commit fbaf3df

Browse files
authored
Merge pull request #5473 from kevineger/colab
Set the 'colab' renderer only for Colab web notebooks
2 parents 5b4363e + b0e46d3 commit fbaf3df

3 files changed

Lines changed: 77 additions & 6 deletions

File tree

CHANGELOG.md

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

77
### Fixed
88
- 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!
9+
- Use presence of `COLAB_NOTEBOOK_ID` env var to enable Colab renderer instead of testing import of `google.colab` [[#5473](https://github.com/plotly/plotly.py/pull/5473)], with thanks to @kevineger 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

1112
### Updated

plotly/io/_renderers.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -488,13 +488,10 @@ def show(fig, renderer=None, validate=True, **kwargs):
488488
elif ipython and ipython.get_ipython():
489489
# Try to detect environment so that we can enable a useful
490490
# default renderer
491-
if not default_renderer:
492-
try:
493-
import google.colab # noqa: F401
494491

495-
default_renderer = "colab"
496-
except ImportError:
497-
pass
492+
# Check if we're running in a Colab web notebook
493+
if not default_renderer and "COLAB_NOTEBOOK_ID" in os.environ:
494+
default_renderer = "colab"
498495

499496
# Check if we're running in a Kaggle notebook
500497
if not default_renderer and os.path.exists("/kaggle/input"):

tests/test_io/test_renderers.py

Lines changed: 73 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,75 @@ 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.
457+
458+
Regression test for https://github.com/plotly/plotly.py/pull/5473.
459+
"""
460+
import sys
461+
import types
462+
import importlib
463+
import plotly.io._renderers as _renderers_mod
464+
from plotly import optional_imports
465+
466+
fake_ipython = MagicMock()
467+
original_get_module = optional_imports.get_module
468+
469+
def patched_get_module(name, *args, **kwargs):
470+
if name in ("IPython", "IPython.display"):
471+
return fake_ipython
472+
return original_get_module(name, *args, **kwargs)
473+
474+
# Make google.colab importable so the old ``import google.colab``
475+
# approach would have chosen the colab renderer here.
476+
fake_google = types.ModuleType("google")
477+
fake_google_colab = types.ModuleType("google.colab")
478+
479+
original_default = pio.renderers.default
480+
try:
481+
with mock.patch.dict(os.environ, {}, clear=True):
482+
with mock.patch.dict(
483+
sys.modules,
484+
{"google": fake_google, "google.colab": fake_google_colab},
485+
):
486+
with mock.patch.object(
487+
optional_imports, "get_module", side_effect=patched_get_module
488+
):
489+
importlib.reload(_renderers_mod)
490+
assert _renderers_mod.renderers.default != "colab"
491+
finally:
492+
importlib.reload(_renderers_mod)
493+
pio.renderers.default = original_default

0 commit comments

Comments
 (0)