Skip to content

Commit 5ec16af

Browse files
author
Christopher Doris
committed
magic can synchronise variables
1 parent 5bb084d commit 5ec16af

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

pysrc/juliacall/ipython.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,37 @@
1414

1515
from IPython.core.magic import Magics, magics_class, line_cell_magic
1616
from . import Main, Base, PythonCall
17+
import __main__
18+
19+
_set_var = Main.seval("(k, v) -> @eval $(Symbol(k)) = $v")
20+
_get_var = Main.seval("k -> @eval $(Symbol(k))")
1721

1822
@magics_class
1923
class JuliaMagics(Magics):
2024

2125
@line_cell_magic
2226
def julia(self, line, cell=None):
23-
code = line if cell is None else cell
27+
invars = []
28+
outvars = []
29+
if cell is None:
30+
code = line
31+
else:
32+
code = cell
33+
for k in line.split():
34+
if k.startswith('<'):
35+
invars.append(k[1:])
36+
elif k.startswith('>'):
37+
outvars.append(k[1:])
38+
else:
39+
invars.append(k)
40+
outvars.append(k)
41+
for k in invars:
42+
if k in __main__.__dict__:
43+
_set_var(k, __main__.__dict__[k])
2444
ans = Main.seval('begin\n' + code + '\nend')
2545
PythonCall._flush_stdio()
46+
for k in outvars:
47+
__main__.__dict__[k] = _get_var(k)
2648
if not code.strip().endswith(';'):
2749
return ans
2850

0 commit comments

Comments
 (0)