jupyter-widgets / ipywidgets

Interactive Widgets for the Jupyter Notebook

Home Page:https://ipywidgets.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Javascript is evaluated twice

A13xs opened this issue · comments

commented

Description

If I execute the following code in Notebook Classic

from typing import List
from IPython.display import Javascript, display
from ipywidgets import interactive, Box

display(Javascript('window.myGlobalVar = 0;')) 

def _fnc_test():
    print ("fnc_test")
    display(Javascript('window.myGlobalVar++; console.log("javascript - " + window.myGlobalVar );')) 

_fwidget = interactive(_fnc_test)    
_layout = Box([_fwidget])
display(_layout)

The browser console display this

javascript - 1 
javascript - 2

But it should show only one line

javascript - 1 

imagen

Reproduce

  1. Create a dockerfile with this content:
FROM jupyter/base-notebook:notebook-6.4.5 AS base

RUN python -m pip install ipywidgets==8.1.1 
  1. Run docker build . -t myimage
  2. Run docker run -p 8888:8888 myimage
  3. In the browser, paste the url that displays the console log
  4. Go to "New" -> "Python 3 (ipykernel)"
  5. Paste the following code into the notebook cell and execute it
from typing import List
from IPython.display import Javascript, display
from ipywidgets import interactive, Box

display(Javascript('window.myGlobalVar = 0;')) 

def _fnc_test():
    print ("fnc_test")
    display(Javascript('window.myGlobalVar++; console.log("javascript - " + window.myGlobalVar );')) 

_fwidget = interactive(_fnc_test)    
_layout = Box([_fwidget])
display(_layout)
  1. Press F12 to display the browser console

Expected behavior

It should display a single line with the message javascript -1

This issue is easily reproducible. Could anybody take a look at it?