-
Notifications
You must be signed in to change notification settings - Fork 82
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
Make ImageJ GUI work on macOS #23
Comments
Same issue here, the script hangs when
import time
import imagej
ij = imagej.init('/Applications/Fiji.app', headless=False)
ij.ui().showUI()
while True:
time.sleep(1)
However, the fiji menu didn't show as expected, any idea? @ctrueden |
@oeway Hmm. I guess that since Java is started inside the Python process, the usual macOS menu bar logic does not work? I don't immediately know how to fix it. But one quick workaround is to use: ij = imagej.init('/Applications/Fiji.app', headless=False)
ij.ui().showUI("swing") This will pop up the Swing UI instead of the legacy ImageJ1 UI. In my quick experimentation, major things including legacy IJ1 plugins do still work—although you may encounter rough edges in some cases. Relatedly: I am exploring integrating the |
This issue has been mentioned on Image.sc Forum. There might be relevant details there: https://forum.image.sc/t/pyimagej-imagej-py-show-issues-on-macos/37472/1 |
Has this been integrated? |
@mmarras Unfortunately not. I tried hard last December with @hanslovsky to figure out how to do it, but it's deceptively tricky to make it totally seamless. We just finished with the 1.0.0 release though, which is now built on JPype rather than PyJNIus. It is possible that JPype somehow avoids this problem; haven't tested it yet. I'll report back soon. If this issue is still a problem, we'll keep trying. |
Not sure if this is relevant but there was a piece of code for darwin to deal with app issues. I have never used is so not sure if it helps. https://github.com/jpype-project/jpype/blob/master/jpype/_gui.py |
I am still encountering this issue with the latest install.
Causes the notebook to hang. Is there a workaround or fix for this on MacOS? Conda env:
|
This uses jpype.setupGuiEnvironment. It works, but: * You must have pyobjc installed from pip (conda packages did not work for me). * With this change, it is now inconsistent whether imagej.init(headless=False) pops a GUI or not. On macOS yes, on Linux and Windows no, with ij.ui().showUI() needing to be called. * This change does not fix the fact that imagej.init(headless=False) is non-blocking on Linux and Windows, but blocking on macOS. This is because on macOS the main thread must eternal stay busy doing the console event loop from this point forward. See #23.
One year later, finally had time to look at this again. @Thrameos As usual you are correct: that code is exactly what we needed, thank you. Much more concise than the I have now pushed a couple of commits making the situation less horrible on macOS: 27aea88 and f54e59e. @hinerm @elevans I would value your feedback. In particular:
So: thoughts? Ways to improve this, so we can finally put this issue to rest? Perhaps something as simple as adjusting the |
My current thinking is to add a new
For For the old |
Here are my 2 cents:
|
@elevans Correct on all counts. At least this way, you can get a GUI on macOS. Previously, if you tried, it would just hang forever. I do not know a way to get a GUI on macOS without blocking, within the same process/subprocess. We could do it using
From the main thread, yes. But if someone is running Python code on other threads, those could presumably still make new ImageJ2 gateways. |
With #23 being fixed, it is probably not relevant anymore. If someone reports that the default (legacy) UI does not work on macOS for them, but the ImageJ2 Swing UI does, we can reintroduce something.
Is there an issue that we can track regarding the absence of interactive mode for Mac OSX ? |
This issue has been mentioned on Image.sc Forum. There might be relevant details there: https://forum.image.sc/t/abba-aligning-big-brains-and-atlases-v0-8-0-released/91229/1 |
Issues relating to this problem are tagged with |
For a long time I have been trying to get an option in JPype which will launch the Java JVM in another thread automatically. Unfortunately I have never had access to a Mac so I have not had any opportunities to test the solution. The required mods would be to
It should be 20 liner for someone who knows Python threading system. If someone can mod JPype, test in on a Mac to confirm that getting the JVM off the main thread fixes the gui issue, and the submit it I would appreciate it. |
Hi @Thrameos, Thanks for this reply, I really appreciate it! This problem is a real pain point for a lot of our users. I have access to a Mac here so I'll give what you suggested a try 🚀. I'm pretty busy but I'll try to report back here in a few days. |
Okay I couldn't resist. I forked >>> import imagej
>>> ij = imagej.init()
Traceback (most recent call last):
File "ImageJ.java", line 75, in net.imagej.ImageJ.<init>
java.lang.java.lang.NullPointerException: java.lang.NullPointerException
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "LegacyService.java", line 142, in net.imagej.legacy.LegacyService.<clinit>
java.lang.java.lang.RuntimeException: java.lang.RuntimeException: Found incompatible ImageJ class
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "ImageJ.java", line 75, in net.imagej.ImageJ.<init>
java.lang.java.lang.ExceptionInInitializerError: java.lang.ExceptionInInitializerError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "ImageJ.java", line 75, in net.imagej.ImageJ.<init>
Exception: Java Exception
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/edward/Documents/repos/loci/pyimagej/src/imagej/__init__.py", line 1249, in init
return _create_gateway()
^^^^^^^^^^^^^^^^^
File "/home/edward/Documents/repos/loci/pyimagej/src/imagej/__init__.py", line 1279, in _create_gateway
ij = ImageJ()
^^^^^^^^
java.lang.java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Invalid service: net.imagej.legacy.LegacyService I haven't tried this on a Mac yet. I'll try to get on one tomorrow. |
@NicoKiaru We filed a dedicated issue for getting interactive mode working on macOS: #298. The investigations @elevans mentioned above did not actually pan out. See #298 for details. Meanwhile, I was able to get a Python REPL simultaneously with a Java GUI by starting Python in a separate thread using python.c#include <stdio.h>
#include <dlfcn.h>
#include <CoreFoundation/CoreFoundation.h>
#include <pthread.h>
static void dummy_call_back(void *info) { }
static int start_python() {
// Load libpython dynamically.
const char *libpython_path =
"/usr/local/Caskroom/mambaforge/base/envs/pyimagej-dev/lib/libpython3.10.dylib";
void *libpython = dlopen(libpython_path, RTLD_LAZY);
if (!libpython) {
fprintf(stderr, "Error loading libpython3.10: %s\n", dlerror());
return 1;
}
typedef int (*Py_MainFunc)(int, char **);
Py_MainFunc Py_Main = (Py_MainFunc)dlsym(libpython, "Py_Main");
if (!Py_Main) {
fprintf(stderr, "Error finding Py_Main function: %s\n", dlerror());
dlclose(libpython);
return 1;
}
const char *args[] = {};
int result = Py_Main(0, (char **)args);
if (result != 0) {
fprintf(stderr, "Error running Python script: %d\n", result);
dlclose(libpython);
return 1;
}
return 0;
}
static void *run_python(void *dummy) {
exit(start_python());
}
int main() {
// Start Python on a dedicated thread.
pthread_t thread;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&thread, &attr, run_python, NULL);
pthread_attr_destroy(&attr);
// Run the AppKit event loop here on the main thread.
CFRunLoopSourceContext context;
memset(&context, 0, sizeof(context));
context.perform = &dummy_call_back;
CFRunLoopSourceRef ref = CFRunLoopSourceCreate(NULL, 0, &context);
CFRunLoopAddSource (CFRunLoopGetCurrent(), ref, kCFRunLoopCommonModes);
CFRunLoopRun();
return 0;
} More to come on issue #298, hopefully. |
When launching ImageJ with a display from Python on macOS, it hangs. See 68dad62 for the current workaround and links to more information. The pyimagej code should try to be clever and start the Cocoa event loop itself before spinning up Java.
See also
https://github.com/imglib/imglyb/blob/1ff6fd12ae5270093be6f3ce340247bde1a45dfc/imglyb/OSXAWTwrapper.py
The text was updated successfully, but these errors were encountered: