K3D-tools / K3D-jupyter

K3D lets you create 3D plots backed by WebGL with high-level API (surfaces, isosurfaces, voxels, mesh, cloud points, vtk objects, volume renderer, colormaps, etc). The primary aim of K3D-jupyter is to be easy for use as stand alone package like matplotlib, but also to allow interoperation with existing libraries as VTK.
MIT License
921 stars 123 forks source link

JavascriptException: Message: javascript error: k3dRefresh is not defined #354

Open basilevh opened 2 years ago

basilevh commented 2 years ago

When I try to run https://github.com/K3D-tools/K3D-jupyter/blob/main/examples/headless.ipynb via Jupyter notebook on a remote Ubuntu 20.04.4 LTS machine with Python 3.7.13 and k3d 2.14.1 installed, calling headless.sync() triggers the following rather hard to interpret error:

---------------------------------------------------------------------------
JavascriptException                       Traceback (most recent call last)
/tmp/ipykernel_649167/1878959545.py in <module>
----> 1 headless.sync()

/proj/vondrick2/basile/venvs/bcv11/lib/python3.7/site-packages/k3d/headless.py in sync(self, hold_until_refreshed)
    108 
    109     def sync(self, hold_until_refreshed=False):
--> 110         self.browser.execute_script("k3dRefresh()")
    111 
    112         if hold_until_refreshed:

/proj/vondrick2/basile/venvs/bcv11/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in execute_script(self, script, *args)
    879         return self.execute(command, {
    880             'script': script,
--> 881             'args': converted_args})['value']
    882 
    883     def execute_async_script(self, script: str, *args):

/proj/vondrick2/basile/venvs/bcv11/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    423         response = self.command_executor.execute(driver_command, params)
    424         if response:
--> 425             self.error_handler.check_response(response)
    426             response['value'] = self._unwrap_value(
    427                 response.get('value', None))

/proj/vondrick2/basile/venvs/bcv11/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    245                 alert_text = value['alert'].get('text')
    246             raise exception_class(message, screen, stacktrace, alert_text)  # type: ignore[call-arg]  # mypy is not smart enough here
--> 247         raise exception_class(message, screen, stacktrace)
    248 
    249     def _value_or_default(self, obj: Mapping[_KT, _VT], key: _KT, default: _VT) -> _VT:

JavascriptException: Message: javascript error: k3dRefresh is not defined
  (Session info: headless chrome=101.0.4951.41)
Stacktrace:
#0 0x55b7559c2533 <unknown>
#1 0x55b7557211e8 <unknown>
#2 0x55b7557240a7 <unknown>
#3 0x55b755723f4b <unknown>
#4 0x55b755724ae2 <unknown>
#5 0x55b755788e03 <unknown>
#6 0x55b755774f42 <unknown>
#7 0x55b755788224 <unknown>
#8 0x55b755774e33 <unknown>
#9 0x55b75574a71a <unknown>
#10 0x55b75574b875 <unknown>
#11 0x55b755a06e1d <unknown>
#12 0x55b755a0a751 <unknown>
#13 0x55b7559f107e <unknown>
#14 0x55b755a0b388 <unknown>
#15 0x55b7559e5fe0 <unknown>
#16 0x55b755a27748 <unknown>
#17 0x55b755a278c8 <unknown>
#18 0x55b755a4170d <unknown>
#19 0x7f29a3347609 <unknown>

Any help is appreciated. Thanks!

Luunynliny commented 2 years ago

Hi @basilevh, thank you for your feedback.

I ran the headless.ipynb notebook on my laptop (Fedora 37, Python 3.10.4, Firefox 100) with the same 2.14.1 version and everything word fine on my side.

However I already had this error message several time while working with headless.sync() but this wasn't always automatic. The more plausible answer I thought this issue may come from must have something to do with your browser / Jupyter Notebook configuration (cache management, installed extension, ...).

Most of the time, rebooting my laptop solve the issue for me.

Davide-sd commented 1 year ago

I'm battling with the same problem. With trials and errors I understood that:

Now to the funny behavior: let's say I'm trying to generate some screenshots in headless mode with Chrome. The first time executing a script always works fine. From the second time, the above errors start displaying at random. Now, If I change the browser to headless Firefox everything works fine. Then, If I switch back to headless Chrome, again the first time always works fine. From the second time the above errors start displaying at random... It's weird, it's like Chrome stores some memory from different sessions. I've tried resetting the cache between executions, both it doesn't help.

Also note that that options like "--disable-dev-shm-usage" or "--no-sandbox" always make Chrome headless to raise that error.

dazzag24 commented 8 months ago

I was having the same issue on MacOS when running the headless.ipynb notebook in VSCode. Since my main browser is Firefox I decided to see if using Firefox instead of Chrome would help. I edited k3d/headless.py in my site-package to add this new function:

def get_headless_firefox_driver(no_headless=False):
    from selenium import webdriver
    from selenium.webdriver.firefox.service import Service as FirefoxService
    from webdriver_manager.firefox import GeckoDriverManager

    options = webdriver.FirefoxOptions()

    options.add_argument("--no-sandbox")

    if not no_headless:
        options.add_argument("--headless")

    return webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()),
                             options=options)

and then replaced the import and call to get_headless_driver with get_headless_firefox_driver. It worked perfectly.

In an ideal world get_headless_driver would have a default of chrome but could accept other browsers if users want.

artur-trzesiok commented 6 months ago

Thanks @dazzag24 ! I like idea to not force chrome. I will add it to official k3d package!

artur-trzesiok commented 6 months ago

Hi! Please check https://pypi.org/project/k3d/2.16.1/ :)

spanag commented 4 months ago

I can reproduce the Firefox problem with jupyter on docker and readthedocs (ie Ubuntu), with the same behavior. Using Firefox fails with ReferenceError: k3dRefresh is not defined: see https://spanag-rtd-tutorial.readthedocs.io/en/latest/headless_k3d.html . If Chrome or Chromium is installed (from a deb file bc snap doesn't work on Docker sadly) the same notebook (with the get_headless_driver that is) is nbexported fine (but can't be used on readthedocs).