-
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
Unittest IPython extension + Automatic loading #478
Changes from 5 commits
c0e31a3
d842b8a
88dcb5f
bd12335
dfe5b84
1672826
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -248,8 +248,34 @@ def jlstr(x): | |
"PYTHON_JULIACALL_HANDLE_SIGNALS=no." | ||
) | ||
|
||
init() | ||
# Next, automatically load the juliacall extension if we are in IPython or Jupyter | ||
CONFIG['autoload_ipython_extension'] = choice('autoload_ipython_extension', ['yes', 'no'])[0] | ||
if CONFIG['autoload_ipython_extension'] in {'yes', None}: | ||
try: | ||
get_ipython = sys.modules['IPython'].get_ipython | ||
|
||
if CONFIG['autoload_ipython_extension'] is None: | ||
# Only let the user know if it was not explicitly set | ||
print( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should maybe be a warning, so can be disabled using Python's warnings mechanisms? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm ambivalent about this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried this. If it is a warning, it feels like you need to set the environment variable (one way or the other), and ends up being a pain. When I have this set up I just want it to run automatically and not worry about it. A single-line print seems nice though. I also tried The user can always set that env variable to turn off printing. The printing only happens when it's unset. |
||
"Detected IPython. Loading juliacall extension. To disable, you can either " | ||
MilesCranmer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"set the environment variable PYTHON_JULIACALL_AUTOLOAD_IPYTHON_EXTENSION=no or pass the " | ||
"command line argument '-X juliacall-autoload-ipython-extension=no'. Inside Jupyter, you can " | ||
"do this with `import os; os.environ['PYTHON_JULIACALL_AUTOLOAD_IPYTHON_EXTENSION'] = 'no'`. " | ||
"To suppress this message, set PYTHON_JULIACALL_AUTOLOAD_IPYTHON_EXTENSION=yes." | ||
) | ||
|
||
load_ipython_extension(get_ipython()) | ||
except Exception as e: | ||
if CONFIG['autoload_ipython_extension'] == 'yes': | ||
# Only warn if the user explicitly requested the extension to be loaded | ||
warnings.warn( | ||
"Could not load juliacall extension in Jupyter notebook: " + str(e) | ||
) | ||
pass | ||
|
||
|
||
def load_ipython_extension(ip): | ||
import juliacall.ipython | ||
juliacall.ipython.load_ipython_extension(ip) | ||
|
||
init() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# NBVAL_IGNORE_OUTPUT\n", | ||
"import numpy as np\n", | ||
"from juliacall import Main as jl" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"3\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%%julia\n", | ||
"\n", | ||
"# Automatically activates Julia magic\n", | ||
"\n", | ||
"x = 1\n", | ||
"println(x + 2)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"4\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%julia println(x + 3)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"1.0" | ||
] | ||
}, | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"jl.cos(0)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"f (generic function with 1 method)" | ||
] | ||
}, | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"%%julia\n", | ||
"\n", | ||
"function f(x::AbstractArray)\n", | ||
" sum(x)\n", | ||
"end" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"6" | ||
] | ||
}, | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"jl.f(np.array([1, 2, 3]))" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.10" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you put this near the top of init? Just so if it's set incorrectly the error gets raised early.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we want it come after Julia is initialized? Otherwise we would see the printout for installing Julia, and the extension autoload message would be lost above it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant literally just the
CONFIG['autoload_ipython_extension'] = ...
line.