Skip to content

Commit 5491c05

Browse files
authored
Merge pull request #1 from Axiomatic-AI/add-magic-for-ipython
add magic for ipython
2 parents 04370fc + 5a4ff28 commit 5491c05

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

.fernignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
# Specify files that shouldn't be modified by Fern
2+
src/axiomatic/magic.py

src/axiomatic/magic.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import platformdirs
2+
import os
3+
4+
5+
from . import Axiomatic
6+
7+
8+
class AXMagic:
9+
"""Class implementing magic functions for IPython.
10+
Import with `%load_ext axiomatic.magic`."""
11+
12+
def __init__(self):
13+
self.folder = platformdirs.user_config_dir("axiomatic")
14+
if not os.path.exists(f"{self.folder}/api_key"):
15+
os.makedirs(self.folder, exist_ok=True)
16+
self.api_key = None
17+
else:
18+
with open(f"{self.folder}/api_key", "r") as f:
19+
self.api_key = f.read()
20+
self.ax = Axiomatic(api_key=self.api_key)
21+
22+
def ax_api(self, query):
23+
folder = platformdirs.user_config_dir("axiomatic")
24+
with open(f"{folder}/api_key", "w") as f:
25+
f.write(query)
26+
self.api = query
27+
self.ax = Axiomatic(api_key=self.api)
28+
print("API key set.")
29+
30+
def ax_query(self, query, cell=None):
31+
# NOTE: we need to add additional dependencies to the requirements!
32+
from IPython import get_ipython
33+
from IPython.core.magic import register_line_cell_magic, register_line_magic
34+
from IPython.display import HTML, display
35+
36+
if self.api_key:
37+
result = self.ax.experimental.magic_request(query=query, cell=cell)
38+
display(HTML(result.response))
39+
40+
try:
41+
# When running in colab
42+
from google.colab import _frontend
43+
44+
_frontend.create_scratch_cell(
45+
f"""# {query}\n{result.code}""", bottom_pane=True
46+
)
47+
except Exception as e:
48+
# When running in jupyter
49+
get_ipython().set_next_input(f"{result.code}", replace=False)
50+
51+
else:
52+
print(
53+
"Please set your Axiomatic API key first with the command %ax_api API_KEY. Request the api key at our Customer Service."
54+
)
55+
56+
def ax_fix(self, query, cell=None):
57+
# Just dummy at the moment
58+
return self.ax_query(query, cell)
59+
60+
61+
def ax_help(value: str):
62+
print(
63+
"""
64+
Available commands:
65+
66+
- `%load_ext axiomatic_pic` loads the ipython extension.
67+
- `%ax_api` sets up the API key
68+
- `%ax_query` returns the requested circuit using our experimental API
69+
- `%%ax_fix` edit the given code
70+
"""
71+
)
72+
73+
74+
def load_ipython_extension(ipython):
75+
ax_magic = AXMagic()
76+
ipython.register_magic_function(ax_magic.ax_query, "line_cell")
77+
ipython.register_magic_function(ax_magic.ax_fix, "line_cell")
78+
ipython.register_magic_function(ax_magic.ax_api, "line")
79+
ipython.register_magic_function(ax_help, "line")

0 commit comments

Comments
 (0)