-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jl.println
and jl.display
do not work in Jupyter notebook
#232
Comments
If you do https://cjdoris.github.io/PythonCall.jl/dev/compat/#IPython (It's only in the dev version of the docs because it appears the docs have not built for the last few releases, that's a separate issue.) |
Turns out what I had implemented was pretty hacky. It's much better now, I've made a release, it certainly seems to be much better in VSCode now. |
Yeah, that's because in Julia you can't (reliably) replace stdout in Julia, you can only redirect it, and that needs to happen asynchronously, so anything printed to stdout from Julia may appear out of order with things printed from Python. |
This issue has been marked as stale because it has been open for 30 days with no activity. If the issue is still relevant then please leave a comment, or else it will be closed in 7 days. |
This issue has been closed because it has been stale for 7 days. If it is still |
Can this be done automatically? i.e.,
|
I threw this in PySR and it seems to work (modified from tqdm): try:
get_ipython = sys.modules["IPython"].get_ipython
if 'IPKernelApp' not in get_ipython().config:
raise ImportError("console")
print("Detected Jupyter notebook. Loading juliacall extension.")
get_ipython().run_line_magic("load_ext", "juliacall")
except Exception:
pass |
That seems reasonable - if we also provide a mechanism (env var) to turn this behaviour off. |
Great minds think alike! 😄 Here's the code I ended up releasing in PySR: # Next, automatically load the juliacall extension if we're in a Jupyter notebook
autoload_extensions = os.environ.get("PYSR_AUTOLOAD_EXTENSIONS", "yes")
if autoload_extensions in {"yes", ""} and jl_version >= (1, 9, 0):
try:
get_ipython = sys.modules["IPython"].get_ipython
if "IPKernelApp" not in get_ipython().config:
raise ImportError("console")
print(
"Detected Jupyter notebook. Loading juliacall extension. Set `PYSR_AUTOLOAD_EXTENSIONS=no` to disable."
)
# TODO: Turn this off if juliacall does this automatically
get_ipython().run_line_magic("load_ext", "juliacall")
except Exception:
pass
elif autoload_extensions not in {"no", "yes", ""}:
warnings.warn(
"PYSR_AUTOLOAD_EXTENSIONS environment variable is set to something other than 'yes' or 'no' or ''."
) Want me to PR to juliacall? I think it is nice to have printing work automatically. |
Yeah go for it. Please add a setting so this feature can be disabled. |
The following test was made in a .ipynb notebook in VS code.

We see that
println
has no output at all, whiledisplay
leads to an error.The text was updated successfully, but these errors were encountered: