|
1 | 1 | import sys |
2 | 2 |
|
| 3 | +import pandas as pd |
| 4 | +from itables import show |
3 | 5 | from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic) |
4 | 6 |
|
5 | | -from jumper.context import kernel_context, KernelMode |
6 | 7 |
|
| 8 | +from jumper.context import kernel_context, KernelMode |
7 | 9 | import jumper.visualization as perfvis |
8 | 10 |
|
9 | 11 |
|
@@ -68,7 +70,7 @@ def display_graph_for_index(self, line): |
68 | 70 | if time_indices: |
69 | 71 | sub_idxs = [x[0] for x in time_indices[0]] |
70 | 72 | self.cell_output( |
71 | | - f"📈 Cell seemed to be tracked in multi-cell mode. " |
| 73 | + f"Cell seemed to be tracked in multi-cell mode. " |
72 | 74 | f"Got performance data for the following sub-cells: {sub_idxs}" |
73 | 75 | ) |
74 | 76 |
|
@@ -99,6 +101,46 @@ def display_graph_for_last(self, line): |
99 | 101 | ) |
100 | 102 |
|
101 | 103 |
|
| 104 | + @line_magic |
| 105 | + def display_code_for_index(self, line): |
| 106 | + """ |
| 107 | + Display stored source code of a previously executed cell by index. |
| 108 | + Usage: |
| 109 | + %%display_code_for_index 2 |
| 110 | + """ |
| 111 | + if not line.strip(): |
| 112 | + self.cell_output("No index specified. Use: %%display_code_for_index <index>", stream="stdout") |
| 113 | + return |
| 114 | + |
| 115 | + try: |
| 116 | + index = int(line.strip()) |
| 117 | + except ValueError: |
| 118 | + self.cell_output("Invalid index format. Provide an integer.", stream="stderr") |
| 119 | + return |
| 120 | + |
| 121 | + history = kernel_context.perfdata_handler.get_perfdata_history() |
| 122 | + if index >= len(history): |
| 123 | + self.cell_output( |
| 124 | + f"Tracked only {len(history)} cells. This index is not available.", |
| 125 | + stream="stdout" |
| 126 | + ) |
| 127 | + return |
| 128 | + |
| 129 | + timestamp, code = kernel_context.perfdata_handler.get_code_history()[index] |
| 130 | + self.cell_output(f"Cell timestamp: {timestamp}\n--", stream="stdout") |
| 131 | + self.cell_output(code, stream="stdout") |
| 132 | + |
| 133 | + @line_magic |
| 134 | + def display_code_history(self, line): |
| 135 | + show( |
| 136 | + pd.DataFrame( |
| 137 | + kernel_context.perfdata_handler.get_code_history(), |
| 138 | + columns=["timestamp", "code"], |
| 139 | + ).reset_index(), |
| 140 | + layout={"topStart": "search", "topEnd": None}, |
| 141 | + columnDefs=[{"className": "dt-left", "targets": 2}], |
| 142 | + ) |
| 143 | + |
102 | 144 | @cell_magic |
103 | 145 | def set_perfmonitor(self, line, code): |
104 | 146 | """ |
|
0 commit comments