Highlight IPython cell magic. Captures the output of a cell and stores it in the cassini.environment.env.o highlights file.
If the cell returns a string, this is used as a caption for the output.
A title argument must be provided.
            
              Source code in cassini/magics.py
              | 11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 | def hlt(line: str, cell: str):
    """
    Highlight IPython cell magic. Captures the output of a cell and stores it in the [cassini.environment.env.o][cassini.environment._Env.o] highlights file.
    If the cell returns a string, this is used as a caption for the output.
    A title argument must be provided.
    """
    if env.is_shared(env):
        warn(
            "This notebook is in a shared context and therefore highlights magics won't work"
        )
        return cell
    if not isinstance(env.o, NotebookTierBase):
        raise ValueError(
            "Highlights can only be added to tiers that subclass NotebookTierBase"
        )
    if not line:
        raise ValueError("Please provide a title for the highlight")
    assert env.o
    outputs = []
    def capture_display(msg):
        if msg["content"]["data"] in outputs:  # display only once
            return None
        outputs.append(msg["content"])
        return msg
    cell = cell.strip()
    annotation = None
    if cell.endswith('"""'):
        *rest, annotation = cell.split('"""')[:-1]
        cell = '"""'.join(rest)
    shell = InteractiveShell.instance()
    # Capture any output that is displayed before output (like matplotlib plots)
    shell.display_pub.register_hook(capture_display)
    header = f"## {line}"
    try:
        publish_display_data({"text/markdown": header})
        result = shell.run_cell(cell).result
        if shell.display_formatter:
            outputs.append(
                dict(zip(("data", "metadata"), shell.display_formatter.format(result)))
            )
        if annotation:
            publish_display_data({"text/markdown": annotation})
    finally:
        shell.display_pub.unregister_hook(capture_display)
    all_out = outputs
    env.o.add_highlight(line, all_out)
    return None
 |