Skip to content

Commit cb8f9cc

Browse files
Merge pull request #16 from Axiomatic-AI/tools-magic
Add ax_tool magic for tools hosting code execution from cell
2 parents 04fe6f3 + e9a8b24 commit cb8f9cc

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

src/axiomatic/magic.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import platformdirs # type: ignore
66
import os
77
import sys
8+
import time
89
from . import Axiomatic
910

1011

@@ -68,7 +69,6 @@ def axquery(self, query, cell=None):
6869
# When running in jupyter
6970
get_ipython().set_next_input(f"# %%ax_fix\n{code}", replace=False)
7071

71-
7272
def ax_query(self, query, cell=None):
7373
# Updates the target circuit query
7474
self.query = query
@@ -78,6 +78,61 @@ def ax_fix(self, query, cell=None):
7878
# Runs again without updating the query
7979
return self.axquery(query, cell)
8080

81+
def ax_tool(self, tool, cell):
82+
"""
83+
A custom IPython cell magic that sends python code in a cell to the tools scheduling API
84+
using Axiomatic's `client.tools.schedule` and wait for the tool execution to finish and
85+
show the 'stdout' of the script
86+
87+
Usage:
88+
89+
%%tool_schedule [optional_tool_name]
90+
<some code here>
91+
92+
Example:
93+
94+
%%tool_schedule fdtd
95+
print("Hello from this cell!")
96+
x = 1 + 2
97+
98+
This cell won't run as Python code; instead, the text will be sent to the tool_schedule method
99+
of the Axiomatic client.
100+
101+
Available Tools:
102+
- fdtd
103+
- femwell
104+
- jaxfem
105+
- optiland
106+
- pyspice
107+
- sax-gdsfactory
108+
"""
109+
if not tool.strip():
110+
print("Please provider a tool name when calling this magic like: %%tool_schedule [optional_tool_name]")
111+
else:
112+
tool_name = tool.strip()
113+
code_string = cell
114+
115+
output = self.client.tools.schedule(
116+
tool_name=tool_name,
117+
code=code_string,
118+
)
119+
if output.is_success is True:
120+
job_id = output.job_id
121+
result = self.client.tools.status(job_id=output.job_id)
122+
print(f"job_id: {job_id}")
123+
while True:
124+
result = self.client.tools.status(job_id=output.job_id)
125+
if result.status == "PENDING" or result.status == "RUNNING":
126+
time.sleep(3)
127+
else:
128+
if result.status == "SUCCEEDED":
129+
print(result.output)
130+
else:
131+
print(result.error_trace)
132+
break
133+
else:
134+
print(output.error_trace)
135+
81136

82137
def ax_help(value: str):
83138
print(
@@ -87,6 +142,8 @@ def ax_help(value: str):
87142
- `%load_ext axiomatic_pic` loads the ipython extension.
88143
- `%ax_query` returns the requested circuit using our experimental API
89144
- `%%ax_fix` edit the given code
145+
- `%%ax_tool tool_name` executes python code for a given tool. Available tools are:
146+
`fdtd, femwell, jaxfem, optiland, pyspice and sax-gdsfactory`
90147
"""
91148
)
92149

@@ -96,4 +153,5 @@ def load_ipython_extension(ipython):
96153
ipython.register_magic_function(ax_magic.ax_query, "line_cell")
97154
ipython.register_magic_function(ax_magic.ax_fix, "line_cell")
98155
ipython.register_magic_function(ax_magic.ax_api, "line")
156+
ipython.register_magic_function(ax_magic.ax_tool, "cell")
99157
ipython.register_magic_function(ax_help, "line")

0 commit comments

Comments
 (0)