Skip to content
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

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

Open
basilevh opened this issue May 28, 2022 · 7 comments
Open

Comments

@basilevh
Copy link

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
Copy link
Contributor

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
Copy link
Contributor

Davide-sd commented Mar 26, 2023

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.

@dazzag24
Copy link

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
Copy link
Collaborator

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

@artur-trzesiok
Copy link
Collaborator

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

@spanag
Copy link

spanag commented Mar 4, 2024

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

@GregorKobsik
Copy link

GregorKobsik commented Sep 18, 2024

Hi,

I did experience a similar error in my application. I run a custom ML model on a SLURM cluster and wanted to render the output with k3d and save the image to tensorboard logger.

I used the get_headless_firefox_driver function to instantiate my headless renderer k3d_remote(plot, get_headless_firefox_driver(), width, height), but unfortunatelly, this does not work out for me. I do not have the time to debug this issue myself, but wanted to report the problem.

  File "/clusterstorage/gkobsik/geometry-abstraction/main_cuboid_abstraction_v1.py", line 505, in log_visualization
    image = render_headless(headless)
  File "/clusterstorage/gkobsik/geometry-abstraction/utils/render.py", line 175, in render_headless
    headless.sync()
  File "/clusterstorage/gkobsik/miniforge3/envs/py3d/lib/python3.10/site-packages/k3d/headless.py", line 121, in sync
    self.browser.execute_script("k3dRefresh()")
  File "/clusterstorage/gkobsik/miniforge3/envs/py3d/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 414, in execute_script
    return self.execute(command, {"script": script, "args": converted_args})["value"]
  File "/clusterstorage/gkobsik/miniforge3/envs/py3d/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 354, in execute
    self.error_handler.check_response(response)
  File "/clusterstorage/gkobsik/miniforge3/envs/py3d/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: ReferenceError: k3dRefresh is not defined
Stacktrace:
@http://localhost:8080/headless.html:2:7
@http://localhost:8080/headless.html:3:8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants