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

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

Shubham039
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!")
 
display(text_input)
display(button)
 
def on_button_click(b):
    my_function(text_input.value)
 
button.on_click(on_button_click)

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

8 REPLIES 8

-werners-
Esteemed Contributor III

databricks notebooks are not the same as jupyter notebooks.

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

https://learn.microsoft.com/en-us/azure/databricks/notebooks/widgets

https://docs.databricks.com/notebooks/widgets.html

However you can use the ipython kernel:

https://docs.databricks.com/notebooks/ipython-kernel.html

But I am not sure if the button will work.

Shubham039
New Contributor III

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

https://learn.microsoft.com/en-us/azure/databricks/notebooks/ipywidgets

-werners-
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:
      output.clear_output()
      df = load_sample_df('<catalog>.<schema>.<table>')
      print(df.toPandas())
 
# Register the button's callback function to query UC and display results to the output widget
button.on_click(on_button_clicked)
 
display(button, output)

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

-werners-
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?

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

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

Cheers!

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.