K3D-jupyter icon indicating copy to clipboard operation
K3D-jupyter copied to clipboard

JavascriptException: Message: javascript error: k3dRefresh is not defined

Open basilevh opened this issue 2 years ago • 7 comments

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!

basilevh avatar May 28 '22 18:05 basilevh

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.

Luunynliny avatar May 31 '22 11:05 Luunynliny

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

  • it only happens with Chrome when using headless mode. I never encountered the problem by running Chrome non-headless.
  • Firefox works just fine both in headless and non-headless.

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.

Davide-sd avatar Mar 26 '23 15:03 Davide-sd

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.

dazzag24 avatar Nov 22 '23 15:11 dazzag24

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

artur-trzesiok avatar Jan 06 '24 14:01 artur-trzesiok

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

artur-trzesiok avatar Jan 06 '24 14:01 artur-trzesiok

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).

spanag avatar Mar 04 '24 14:03 spanag