Skip to content

Commit 9cbf5f2

Browse files
authored
Respect DisplayFormatter.active_types trait configuration (ipython#14996)
## Fixes ipython#14991 ### Description If `DisplayFormatter.active_types` is set via config, avoid overriding it with `["text/plain"]` in `TerminalInteractiveShell`. This allows users to include `"image/png"` in `active_types`, ensuring that plot images are properly published for later export with the `%notebook` magic.
2 parents 70dc8cc + f53ff48 commit 9cbf5f2

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

IPython/terminal/interactiveshell.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -765,8 +765,13 @@ def restore_term_title(self):
765765

766766
def init_display_formatter(self):
767767
super(TerminalInteractiveShell, self).init_display_formatter()
768-
# terminal only supports plain text
769-
self.display_formatter.active_types = ["text/plain"]
768+
# terminal only supports plain text if not explicitly configured
769+
config = self.display_formatter._trait_values["config"]
770+
if not (
771+
"DisplayFormatter" in config
772+
and "active_types" in config["DisplayFormatter"]
773+
):
774+
self.display_formatter.active_types = ["text/plain"]
770775

771776
def init_prompt_toolkit_cli(self):
772777
if self.simple_prompt:

tests/test_formatters.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,36 @@ def foo_printer(obj, pp, cycle):
5656
pp.text("foo")
5757

5858

59+
def test_display_formatter_active_types_config():
60+
from IPython.terminal.interactiveshell import TerminalInteractiveShell
61+
from IPython.core.history import HistoryManager
62+
from traitlets.config import Config
63+
64+
# Clear HistoryManager instances to bypass singleton limit before creating new shell
65+
prev_instances = HistoryManager._instances.copy()
66+
HistoryManager._instances.clear()
67+
68+
try:
69+
c = Config()
70+
c.DisplayFormatter.active_types = ["text/plain", "image/png"]
71+
# Disable history
72+
c.HistoryManager.enabled = False
73+
74+
ip = TerminalInteractiveShell(config=c)
75+
76+
active_types = ip.display_formatter.active_types
77+
assert "text/plain" in active_types
78+
assert "image/png" in active_types
79+
80+
# Ensure only the expected types are active
81+
assert set(active_types) == {"text/plain", "image/png"}
82+
83+
finally:
84+
# Clean up the new instance and restore previous state
85+
HistoryManager._instances.clear()
86+
HistoryManager._instances.update(prev_instances)
87+
88+
5989
def test_pretty():
6090
f = PlainTextFormatter()
6191
f.for_type(A, foo_printer)

0 commit comments

Comments
 (0)