From 49672f3ac762f5a4a4db7f8f858a80cce947babf Mon Sep 17 00:00:00 2001
From: fff122 <1.59777134e+08+fff122@users.noreply.github.com>
Date: Thu, 13 Aug 2026 17:20:52 +0000
Subject: [PATCH] Fix local image URL when not embedding
---
IPython/core/display.py | 3 ++-
tests/test_display_2.py | 7 +++++++
2 files changed, 9 insertions(+), 1 deletion(-)
diff --git a/IPython/core/display.py b/IPython/core/display.py
index 263850ce239..78d552cdee9 100644
--- a/IPython/core/display.py
+++ b/IPython/core/display.py
@@ -1067,8 +1067,9 @@ def _repr_html_(self):
klass = ' class="unconfined"'
if self.alt:
alt = ' alt="%s"' % html.escape(self.alt)
+ url = self.url if self.url is not None else self.filename
return '
'.format(
- url=html.escape(self.url or ""),
+ url=html.escape(url or ""),
width=width,
height=height,
klass=klass,
diff --git a/tests/test_display_2.py b/tests/test_display_2.py
index 8e6fc646132..8f4392e0bc2 100644
--- a/tests/test_display_2.py
+++ b/tests/test_display_2.py
@@ -37,6 +37,13 @@ def test_image_size():
assert '
' % (thisurl) == img._repr_html_()
+def test_image_local_file_url_without_embedding():
+ """A local image requested without embedding keeps its file URL."""
+ filename = os.path.join(os.path.dirname(__file__), "2x2.png")
+ img = display.Image(filename=filename, embed=False)
+ assert f'
' == img._repr_html_()
+
+
def test_image_mimes():
fmt = get_ipython().display_formatter.format
for name, format in ImageFormat.__members__.items():