Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

393
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda