ipywidgets examples(Python)

Loading...

ipywidgets examples

This notebook illustrates how you can use interactive ipywidgets in Databricks notebooks. The examples use one of the datasets that is built-in to Databricks.

For more information about ipywidgets, see the ipywidgets documentation.

This notebook steps through how a data scientist might browse a new dataset. To skip directly to examples of ipywidgets, go to Cell 10.

Requirements

Databricks Runtime 11.0 or above.

The bike-sharing dataset contains two years of daily information about the date, weather, and number of bicycles rented by casual and registered users.

sparkDF = spark.read.csv("/databricks-datasets/bikeSharing/data-001/day.csv", header="true", inferSchema="true")
display(sparkDF)

A common way to explore a new dataset is to plot the variables to look for relationships. The next cell creates a scatter plot of the total number of bicycles rented on a day versus the temperature recorded for that day.

pdf = sparkDF.toPandas()
pdf.plot.scatter(x='temp', y='cnt')

You can create a function to make it easier to browse the different predictors and outcomes.

def f(x ='temp', y = 'cnt'):
  pdf.plot.scatter(x=x, y=y)

Now you can pass in any two column names to plot the relationship.

f('hum', 'casual')

With ipywidgets, you can add interactive controls to your plots. The @interact decorator lets you define interactive widgets with a single line of code.

After you run the cell below, the plot appears with cnt on the y-axis and the default value temp on the x-axis. Because the default value of fit=True, the plot includes a regression line.

You can use the widget selectors above the plot to select a different value for the x-axis and turn on or off the regression line. As you make selections using the widget, changes are reflected immediately in the plot.

For details about the different types of ipywidgets, see the ipywidgets documentation.

import ipywidgets as widgets
import seaborn as sns 
from ipywidgets import interact
 
# In this code, the list ['temp', 'atemp', 'hum', 'windspeed'] creates a drop-down menu widget. 
# Setting a variable to True or False (`fit=True`) creates a checkbox widget.
@interact(column=['temp', 'atemp', 'hum', 'windspeed'], fit=True)
def f(column='temp', fit=True):
  sns.lmplot(x=column, y='cnt', data=pdf, fit_reg=fit)
 

In the following cell, the drop-down menu enables you to plot any of the weather variables in a histogram.
You can also use the bin slider to specify the number of bins in the histogram.

# In this code, `(bins=(2, 20, 2)` defines an integer slider widget that allows values between 2 and 20 with a step size of 2.
@interact(bins=(2, 20, 2), value=['temp', 'atemp', 'hum', 'windspeed'])
def plot_histogram(bins, value):
  pdf = sparkDF.toPandas()
  pdf.hist(column=value, bins=bins)