Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

382
Vistas
Call Javascript from a button click event function in Jupyter Lab or Jupyter notebook

In jupyter cell, I have the following code, and I can see the it works with "hello world!" printed out in Chrome console.

from IPython.display import Javascript
my_js = """
console.log('hello world!');
"""
Javascript(my_js)

Now, I create a button in jupyter and move that function to button click event. Now, when I click button, the event is executed, but the javascript doesn't work and no output in chrome console:

button = widgets.Button(
   description='Button',
   disabled=False,
   button_style='', 
   tooltip='Button',
   icon='check' 
)

def on_button_clicked(b):
   print("on_button_clicked called")
   my_js = """
   console.log('Hello world2!');
   """
   Javascript(my_js)
   print("on_button_clicked call ended")
   
button.on_click(on_button_clicked)

display(button)

enter image description here

Any idea? Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are creating an object of Javascript class, but then discarding it (as you are not assigning it to any variable, nor displaying it). This works with your first use of Javascript because when the last result of an IPython cell is not assigned to a variable it will be automatically displayed; this also means that you can skip display() call when displaying a button (if it is the last line in the cell):

button = widgets.Button(
    description='Button',
    disabled=False,
    button_style='',
    tooltip='Button',
    icon='check'
)
button.on_click(on_button_clicked)
# notice lack of `display()` call:
button

but it does not work if the object to be displayed is inside the function callback, because IPython does not know which expressions to display and which to ignore. Javascript objects need to be displayed in order for the JavaScript code to be executed by your browser. To make it work:

  • use display explicitly on Javascript object, and
  • define an output area in which the object should be displayed in

For example, cell 1:

out = widgets.Output(layout={'border': '1px solid black'})
out

And then:

def on_button_clicked(b):
    my_js = """
    console.log('Hello world2!');
    """
    with out:
        display(Javascript(my_js))
        display(Markdown("on_button_clicked called"))

enter image description here

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda