From b4c6c684b929ceca5aa43517eb86c16b86669ca3 Mon Sep 17 00:00:00 2001 From: Hassan Kibirige Date: Mon, 19 Jan 2026 18:37:46 +0300 Subject: [PATCH] fix: rendering of SVGs in jupyter notebooks --- doc/changelog.qmd | 2 ++ plotnine/_utils/ipython.py | 5 ++++- plotnine/typing.py | 4 +++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/doc/changelog.qmd b/doc/changelog.qmd index aebac1d527..3af3941e69 100644 --- a/doc/changelog.qmd +++ b/doc/changelog.qmd @@ -23,6 +23,8 @@ title: Changelog - Fixed [](:class:`~plotnine.geom_smooth`) / [](:class:`~plotnine.stat_smooth`) when using a linear model via "lm" with weights for the model to do a weighted regression. This bug did not affect the formula API of the linear model. ({{< issue 1005 >}}) +- Fixed rendering of SVGs in jupyter notebooks. + ## v0.15.2 (2025-12-12) diff --git a/plotnine/_utils/ipython.py b/plotnine/_utils/ipython.py index f2820822b0..794b8a547f 100644 --- a/plotnine/_utils/ipython.py +++ b/plotnine/_utils/ipython.py @@ -55,6 +55,7 @@ def get_mimebundle( } mimetype = lookup[format] + image: bytes | str = b metadata: dict[str, DisplayMetadata] = {} w, h = figure_size_px if format in ("png", "jpeg"): @@ -62,5 +63,7 @@ def get_mimebundle( elif format == "retina": # `retina=True` in IPython.display.Image just halves width/height metadata = {mimetype: {"width": w // 2, "height": h // 2}} + elif format == "svg": + image = b.decode() - return {mimetype: b}, metadata + return {mimetype: image}, metadata diff --git a/plotnine/typing.py b/plotnine/typing.py index 2fd64b2f5b..a71aa9f26a 100644 --- a/plotnine/typing.py +++ b/plotnine/typing.py @@ -157,4 +157,6 @@ class DisplayMetadata(TypedDict): height: NotRequired[int] -MimeBundle: TypeAlias = tuple[dict[str, bytes], dict[str, DisplayMetadata]] +MimeBundle: TypeAlias = tuple[ + dict[str, bytes | str], dict[str, DisplayMetadata] +]