Skip to content

Commit bdc884b

Browse files
author
Christopher Doris
committed
documentation
1 parent 7c265b1 commit bdc884b

File tree

6 files changed

+56
-18
lines changed

6 files changed

+56
-18
lines changed

docs/src/compat.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
Some packages require a little extra help to work nicely with PythonCall.
44

5-
Some of these are "fixes" that are silently applied for you, and some are just extra functions to bridge a gap. We aim to keep these as minimal as possible.
5+
Some of these are "fixes" that are silently applied for you, and some are just extra
6+
functions to bridge a gap. We aim to keep these as minimal as possible.
67

78
## Stdlib
89

@@ -58,6 +59,11 @@ PythonCall.fix_qt_plugin_path
5859

5960
## IPython
6061

61-
If Python is running an IPython kernel, then:
62-
- Currently disabled: Julia's `Base.stdout` is set to Python's `sys.stdout`.
63-
- A `PythonDisplay` and `IPythonDisplay` are pushed onto Julia's display stack, so that `display(x)` goes to IPython if possible.
62+
The `juliacall.ipython` IPython extension adds these features to your IPython session:
63+
- The line magic `%jl code` executes the given Julia code in-line.
64+
- The cell magic `%%jl` executes a cell of Julia code.
65+
- Julia's `stdout` and `stderr` are redirected to IPython.
66+
- Calling `display(x)` from Julia will display `x` in IPython.
67+
68+
Enable the extension with `%load_ext juliacall.ipython`.
69+
See https://ipython.readthedocs.io/en/stable/config/extensions/.

pysrc/juliacall/importer.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1-
import base64
1+
"""Experimental module for loading Julia files as Python modules.
2+
3+
Being experimental, it does not form part of the JuliaCall API. It may be changed or removed
4+
in any release.
5+
6+
Basic usage is:
7+
import juliacall.importer
8+
juliacall.importer.install()
9+
10+
And now you can import any Julia file as Python module. That is, if 'foo.jl' is in your
11+
Python path, then 'import foo' will work.
12+
13+
By default, all objects at the top level of the file whose name does not start with '_' are
14+
exposed. Alternatively, you may explicitly 'export' objects.
15+
16+
The importer is intended for development use. To release a package, use 'gen_file' on each
17+
Julia module file to generate a corresponding Python file and only include the Python files
18+
in the release.
19+
"""
20+
221
import io
322
import os
423
import sys
@@ -28,11 +47,19 @@ def find_spec(self, fullname, path, target=None):
2847
return ModuleSpec(fullname, SourceFileLoader(fullname, pyfile), origin=jlfile)
2948

3049
def install(**kw):
50+
"""Install the Julia importer.
51+
52+
After calling this, files ending '.jl' in your Python path may be imported as Python
53+
modules.
54+
55+
The return value may be passed to 'uninstall'.
56+
"""
3157
finder = Finder(**kw)
3258
sys.meta_path.insert(0, finder)
3359
return finder
3460

3561
def uninstall(finder):
62+
"""Uninstall the Julia importer."""
3663
sys.meta_path.remove(finder)
3764

3865
def gen_code(jl):
@@ -46,6 +73,7 @@ def gen_code(jl):
4673
return buf.getvalue()
4774

4875
def gen_file(jl, py):
76+
"""Convert a Julia module file to a Python module file."""
4977
with open(jl, encoding='utf-8') as fp:
5078
jlcode = fp.read()
5179
pycode = gen_code(jlcode)

pysrc/juliacall/ipython.py

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
"""Experimental IPython extension for Julia.
2+
3+
Being experimental, it does not form part of the JuliaCall API. It may be changed or removed
4+
in any release.
5+
6+
Enable the extension by calling the magic '%load_ext juliacall.ipython'.
7+
8+
Features:
9+
- Magic `%jl code` to evaluate a piece of Julia code in-line.
10+
- Cell magic `%%jl` to evaluate a cell of Julia code.
11+
- Julia's stdin and stdout are redirected to Python's stdin and stdout.
12+
- Calling Julia's 'display(x)' function will display 'x' in IPython.
13+
"""
14+
115
from IPython.core.magic import Magics, magics_class, line_cell_magic
216
from . import Main, Base, PythonCall
317

@@ -17,11 +31,14 @@ def load_ipython_extension(ip):
1731
# register magics
1832
ip.register_magics(JuliaMagics(ip))
1933
# redirect stdout/stderr
34+
# push displays
2035
PythonCall.seval("""begin
2136
const _redirected_stdout = redirect_stdout()
2237
const _redirected_stderr = redirect_stderr()
2338
const _py_stdout = PyIO(pyimport("sys" => "stdout"), buflen=1)
2439
const _py_stderr = PyIO(pyimport("sys" => "stderr"), buflen=1)
2540
const _redirect_stdout_task = @async write($_py_stdout, $_redirected_stdout)
2641
const _redirect_stderr_task = @async write($_py_stderr, $_redirected_stderr)
42+
pushdisplay(PythonDisplay())
43+
pushdisplay(IPythonDisplay())
2744
end""")

src/PythonCall.jl

-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ function __init__()
106106
init_stdlib()
107107
init_pyshow()
108108
init_gui()
109-
init_ipython()
110109
init_tables()
111110
init_ctypes()
112111
init_numpy()

src/compat/ipython.jl

-11
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,3 @@ function Base.display(d::IPythonDisplay, @nospecialize(x))
6868
ipy.display.display(dict, raw=true)
6969
return
7070
end
71-
72-
function init_ipython()
73-
# EXPERIMENTAL: IPython integration
74-
if C.CTX.is_embedded && CONFIG.auto_ipython_display
75-
is_ipython = ("IPython" in pysysmodule.modules) && !pyisnone(pysysmodule.modules["IPython"].get_ipython())
76-
if is_ipython
77-
pushdisplay(PythonDisplay())
78-
pushdisplay(IPythonDisplay())
79-
end
80-
end
81-
end

src/config.jl

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ Base.@kwdef mutable struct Config
22
meta :: String = ""
33
auto_sys_last_traceback :: Bool = true
44
auto_fix_qt_plugin_path :: Bool = true
5-
auto_ipython_display :: Bool = true
65
end
76

87
const CONFIG = Config()

0 commit comments

Comments
 (0)