Databricks notebook ipywidgets not working as expected ( button click issue)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2023 02:37 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2023 02:55 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2023 02:59 AM
Hi, I'm using the ipywidgets not the older databricks widgets.
https://learn.microsoft.com/en-us/azure/databricks/notebooks/ipywidgets
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2023 03:10 AM
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2023 03:28 AM
Did you try running my code snippet? Is it working fine?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2023 04:01 AM
yes and it does work.
your function prints to the standard output of the driver
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2023 04:12 AM
I Clicked on Click me, but there was no print statement for me.
Any reason why this issue?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2023 04:17 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2023 03:24 AM
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!