niivue / ipyniivue

A WebGL-powered Jupyter Widget for Niivue based on anywidget
BSD 2-Clause "Simplified" License
25 stars 8 forks source link

run_custom_code is painfully slow #24

Closed christian-oreilly closed 5 months ago

christian-oreilly commented 1 year ago

The following code

import ipyniivue
import time

nv = ipyniivue.Niivue()
t1 = time.time() 
is_pial = nv.run_custom_code('this.nv.isMeshExt("test.pial")')
print(time.time() - t1)

launched on Google Colab takes about 60s to run. My understanding is that this code just checks if "pial" is a mesh extension, which should be a very fast operation. All similar calls seem to be that slow. This can be tested using the example notebook on running custom code.

christian-oreilly commented 1 year ago

Let me wrap this also in this issue to avoid creating another issue just for that: The run_custom_code also seems to fail to return values. Printing is_pial in this example currently gives me None. With fixing this, we should add a test for pytest that avoids code regression on that. This is a fairly simple test to add.

AnthonyAndroulakis commented 1 year ago

Thank you for bringing this up. run_custom_code does not work online Jupyter environments, such as Google Colab, due to the way online Jupyter handles concurrent execution. See https://github.com/Kirill888/jupyter-ui-poll/issues/23#issuecomment-1321256681 for more info.

Concerning ipyniivue, we can either add note to the documentation specifying that run_custom_code (and therefore all getters that use run_custom_code) do not work in online Jupyter environments. Alternatively, we could explore implementing some solution that runs in online Jupyter environments. Currently, I don't know of any ways to do this.

christian-oreilly commented 1 year ago

What is the difference between local and "online" Jupyter environments exactly? The issue you referred to seems to make a difference between ipykernel (works) and jupyterlite (does not work). I may be mistaken but I did not see any clear restrictions (except in the version maybe) for ipykernel on the Colab...? Also, jupyter-ui-poll seems to work on Colab (https://github.com/Kirill888/jupyter-ui-poll/issues/18). It seems like there was some issues with ipykernel 4.x, which was the default for Colab at the time the ticket was created, but the current version is now 5.5.6.

AnthonyAndroulakis commented 1 year ago

I see, I think I was confused with JupyterLite and Google Colab, and just assumed they used the same kernel. I'll have to do more testing to see if I can get run_custom_code to work in Colab then. The reason it takes at most 60 seconds to run the run_custom_code function is because it times out at 60 seconds.

christian-oreilly commented 1 year ago

So, a deeper issue is the transparency of JS errors from Python side. This should cause a timeout exception on Python side so that the user knows what is happening.