Skip to content
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

Open
ShuhuaGao opened this issue Oct 17, 2022 · 13 comments
Open

jl.println and jl.display do not work in Jupyter notebook #232

ShuhuaGao opened this issue Oct 17, 2022 · 13 comments

Comments

@ShuhuaGao
Copy link
Contributor

The following test was made in a .ipynb notebook in VS code.
Snipaste_2022-10-17_17-22-36
We see that println has no output at all, while display leads to an error.

  • Julia version 1.8.2
  • Python version 3.9.12
  • juliacall 0.9.7
@cjdoris
Copy link
Collaborator

cjdoris commented Oct 17, 2022

If you do %load_ext juliacall.ipython first then it will work. I had never tried it from VSCode before, but am happy to see that it does!

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.)

@ShuhuaGao
Copy link
Contributor Author

Thanks. I tested that %load_ext juliacall.ipython in VS Code notebook. It sometimes worked, and the behavior was weired.

  • Work
    Snipaste_2022-10-18_10-00-38

  • Not work. If I insert a jl.versioninfo() at the beginning, then println gives no output.
    Snipaste_2022-10-18_09-59-26

However, jl.VERSION does not have that side effect.

Snipaste_2022-10-18_10-00-38

@cjdoris
Copy link
Collaborator

cjdoris commented Oct 18, 2022

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.

@ShuhuaGao
Copy link
Contributor Author

ShuhuaGao commented Oct 19, 2022

That's great. Now the above issue of versioninfo() is fixed. See figure below.
Snipaste_2022-10-19_15-01-20

However, if I mix up println and display (or even print in Python), the output order seems corrupted.
2
3

Note that, if the same code is written as a python script, then the output order is correct.

@cjdoris
Copy link
Collaborator

cjdoris commented Oct 20, 2022

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. display should always appear in order though.

@ShuhuaGao
Copy link
Contributor Author

display should always appear in order though.
Yes, it is true in my test.

Besides, flush after println may be a workaround to enforce the print in order.
Snipaste_2022-10-21_13-31-57

@github-actions
Copy link
Contributor

github-actions bot commented Sep 7, 2023

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.

@github-actions github-actions bot added the stale Issues about to be auto-closed label Sep 7, 2023
@github-actions
Copy link
Contributor

This issue has been closed because it has been stale for 7 days. If it is still
relevant, please re-open it.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 15, 2023
@cjdoris cjdoris removed the stale Issues about to be auto-closed label Sep 22, 2023
@cjdoris cjdoris reopened this Sep 22, 2023
@MilesCranmer
Copy link
Contributor

MilesCranmer commented Feb 9, 2024

Can this be done automatically? i.e.,

  1. Check if we are in a Jupyter notebook with hasattr(__builtins__, "__IPYTHON__").
  2. Call get_ipython().run_line_magic('load_ext', 'juliacall.ipython').

@MilesCranmer
Copy link
Contributor

MilesCranmer commented Feb 9, 2024

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

@cjdoris
Copy link
Collaborator

cjdoris commented Feb 13, 2024

That seems reasonable - if we also provide a mechanism (env var) to turn this behaviour off.

@MilesCranmer
Copy link
Contributor

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.

@cjdoris
Copy link
Collaborator

cjdoris commented Feb 22, 2024

Yeah go for it. Please add a setting so this feature can be disabled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants