Skip to content

Commit 94feeb2

Browse files
author
OutlyingWest
committed
perfdata_to_variable magic moved to the extension
1 parent d248e33 commit 94feeb2

2 files changed

Lines changed: 35 additions & 46 deletions

File tree

src/jumper/kernel.py

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -955,52 +955,6 @@ async def do_execute(
955955
cell_id=cell_id,
956956
)
957957

958-
elif code.startswith("%%perfdata_to_variable"):
959-
if len(code.split(" ")) == 1:
960-
self.cell_output(
961-
"No variable for export specified. Use: "
962-
"%%perfdata_to_variable myvar",
963-
"stdout",
964-
)
965-
else:
966-
varname = code.split(" ")[1]
967-
# for multi cell mode, we might have time indices which we want
968-
# to communicate to the user, each time_index has the index of
969-
# the overall mult cell and the sub index within this cell.
970-
# In addition, it has a counter for the number of measurements
971-
# each sub cell corresponds to in the list of performance data
972-
# measurements, e.g. (2_0, 5), (2_1, 3), (2_2, 7)
973-
mcm_time_indices = kernel_context.perfdata_handler.get_time_indices()
974-
mcm_time_indices = list(
975-
filter(lambda item: item is not None, mcm_time_indices)
976-
)
977-
978-
code = (
979-
f"{varname}="
980-
f"{kernel_context.perfdata_handler.get_perfdata_history()}"
981-
)
982-
983-
if mcm_time_indices:
984-
code += f"\n{varname}.append({mcm_time_indices})"
985-
986-
await super().do_execute(code, silent=True)
987-
self.cell_output(
988-
"Exported performance data to "
989-
+ str(varname)
990-
+ " variable. ",
991-
"stdout",
992-
)
993-
if mcm_time_indices:
994-
self.cell_output(
995-
"Detected that cells were executed in multi cell mode."
996-
+ f"Last entry in {varname} is a list that contains "
997-
f"the sub indices per cell that were executed in "
998-
f"in multi cell mode and a counter for the number of"
999-
f" performance measurements within this sub cell, "
1000-
f"e.g. f{mcm_time_indices[-1]}",
1001-
"stdout",
1002-
)
1003-
return self.standard_reply()
1004958
elif code.startswith("%%perfdata_to_json"):
1005959
if len(code.split(" ")) == 1:
1006960
self.cell_output(

src/magic_extension/magic.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,41 @@ def display_code_history(self, line):
141141
columnDefs=[{"className": "dt-left", "targets": 2}],
142142
)
143143

144+
@line_magic
145+
def perfdata_to_variable(self, line):
146+
"""
147+
Export collected performance data into a notebook variable.
148+
Usage:
149+
%%perfdata_to_variable myvar
150+
"""
151+
if not line.strip():
152+
self.cell_output("No variable for export specified. Use: %%perfdata_to_variable myvar", stream="stdout")
153+
return
154+
155+
varname = line.strip()
156+
mcm_time_indices = kernel_context.perfdata_handler.get_time_indices()
157+
mcm_time_indices = list(filter(None, mcm_time_indices))
158+
159+
code = (
160+
f"{varname} = "
161+
f"{kernel_context.perfdata_handler.get_perfdata_history()}"
162+
)
163+
164+
if mcm_time_indices:
165+
code += f"\n{varname}.append({mcm_time_indices})"
166+
167+
self.shell.run_cell(code, store_history=False)
168+
169+
self.cell_output(f"Exported performance data to variable `{varname}`.", stream="stdout")
170+
171+
if mcm_time_indices:
172+
self.cell_output(
173+
f"Detected that cells were executed in multi-cell mode.\n"
174+
f"The last entry in `{varname}` contains sub-cell index info, "
175+
f"e.g. {mcm_time_indices[-1]}.",
176+
stream="stdout"
177+
)
178+
144179
@cell_magic
145180
def set_perfmonitor(self, line, code):
146181
"""

0 commit comments

Comments
 (0)