IJava
IJava copied to clipboard
Executing JShell commands from within notebook
We are evaluating the use of the IJava kernel as part of an introductory course on (Java) programming. It looks very promising, esp. because it nicely integrates with nbgrader. We wondered how one would execute JShell commands from within a notebook, like /vars
? Via magics?
Hi @mrcalvin, very excited to hear that!
In IJava we are interfacing with the JShell API rather than the JShell tool which is why the commands like /vars
don't work out of the box. The benefit is that there is a bit more control which allows us to adapt things to fit more comfortably in a notebook but the downside is that adaptation requires a bit of extra code.
If there are any in particular that you would like prioritized please let me know. /vars
for example can be put together from the variables
method. We would then stick it in a magic like %vars
.
Thanks for the informative reply (tool vs. API)! In fact, when cross-reading the doc on available jshell commands and the API doc, I think an immediately useful starting point would be introspection commands (as they map 1:1 to JShell
methods):
-
%vars
:variables
-
%types
:types
-
%methods
:methods
-
%imports
:imports
I wondered whether a variant of %open
would also be nice, to pick up snippets from a script relative to a given notebook (like a cross-notebook startup script)? This could be used to stage a certain, shared environment of methods and variables, for different students to start working from common grounds. Also, sth. like %save
would be nice, to collect all evaluated snippets into a dump script (to maybe re-process them independently from the Jupyter notebook environment).
@mrcalvin A comment related with %open
magic: there is already a %load
magic which loads and executes a given notebook. I use this approach to set up a notebook for bootstrap my library and I suppose it is quite useful.
Thanks @padreati, I've added the docs label because I'm not sure how accessible this information is. I can't remember if %load
also takes jshell snippets or not.
Also related to docs would be mapping jshell /commands
to %magics
as they exist and their differences.
I agree @mrcalvin, the introspection methods are low hanging fruit. If I remember correctly, I think I was holding off on the implementation until the API for them was better defined. Magics return a value (really they are just jupyter like syntax for global functions) and it was not immediately clear what to return.
Would like to avoid leaking JShell API directly so we don't get stuck to it, but are lists of strings enough? Should a variable be a POJO with name and type? Should there be methods on there for looking up the variable value? Any opinions here are very much welcome and appreciated.
For %save
, the use case you have described (dump effective notebook to script) does seem useful enough to overlap in functionality from what Jupyter and nbconvert does for exporting notebooks as a jshell script. Specifically eyeing the snippets()
method with some filtering for active ones. This would essentially allow for taking a snapshot of the current state which I can see people enjoying.
I was thinking of the same thing as @mrcalvin suggested. I even tried it because I expecting it to work that way. So I hope you'll be able to implement this in the near future.