Generating graphical user interfaces#
In this notebook we ask bob to create a user interface that allows interacting with a plot.
import os
from bia_bob import bob
bob.initialize(endpoint="https://llm.scads.ai/v1",
api_key=os.environ.get('SCADSAI_API_KEY'),
model="deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct")
This notebook may contain text, code and images generated by artificial intelligence.
Used model: deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct, vision model: None, endpoint: https://llm.scads.ai/v1, bia-bob version: 0.25.3.. Do not enter sensitive or private information and verify generated contents according to good scientific practice. Read more: https://github.com/haesleinhuepf/bia-bob#disclaimer
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
import pandas as pd
import os
try:
df = pd.read_csv('vgsales.csv')
except FileNotFoundError:
url = 'https://zenodo.org/records/5898311/files/vgsales.csv?download=1'
df = pd.read_csv(url)
df.to_csv('vgsales.csv', index=False)
# Display the first few rows of the DataFrame to verify the operation
print(df.head())
Rank Name Platform Year Genre Publisher \
0 1 Wii Sports Wii 2006.0 Sports Nintendo
1 2 Super Mario Bros. NES 1985.0 Platform Nintendo
2 3 Mario Kart Wii Wii 2008.0 Racing Nintendo
3 4 Wii Sports Resort Wii 2009.0 Sports Nintendo
4 5 Pokemon Red/Pokemon Blue GB 1996.0 Role-Playing Nintendo
NA_Sales EU_Sales JP_Sales Other_Sales Global_Sales
0 41.49 29.02 3.77 8.46 82.74
1 29.08 3.58 6.81 0.77 40.24
2 15.85 12.88 3.79 3.31 35.82
3 15.75 11.01 3.28 2.96 33.00
4 11.27 8.89 10.22 1.00 31.37
%%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.
The bar-plot should be shown right below the pulldown.
Make sure the plot is updated - and not multiple plots are shown.
import pandas as pd
import ipywidgets as widgets
from IPython.display import display
import matplotlib.pyplot as plt
import seaborn as sns
# Sample data creation (replace this with actual data loading)
data = {
'Publisher': ['Nintendo', 'Sony', 'Nintendo', 'Sony', 'Sony', 'Microsoft', 'Microsoft'],
'NA_Sales': [10, 5, 8, 3, 6, 12, 9],
'EU_Sales': [8, 7, 6, 5, 4, 10, 8],
'JP_Sales': [5, 3, 4, 2, 3, 7, 6],
'Other_Sales': [2, 1, 3, 1, 2, 4, 5]
}
df = pd.DataFrame(data)
# List of unique publishers
publishers = df['Publisher'].unique()
# Create a dropdown
publisher_dropdown = widgets.Dropdown(options=publishers, description='Select Publisher:')
# Function to plot the sales based on selected publisher
def plot_sales(publisher):
filtered_df = df[df['Publisher'] == publisher]
sales_data = filtered_df.drop(columns=['Publisher']).sum().reset_index()
sales_data.columns = ['Sales Type', 'Sales']
plt.figure(figsize=(10, 5))
sns.barplot(x='Sales Type', y='Sales', data=sales_data)
plt.title(f'Sales by Region for {publisher}')
plt.show()
# Display the dropdown
display(publisher_dropdown)
# Link the dropdown to the plot function
widgets.interact(plot_sales, publisher=publisher_dropdown)
<function __main__.plot_sales(publisher)>