Skip to content

Commit b1fbb1a

Browse files
feat(magic): add support for jinja2 templates
1 parent e12d028 commit b1fbb1a

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

run_personal/magic.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from IPython.core.magic import magics_class
99
from IPython.core.magic import needs_local_scope
1010
from IPython.core.magic import no_var_expand
11+
from jinja2 import Template
1112

1213

1314
@magics_class
@@ -28,11 +29,15 @@ def run_personal(self, line: str, local_ns: Any = None) -> Any:
2829
2930
%run_personal personal_file.ipynb
3031
32+
%run_personal {{ sample_notebook_name }}
33+
3134
"""
32-
personal_file = line.strip()
35+
template = Template(line.strip())
36+
personal_file = template.render(local_ns)
3337
if not personal_file:
3438
raise ValueError('No personal file specified.')
35-
if personal_file.startswith("'") and personal_file.endswith("'"):
39+
if (personal_file.startswith("'") and personal_file.endswith("'")) or \
40+
(personal_file.startswith('"') and personal_file.endswith('"')):
3641
personal_file = personal_file[1:-1]
3742
if not personal_file:
3843
raise ValueError('No personal file specified.')

run_shared/magic.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from IPython.core.magic import magics_class
99
from IPython.core.magic import needs_local_scope
1010
from IPython.core.magic import no_var_expand
11+
from jinja2 import Template
1112

1213

1314
@magics_class
@@ -28,11 +29,15 @@ def run_shared(self, line: str, local_ns: Any = None) -> Any:
2829
2930
%run_shared shared_file.ipynb
3031
32+
%run_shared '{{ sample_notebook_name }}'
33+
3134
"""
32-
shared_file = line.strip()
35+
template = Template(line.strip())
36+
shared_file = template.render(local_ns)
3337
if not shared_file:
3438
raise ValueError('No shared file specified.')
35-
if shared_file.startswith("'") and shared_file.endswith("'"):
39+
if (shared_file.startswith("'") and shared_file.endswith("'")) or \
40+
(shared_file.startswith('"') and shared_file.endswith('"')):
3641
shared_file = shared_file[1:-1]
3742
if not shared_file:
3843
raise ValueError('No personal file specified.')

0 commit comments

Comments
 (0)