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

Add a REPL widget that supports the SciJava ScriptREPL #265

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

kephale
Copy link
Contributor

@kephale kephale commented Jun 8, 2023

This PR adds a widget for the SciJava ScriptREPL, allowing users to interactively write SciJava friendly code directly within napari.

When I was exploring the compatibility of various ImageJ/Fiji plugins with napari-imagej, I frequently found myself wanting to poke at variables/state. That led me to writing this REPL widget so we can quickly poke around on the JVM instead of always needing to deal with jpipe/jimport stuff.

napari_imagej_repl_001
Caption: This is a screenshot of napari-imagej with the REPL widget active and some example output from Jython.

Now updated to have the corrected icon:
image

@kephale
Copy link
Contributor Author

kephale commented Jun 8, 2023

BTW, one thing I noticed with this is that the Clojure language actually crashes this plugin. It looks like one of the Clojure classes from the main clojure jar does not get found. All of the other languages work.

@gselzer
Copy link
Collaborator

gselzer commented Jun 8, 2023

I'm not sure that I'll have the chance to look at the code until Tuesday or so @kephale, but this looks really awesome - thanks for implementing it!

One quick comment - have you considered adding the REPL to the bottom by default? I really like how the napari console sits at the bottom - naively this seems like it should go in the same place.

@kephale
Copy link
Contributor Author

kephale commented Jun 8, 2023

I'm very open to where it goes. I have some concerns about all the consoles/repls getting confused. I was actually a little worried about making this icon so similar to the napari-console icon.

@gselzer
Copy link
Collaborator

gselzer commented Jun 8, 2023

I have some concerns about all the consoles/repls getting confused. I was actually a little worried about making this icon so similar to the napari-console icon.

Yeah, I thought about that too.

As far as the button goes, maybe we could color it differently, or add the ImageJ Logo somewhere? Another option would be to just make the SciJava Script REPL a separate napari plugin - making it show up in the napari menus - then we don't have to worry about a button.

Then there's the question of differentiating the two consoles - does the SciJava Script REPL have a Java UI that we could launch (i.e. launch a separate window), and if so, would that detract from your probing properties if it was in a different window? That could be one way to do it, and would require less Qt code to maintain.

@kephale
Copy link
Contributor Author

kephale commented Jun 8, 2023

SciJava Script REPL have a Java UI

Yes, but I was particularly keen on this for the headless situation, so I do hope that the UI is on the Python side.

Copy link
Collaborator

@gselzer gselzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code itself looks great @kephale, just had one minor suggestion.

I think some documentation, both in the GUI (tooltips) and in the docs would be warranted (maybe this could help in troubleshooting?)

src/napari_imagej/widgets/menu.py Show resolved Hide resolved
@kephale
Copy link
Contributor Author

kephale commented Jul 6, 2023

@gselzer I emailed you 1 more figure for this addition to the docs: https://github.com/kephale/napari-imagej/blob/4ccab72c0d8e2890e9ed27d8e4b5e1bff3da547e/doc/Configuration.rst?plain=1#L106

I've added tooltips + a section in the docs about the REPL.

Copy link
Collaborator

@gselzer gselzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After looking at this again, I'm actually pretty happy with the visual appearance of the REPL - I don't think it's too easy to confuse with the napari console.

One thing that I did think would be cool would be to put a shadow of the napari-imagej logo behind the text edit for a visual reminder of which textedit is which, but I have no idea how you'd do that, so I'm just going to leave it here as an idea.

Using the SciJava REPL
--------------------------------

You can use the SciJava REPL to interactively run SciJava code. This makes it possible to do things like paste existing SciJava scripts into the REPL. More information on scripting in SciJava can be found `here <https://imagej.net/scripting/>`_.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I played a little with the Script REPL, and I couldn't get anything beyond 2+4 to work. I was trying to print Hello World, but I couldn't even figure out how to do that.

So, as a result, I looked here, and I checked out imagej.net/scripting. As someone who is not intimately familiar with that page, I wasn't super sure where to go. I did find the ScriptInterpreter page from that page, which appears to me to be what this is, but I tried typing :help and I didn't get anything.

Then, I typed help, and my napari froze. I went back to my terminal, and I saw this:

(napari-imagej-dev) ➜  napari-imagej git:(scijava-repl) napari
hello
[java.lang.Enum.toString] hello[java.lang.Enum.toString] 
Hello World
[java.lang.Enum.toString] Hello World[java.lang.Enum.toString] 
Hello World
[java.lang.Enum.toString] Hello World[java.lang.Enum.toString] 
Hello World
[java.lang.Enum.toString] Hello World[java.lang.Enum.toString] 
Hello World
[java.lang.Enum.toString] Hello World[java.lang.Enum.toString] 

Welcome to Python 2.7!  This is the online help utility.

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/2.7/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics".  Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".

help> [java.lang.Enum.toString] 
Welcome to Python 2.7!  This is the online help utility.

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/2.7/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics".  Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".

help> 

So, long story short, it looks like the output is not being captured by the TextEdit, and also I'm wondering if we can link to documentation that is more specific to the SciJava REPL.

To solve the output not being captured by the TextEdit, is there an option in SciJava for this @ctrueden?

Comment on lines +98 to +99
Using the SciJava REPL
--------------------------------
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if this would be better suited to the new Usage page of the ReadTheDocs...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or the Troubleshooting page...

Comment on lines +276 to +277
icon = QColoredSVGIcon(resource_path("repl"))
self.setIcon(icon.colored(theme=viewer.theme))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you could replace this with the way that we set the icon here, then the icon will be recolored when the user changes the theme.

Comment on lines 283 to 286
from scyjava import jimport

ByteArrayOutputStream = jimport("java.io.ByteArrayOutputStream")
ScriptREPL = jimport("org.scijava.script.ScriptREPL")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put these imports in java.py, along with all of the other imports? It's kinda nice to use jimport in one place

Comment on lines 63 to 65
from scyjava import jimport

ScriptException = jimport("javax.script.ScriptException")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would also be cool if this jimport was moved to java.py.

Comment on lines +47 to +51
self.repl_button.setToolTip("Show/hide the SciJava REPL")
self.layout().addWidget(self.repl_button)

self.settings_button: SettingsButton = SettingsButton(viewer)
self.settings_button.setToolTip("Show napari-imagej settings")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally I've been putting the tool tips in the constructors of each button - could we move these there for consistency?

Thanks so much for adding the tooltip to the settings button - can't believe I forgot that!

@ctrueden
Copy link
Member

ctrueden commented Jul 22, 2023

@gselzer It seems you started reviewing this as I was actively trying to fix the same problems you discovered. This PR needs some additional work to squash bugs before it can be merged. If you want to get looped into fixing it (which would be really helpful!), I can give you a primer on how the SciJava ScriptREPL works on Monday.

@ctrueden
Copy link
Member

ctrueden commented Jul 26, 2023

I'm still working on this. Need to change ScriptREPL on the scijava-common side to accept an output handler that is not an OutputStream. My plan is to do a generic Consumer<String> and deprecate the existing OutputStream signature to use a lambda that just passes it as a Consumer. Once we have this, we can pass a Python function as the consumer to the REPL.

@ctrueden
Copy link
Member

Progress: scijava/scijava-common@0f3bf44. Was going to release it, but updating scijava-common to pom-scijava 36.0.0 breaks some tests, so gotta sort that out first. Then we can start using the improved ScriptREPL on this PR to finally sort out its input/output handling.

@ctrueden
Copy link
Member

ctrueden commented Aug 3, 2023

Progress today: scijava-common 2.95.0 was released. And I worked further on this branch, rebasing it on top of the latest mainline. Some tests pass now, but others fail. I need to scrutinize the design changes a bit more, and probably reshuffle the commit order to minimize the footprint of the commit implementing the changeover to the nij object.

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

Successfully merging this pull request may close these issues.

None yet

3 participants