When running an algorithm in a python plugin, let’s say an inference with an nnUNet which takes a minute or two, is it possible to show in the GUI a progress bar or any sign that it is working like a loading image? Or can we deactivate the compute button until the computation is finished?
we found a workaround for now that seems to do the job. the solution was to create and show another window in a separate thread. I put the code here in case someone else would need it:
def show_loading_window(self, stop_event):
"""Function to create and display the loading window."""
window = tk.Tk()
window.title("Loading")
window.geometry("200x100")
# Set the background color of the window
window.configure(bg="darkgray")
# Create a label with white text and dark gray background
label = tk.Label(
window,
text="Loading...",
font=("Arial", 14),
bg="darkgray",
fg="white" # Text color
)
label.pack(expand=True, fill="both")
# Check periodically if the stop event is set
def check_stop():
if stop_event.is_set():
window.destroy() # Close the window
else:
window.after(100, check_stop) # Check again after 100 ms
# Start the periodic check
check_stop()
# Start the tkinter event loop
window.mainloop()
def compute(self):
stop_event = threading.Event() # Event to signal when to stop the loading window
# Start the loading window in a separate thread
loading_thread = threading.Thread(target=self.show_loading_window, args=(stop_event,), daemon=True)
loading_thread.start()
self.load_checkpoint()
image_imfusion = self.image # Assuming a single image is provided
img_path = self.tmp_path
imfusion.save(image_imfusion, img_path)
img, props = SimpleITKIO().read_images([img_path])
props = fix_properties(props)
image_imfusion = image_imfusion[0]
affine = image_imfusion.world_to_image_matrix
spacing = image_imfusion.spacing
output = self.predictor.predict_single_npy_array(
img,
props,
)
output = np.expand_dims(output, axis=-1)
image_out = imfusion.SharedImage(output)
image_out.world_to_image_matrix = affine
image_out.spacing = spacing
self.imageset_out.add(image_out)
self.imageset_out.modality = imfusion.Data.Modality.LABEL
# Once done, set the stop event to close the loading window
print("Main task completed. Closing loading window...")
stop_event.set()
# Ensure the loading thread has time to finish
loading_thread.join()