Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Showing results for 
Search instead for 
Did you mean: 

Databricks notebook ipywidgets not working as expected ( button click issue)

New Contributor III

I am working on Azure databricks(IDE). I wanted to create a button which takes a text value as input and on the click of a button a function needed to be run which prints the value entered.

For that I created this code:

from IPython.display import display
import ipywidgets as widgets
def my_function(param):
    print(f"The parameter is: {param}")
text_input = widgets.Text(description="Enter text:")
button = widgets.Button(description="Click Me!")
def on_button_click(b):

But when I click the button, nothing happens. It should run the `my_function` and print the input text.

Strangely this exact code works fine when I run it in **jupyter notebook**.

I am not able to make it work in **Azure Databricks**. 

Any insights would be helpful


Esteemed Contributor III

databricks notebooks are not the same as jupyter notebooks.

There are widgets on databricks though (but not with a button afaik).

However you can use the ipython kernel:

But I am not sure if the button will work.

New Contributor III

Hi, I'm using the ipywidgets not the older databricks widgets.

Esteemed Contributor III

I see, I missed that.

Buttons should work according to the docs.

The example has output however, maybe that is the issue:

import ipywidgets as widgets
# Create button widget. Clicking this button loads a sampled dataframe from UC table.
button = widgets.Button(description="Load dataframe sample")
# Output widget to display the loaded dataframe
output = widgets.Output()
def load_sample_df(table_name):
  return spark.sql(f"SELECT * FROM {table_name} LIMIT 1000")
def on_button_clicked(_):
    with output:
      df = load_sample_df('<catalog>.<schema>.<table>')
# Register the button's callback function to query UC and display results to the output widget
display(button, output)

Did you try running my code snippet? Is it working fine?

Esteemed Contributor III

yes and it does work.

your function prints to the standard output of the driver

image I Clicked on Click me, but there was no print statement for me.

Any reason why this issue?

Esteemed Contributor III

well it does print, but not in the notebook. it prints to the standard output of the driver (which you can find in the logs of the cluster).

So I guess you have to use something else than print, I am no widget expert though.

Not applicable

Hi @Shubham Ringne​ 

Hope everything is going great.

Just wanted to check in if you were able to resolve your issue. If yes, would you be happy to mark an answer as best so that other members can find the solution more quickly? If not, please tell us so we can help you.