Graphical user interfaces#

In this notebook we ask bob to create a user interface that allows interacting with a plot.

from bia_bob import bob

In this example we use a videogame sales dataset by Gregory Smith which is licensed CC-BY 4.0.

%%bob
write python code that
* opens the file vgsales.csv using pandas
* if the file doesn't exist, open it from https://zenodo.org/records/5898311/files/vgsales.csv?download=1 
  and save it as vgsales.csv

Plan:

  1. Import the pandas library.

  2. Try opening the file vgsales.csv using pandas.

  3. If the file doesn’t exist, download it from the given URL and save it as vgsales.csv.

Code:

import pandas as pd

# Try opening the file vgsales.csv
try:
    df = pd.read_csv('vgsales.csv')
except FileNotFoundError:
    # Download the file from the URL and save it as vgsales.csv
    df = pd.read_csv('https://zenodo.org/records/5898311/files/vgsales.csv?download=1')
    df.to_csv('vgsales.csv', index=False)
%%bob
Build a graphical user interface using ipywidgets where the user 
can select the publisher. After the publisher is selected, draw 
a bar plot showing the NA_Sales, EU_Sales, JP_Sales and Other_Sales 
from the existing dataframe.
The bar-plot should be shown right below the pulldown and updated 
once the pulldown changes.

Plan:

  1. Import the required libraries: pandas, ipywidgets, and matplotlib.pyplot.

  2. Load the vgsales.csv file using pandas and store it in a DataFrame.

  3. Extract the unique publishers from the DataFrame.

  4. Create a dropdown widget using ipywidgets and populate it with the unique publishers.

  5. Create an output widget to display the bar plot.

  6. Create a function that takes the selected publisher as an argument, plots the bar plot of NA_Sales, EU_Sales, JP_Sales, and Other_Sales, and updates the output widget with the plot.

  7. Display the dropdown widget and the initial bar plot in the output widget.

  8. Use the observe method of the dropdown widget to call the plotting function whenever the selected publisher changes.

Code:

import pandas as pd
import ipywidgets as widgets
import matplotlib.pyplot as plt

# Load the vgsales.csv file using pandas
df = pd.read_csv('vgsales.csv')

# Extract the unique publishers from the DataFrame
publishers = df['Publisher'].unique()

# Create the dropdown widget
dropdown = widgets.Dropdown(options=publishers, description='Publisher:')

# Create the output widget for the bar plot
output = widgets.Output()

# Create the function to plot the bar plot
def plot_sales(publisher):
    with output:
        output.clear_output()
        sales = df[df['Publisher'] == publisher][['NA_Sales', 'EU_Sales', 'JP_Sales', 'Other_Sales']]
        sales.plot(kind='bar', legend=False)
        plt.title('Sales by Region')
        plt.xlabel('Region')
        plt.ylabel('Sales')
        plt.show()

# Display the dropdown widget and initial bar plot in the output widget
display(dropdown)
display(output)
plot_sales(dropdown.value)

# Use the observe method of the dropdown widget to update the bar plot
dropdown.observe(lambda change: plot_sales(change.new), names='value')