Skip to content

Commit 6a471d8

Browse files
author
OutlyingWest
committed
perfdata_to_json magic moved to the extension
1 parent 94feeb2 commit 6a471d8

2 files changed

Lines changed: 45 additions & 34 deletions

File tree

src/jumper/kernel.py

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

958-
elif code.startswith("%%perfdata_to_json"):
959-
if len(code.split(" ")) == 1:
960-
self.cell_output(
961-
"No filename to export specified. Use: "
962-
"%%perfdata_to_variable myfile",
963-
"stdout",
964-
)
965-
else:
966-
filename = code.split(" ")[1]
967-
with open(f"{filename}_perfdata.json", "w") as f:
968-
json.dump(
969-
kernel_context.perfdata_handler.get_perfdata_history(),
970-
default=str,
971-
fp=f,
972-
)
973-
with open(f"{filename}_code.json", "w") as f:
974-
json.dump(
975-
kernel_context.perfdata_handler.get_code_history(),
976-
default=str,
977-
fp=f,
978-
)
979-
self.cell_output(
980-
"Exported performance data to "
981-
+ str(filename)
982-
+ "_perfdata.json and "
983-
+ str(filename)
984-
+ "_code.json",
985-
"stdout",
986-
)
987-
return self.standard_reply()
988958
elif code.startswith("%%scorep_python_binding_arguments"):
989959
return self.scorep_not_available() or self.set_scorep_pythonargs(
990960
code

src/magic_extension/magic.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import sys
23

34
import pandas as pd
@@ -106,10 +107,10 @@ def display_code_for_index(self, line):
106107
"""
107108
Display stored source code of a previously executed cell by index.
108109
Usage:
109-
%%display_code_for_index 2
110+
%display_code_for_index 2
110111
"""
111112
if not line.strip():
112-
self.cell_output("No index specified. Use: %%display_code_for_index <index>", stream="stdout")
113+
self.cell_output("No index specified. Use: %display_code_for_index <index>", stream="stdout")
113114
return
114115

115116
try:
@@ -146,10 +147,10 @@ def perfdata_to_variable(self, line):
146147
"""
147148
Export collected performance data into a notebook variable.
148149
Usage:
149-
%%perfdata_to_variable myvar
150+
%perfdata_to_variable myvar
150151
"""
151152
if not line.strip():
152-
self.cell_output("No variable for export specified. Use: %%perfdata_to_variable myvar", stream="stdout")
153+
self.cell_output("No variable for export specified. Use: %perfdata_to_variable myvar", stream="stdout")
153154
return
154155

155156
varname = line.strip()
@@ -176,11 +177,51 @@ def perfdata_to_variable(self, line):
176177
stream="stdout"
177178
)
178179

180+
@line_magic
181+
def perfdata_to_json(self, line):
182+
"""
183+
Export performance data and code history to JSON files.
184+
Usage:
185+
%perfdata_to_json myfilename
186+
Creates:
187+
myfilename_perfdata.json
188+
myfilename_code.json
189+
"""
190+
if not line.strip():
191+
self.cell_output("No filename specified. Use: %perfdata_to_json <myfile>", stream="stdout")
192+
return
193+
194+
filename = line.strip()
195+
196+
try:
197+
with open(f"{filename}_perfdata.json", "w") as f:
198+
json.dump(
199+
kernel_context.perfdata_handler.get_perfdata_history(),
200+
fp=f,
201+
default=str
202+
)
203+
204+
with open(f"{filename}_code.json", "w") as f:
205+
json.dump(
206+
kernel_context.perfdata_handler.get_code_history(),
207+
fp=f,
208+
default=str
209+
)
210+
211+
self.cell_output(
212+
f"Exported performance data to `{filename}_perfdata.json` "
213+
f"and `{filename}_code.json`",
214+
stream="stdout"
215+
)
216+
except Exception as e:
217+
self.cell_output(f"Failed to export data: {e}", stream="stderr")
218+
179219
@cell_magic
180220
def set_perfmonitor(self, line, code):
181221
"""
182222
Read the perfmonitor and try to select it.
183223
"""
224+
# TODO fix modes (yet exist in kernel and extension)
184225
if self.mode == KernelMode.DEFAULT:
185226
monitor = code.split("\n")[1]
186227
if monitor in {"local", "localhost", "LOCAL", "LOCALHOST"}:

0 commit comments

Comments
 (0)