JuliaPy / PythonCall.jl

Python and Julia in harmony.
https://juliapy.github.io/PythonCall.jl/stable/
MIT License
715 stars 61 forks source link

Unittest IPython extension + Automatic loading #478

Closed MilesCranmer closed 2 months ago

MilesCranmer commented 3 months ago

This adds unittesting for the IPython extension, using nbval: https://nbval.readthedocs.io/

As discussed in issue () with @cjdoris, I have also set up the IPython extension to automatically load when it detects the user has loaded the package from inside Jupyter. I've done this with PySR and it has been really nice as you can basically just do import pysr and then start typing out %%julia blocks of code as if you were using a native Julia notebook.

This can be switched off with a new "autoload_ipython_extension" configuration parameter. The default behavior will be to assume "yes" but will also print a helpful message to the user letting them know how to disable it if they desire. Setting this parameter explicitly to "yes" will disable this message.

cjdoris commented 3 months ago

Thanks this looks good - could you resolve the merge conflicts please so CI can run?

MilesCranmer commented 3 months ago

Thanks this looks good - could you resolve the merge conflicts please so CI can run?

I was very confused about that error... It's actually up-to-date with main. Maybe some GitHub bug? Let's see if it goes away with new commits.

MilesCranmer commented 3 months ago

Okay, all suggestions implemented. I made it work for IPython as well.

MilesCranmer commented 3 months ago

My one other question is I fear the current warning is too verbose and a bit annoying:

In [1]: from juliacall import Main as jl
Detected IPython. Loading juliacall extension. To disable, you c
an either set the environment variable PYTHON_JULIACALL_AUTOLOAD
_IPYTHON_EXTENSION=no or pass the command line argument '-X juli
acall-autoload-ipython-extension=no'. Inside Jupyter, you can do
 this with `import os; os.environ['PYTHON_JULIACALL_AUTOLOAD_IPY
THON_EXTENSION'] = 'no'`. To suppress this message, set PYTHON_J
ULIACALL_AUTOLOAD_IPYTHON_EXTENSION=yes.

Imagine getting hit with this every time you load the package...

I propose we instead just make this:

In [1]: from juliacall import Main as jl
Detected IPython. Loading juliacall extension.

and include the other information in the docs. Users can google how to turn it off if they want (my guess is that 99% of users want it turned on), and will land on the docs page with more details.

MilesCranmer commented 3 months ago

All suggestions implemented and I cleaned up the commit history

MilesCranmer commented 3 months ago

Okay I took the advice into account and added docs on both the environment variables page and the IPython page. The automatic loading now links to the IPython page (https://juliapy.github.io/PythonCall.jl/stable/compat/#IPython) which seems nice as it points the user to the API. It's great for people who learn by trying.

cjdoris commented 2 months ago

Thanks muchly :)