Skip to content

Commit e17c267

Browse files
author
OutlyingWest
committed
display code functionality is moved to the extension
1 parent 3a75691 commit e17c267

2 files changed

Lines changed: 45 additions & 37 deletions

File tree

src/jumper/kernel.py

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -954,41 +954,7 @@ async def do_execute(
954954
allow_stdin,
955955
cell_id=cell_id,
956956
)
957-
elif code.startswith("%%display_code_for_index"):
958-
if len(code.split(" ")) == 1:
959-
self.cell_output(
960-
"No index specified. Use: %%display_code_for_index index",
961-
"stdout",
962-
)
963-
index = int(code.split(" ")[1])
964-
if index >= len(kernel_context.perfdata_handler.get_perfdata_history()):
965-
self.cell_output(
966-
"Tracked only "
967-
+ str(len(kernel_context.perfdata_handler.get_perfdata_history()))
968-
+ " cells. This index is not available."
969-
)
970-
else:
971-
self.cell_output(
972-
"Cell timestamp: "
973-
+ str(kernel_context.perfdata_handler.get_code_history()[index][0])
974-
+ "\n--\n",
975-
"stdout",
976-
)
977-
self.cell_output(
978-
kernel_context.perfdata_handler.get_code_history()[index][1],
979-
"stdout",
980-
)
981-
return self.standard_reply()
982-
elif code.startswith("%%display_code_history"):
983-
show(
984-
pd.DataFrame(
985-
kernel_context.perfdata_handler.get_code_history(),
986-
columns=["timestamp", "code"],
987-
).reset_index(),
988-
layout={"topStart": "search", "topEnd": None},
989-
columnDefs=[{"className": "dt-left", "targets": 2}],
990-
)
991-
return self.standard_reply()
957+
992958
elif code.startswith("%%perfdata_to_variable"):
993959
if len(code.split(" ")) == 1:
994960
self.cell_output(

src/magic_extension/magic.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import sys
22

3+
import pandas as pd
4+
from itables import show
35
from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic)
46

5-
from jumper.context import kernel_context, KernelMode
67

8+
from jumper.context import kernel_context, KernelMode
79
import jumper.visualization as perfvis
810

911

@@ -68,7 +70,7 @@ def display_graph_for_index(self, line):
6870
if time_indices:
6971
sub_idxs = [x[0] for x in time_indices[0]]
7072
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. "
7274
f"Got performance data for the following sub-cells: {sub_idxs}"
7375
)
7476

@@ -99,6 +101,46 @@ def display_graph_for_last(self, line):
99101
)
100102

101103

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+
102144
@cell_magic
103145
def set_perfmonitor(self, line, code):
104146
"""

0 commit comments

Comments
 (0)