Skip to content

Commit 3147fb8

Browse files
author
Christopher Doris
committed
experimental ipython extension
1 parent 50331f5 commit 3147fb8

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

pysrc/juliacall/ipython.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from IPython.core.magic import Magics, magics_class, line_cell_magic
2+
from . import Main, Base, PythonCall
3+
4+
@magics_class
5+
class JuliaMagics(Magics):
6+
7+
@line_cell_magic
8+
def julia(self, line, cell=None):
9+
code = line if cell is None else cell
10+
ans = Main.seval('begin\n' + code + '\nend')
11+
Base.flush(Base.stdout)
12+
Base.flush(Base.stderr)
13+
if not code.strip().endswith(';'):
14+
return ans
15+
16+
def load_ipython_extension(ip):
17+
# register magics
18+
ip.register_magics(JuliaMagics(ip))
19+
# redirect stdout/stderr
20+
PythonCall.seval("""begin
21+
const _redirected_stdout = redirect_stdout()
22+
const _redirected_stderr = redirect_stderr()
23+
const _py_stdout = PyIO(pyimport("sys" => "stdout"), buflen=1)
24+
const _py_stderr = PyIO(pyimport("sys" => "stderr"), buflen=1)
25+
const _redirect_stdout_task = @async write($_py_stdout, $_redirected_stdout)
26+
const _redirect_stderr_task = @async write($_py_stderr, $_redirected_stderr)
27+
end""")

0 commit comments

Comments
 (0)