File: _ipython_ext.py

package info (click to toggle)
python-plumbum 1.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,300 kB
  • sloc: python: 10,016; makefile: 130; sh: 8
file content (32 lines) | stat: -rw-r--r-- 1,015 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from __future__ import annotations

import sys
from io import StringIO

import IPython.display
from IPython.core.magic import Magics, cell_magic, magics_class, needs_local_scope

valid_choices = [x[8:] for x in dir(IPython.display) if x[:8] == "display_"]


@magics_class
class OutputMagics(Magics):  # pragma: no cover
    @needs_local_scope
    @cell_magic
    def to(self, line, cell, local_ns=None):
        choice = line.strip()
        assert choice in valid_choices, "Valid choices for '%%to' are: " + str(
            valid_choices
        )
        display_fn = getattr(IPython.display, "display_" + choice)

        # Captures stdout and renders it in the notebook
        with StringIO() as out:
            old_out = sys.stdout
            try:
                sys.stdout = out
                exec(cell, self.shell.user_ns, local_ns)  # pylint: disable=exec-used
                out.seek(0)
                display_fn(out.getvalue(), raw=True)
            finally:
                sys.stdout = old_out