Skip to content

Commit dfbe613

Browse files
feat(magic): add run_personal and run_shared magic commands
1 parent 1b6131a commit dfbe613

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

run_personal/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from run_personal.magic import load_ipython_extension
2+
3+
4+
__all__ = ['load_ipython_extension']

run_personal/magic.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import os
2+
import time
3+
from typing import Any
4+
5+
from IPython.core.interactiveshell import InteractiveShell
6+
from IPython.core.magic import line_magic
7+
from IPython.core.magic import Magics
8+
from IPython.core.magic import magics_class
9+
from IPython.core.magic import needs_local_scope
10+
from IPython.core.magic import no_var_expand
11+
12+
13+
@magics_class
14+
class RunPersonalMagic(Magics):
15+
def __init__(self, shell: InteractiveShell):
16+
Magics.__init__(self, shell=shell)
17+
18+
@no_var_expand
19+
@needs_local_scope
20+
@line_magic('run_personal')
21+
def run_personal(self, line: str, local_ns: Any = None) -> Any:
22+
"""
23+
Downloads a personal file using the %sql magic and then runs it using %run.
24+
25+
Examples::
26+
27+
# Line usage
28+
29+
%run_personal personal_file.ipynb
30+
31+
"""
32+
personal_file = line.strip()
33+
if not personal_file:
34+
raise ValueError('No personal file specified.')
35+
36+
local_filename = f'{int(time.time() * 1_000_000)}_{personal_file}'
37+
sql_command = f"DOWNLOAD PERSONAL FILE '{personal_file}' TO '{local_filename}'"
38+
39+
# Execute the SQL command
40+
self.shell.run_line_magic('sql', sql_command)
41+
# Run the downloaded file
42+
self.shell.run_line_magic('run', local_filename)
43+
44+
# Delete the local file after running it
45+
if os.path.exists(local_filename):
46+
os.remove(local_filename)
47+
48+
49+
# In order to actually use these magics, you must register them with a
50+
# running IPython.
51+
52+
53+
def load_ipython_extension(ip: InteractiveShell) -> None:
54+
"""
55+
Any module file that define a function named `load_ipython_extension`
56+
can be loaded via `%load_ext module.path` or be configured to be
57+
autoloaded by IPython at startup time.
58+
"""
59+
60+
# Load jupysql extension
61+
# This is necessary for jupysql to initialize internal state
62+
# required to render messages
63+
assert ip.extension_manager is not None
64+
result = ip.extension_manager.load_extension('sql')
65+
if result == 'no load function':
66+
raise RuntimeError('Could not load sql extension. Is jupysql installed?')
67+
68+
# Check if %run magic command is defined
69+
if ip.find_line_magic('run') is None:
70+
raise RuntimeError(
71+
'%run magic command is not defined. '
72+
'Is it available in your IPython environment?',
73+
)
74+
75+
# Register run_personal and run_shared
76+
ip.register_magics(RunPersonalMagic(ip))

run_shared/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from run_shared.magic import load_ipython_extension
2+
3+
4+
__all__ = ['load_ipython_extension']

run_shared/magic.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import os
2+
import time
3+
from typing import Any
4+
5+
from IPython.core.interactiveshell import InteractiveShell
6+
from IPython.core.magic import line_magic
7+
from IPython.core.magic import Magics
8+
from IPython.core.magic import magics_class
9+
from IPython.core.magic import needs_local_scope
10+
from IPython.core.magic import no_var_expand
11+
12+
13+
@magics_class
14+
class RunSharedMagic(Magics):
15+
def __init__(self, shell: InteractiveShell):
16+
Magics.__init__(self, shell=shell)
17+
18+
@no_var_expand
19+
@needs_local_scope
20+
@line_magic('run_shared')
21+
def run_shared(self, line: str, local_ns: Any = None) -> Any:
22+
"""
23+
Downloads a shared file using the %sql magic and then runs it using %run.
24+
25+
Examples::
26+
27+
# Line usage
28+
29+
%run_shared shared_file.ipynb
30+
31+
"""
32+
shared_file = line.strip()
33+
if not shared_file:
34+
raise ValueError('No shared file specified.')
35+
36+
local_filename = f'{int(time.time() * 1_000_000)}_{shared_file}'
37+
sql_command = f"DOWNLOAD SHARED FILE '{shared_file}' TO '{local_filename}'"
38+
39+
# Execute the SQL command
40+
self.shell.run_line_magic('sql', sql_command)
41+
# Run the downloaded file
42+
self.shell.run_line_magic('run', local_filename)
43+
44+
# Delete the local file after running it
45+
if os.path.exists(local_filename):
46+
os.remove(local_filename)
47+
48+
# In order to actually use these magics, you must register them with a
49+
# running IPython.
50+
51+
52+
def load_ipython_extension(ip: InteractiveShell) -> None:
53+
"""
54+
Any module file that define a function named `load_ipython_extension`
55+
can be loaded via `%load_ext module.path` or be configured to be
56+
autoloaded by IPython at startup time.
57+
"""
58+
59+
# Load jupysql extension
60+
# This is necessary for jupysql to initialize internal state
61+
# required to render messages
62+
assert ip.extension_manager is not None
63+
result = ip.extension_manager.load_extension('sql')
64+
if result == 'no load function':
65+
raise RuntimeError('Could not load sql extension. Is jupysql installed?')
66+
67+
# Check if %run magic command is defined
68+
if ip.find_line_magic('run') is None:
69+
raise RuntimeError(
70+
'%run magic command is not defined. '
71+
'Is it available in your IPython environment?',
72+
)
73+
74+
# Register run_personal and run_shared
75+
ip.register_magics(RunSharedMagic(ip))

0 commit comments

Comments
 (0)