{ "cells": [ { "cell_type": "markdown", "id": "32b8a530-7f1a-4117-b5cf-17d2d356ed66", "metadata": {}, "source": [ "# Generating graphical user interfaces\n", "In this notebook we ask bob to create a user interface that allows interacting with a plot." ] }, { "cell_type": "code", "execution_count": 1, "id": "c80f2dfa-811f-4553-b65f-877effaa152e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " This notebook may contain text, code and images generated by artificial intelligence.\n", " 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\n", "
\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import os\n", "from bia_bob import bob\n", "bob.initialize(endpoint=\"https://llm.scads.ai/v1\",\n", " api_key=os.environ.get('SCADSAI_API_KEY'), \n", " model=\"deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct\")" ] }, { "cell_type": "markdown", "id": "89a9baff-fb08-4484-b0b0-3a386aa91297", "metadata": {}, "source": [ "In this example we use a [videogame sales dataset by Gregory Smith](https://zenodo.org/records/5898311) which is licensed [CC-BY 4.0](https://creativecommons.org/licenses/by/4.0/legalcode)." ] }, { "cell_type": "code", "execution_count": 2, "id": "4c58edef-c019-4de8-8a5c-e00ae366e6c0", "metadata": {}, "outputs": [], "source": [ "%%bob\n", "write python code that\n", "* opens the file vgsales.csv using pandas\n", "* if the file doesn't exist, open it from https://zenodo.org/records/5898311/files/vgsales.csv?download=1 \n", " and save it as vgsales.csv" ] }, { "cell_type": "code", "execution_count": 3, "id": "b74ca65d-3c06-490d-b9d1-340590be7e22", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Rank Name Platform Year Genre Publisher \\\n", "0 1 Wii Sports Wii 2006.0 Sports Nintendo \n", "1 2 Super Mario Bros. NES 1985.0 Platform Nintendo \n", "2 3 Mario Kart Wii Wii 2008.0 Racing Nintendo \n", "3 4 Wii Sports Resort Wii 2009.0 Sports Nintendo \n", "4 5 Pokemon Red/Pokemon Blue GB 1996.0 Role-Playing Nintendo \n", "\n", " NA_Sales EU_Sales JP_Sales Other_Sales Global_Sales \n", "0 41.49 29.02 3.77 8.46 82.74 \n", "1 29.08 3.58 6.81 0.77 40.24 \n", "2 15.85 12.88 3.79 3.31 35.82 \n", "3 15.75 11.01 3.28 2.96 33.00 \n", "4 11.27 8.89 10.22 1.00 31.37 \n" ] } ], "source": [ "import pandas as pd\n", "import os\n", "\n", "try:\n", " df = pd.read_csv('vgsales.csv')\n", "except FileNotFoundError:\n", " url = 'https://zenodo.org/records/5898311/files/vgsales.csv?download=1'\n", " df = pd.read_csv(url)\n", " df.to_csv('vgsales.csv', index=False)\n", "\n", "# Display the first few rows of the DataFrame to verify the operation\n", "print(df.head())" ] }, { "cell_type": "code", "execution_count": 4, "id": "9ae4de24-9793-4c6a-a95e-8f8bd6faa39f", "metadata": {}, "outputs": [], "source": [ "%%bob\n", "Build a graphical user interface using ipywidgets where the user can select the publisher. \n", "After the publisher is selected, draw a bar plot showing the NA_Sales, EU_Sales, JP_Sales and Other_Sales.\n", "The bar-plot should be shown right below the pulldown.\n", "Make sure the plot is updated - and not multiple plots are shown." ] }, { "cell_type": "code", "execution_count": 5, "id": "3948b5f6-d7c6-408b-9216-d32b2f597e48", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "252eb6162162443e9c7dd0b4292cfe4e", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Dropdown(description='Select Publisher:', options=('Nintendo', 'Sony', 'Microsoft'), value='Nintendo')" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "ed27092d265c421eb15989328dde6412", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(Dropdown(description='Select Publisher:', options=('Nintendo', 'Sony', 'Microsoft'), val…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "import ipywidgets as widgets\n", "from IPython.display import display\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "\n", "# Sample data creation (replace this with actual data loading)\n", "data = {\n", " 'Publisher': ['Nintendo', 'Sony', 'Nintendo', 'Sony', 'Sony', 'Microsoft', 'Microsoft'],\n", " 'NA_Sales': [10, 5, 8, 3, 6, 12, 9],\n", " 'EU_Sales': [8, 7, 6, 5, 4, 10, 8],\n", " 'JP_Sales': [5, 3, 4, 2, 3, 7, 6],\n", " 'Other_Sales': [2, 1, 3, 1, 2, 4, 5]\n", "}\n", "df = pd.DataFrame(data)\n", "\n", "# List of unique publishers\n", "publishers = df['Publisher'].unique()\n", "\n", "# Create a dropdown\n", "publisher_dropdown = widgets.Dropdown(options=publishers, description='Select Publisher:')\n", "\n", "# Function to plot the sales based on selected publisher\n", "def plot_sales(publisher):\n", " filtered_df = df[df['Publisher'] == publisher]\n", " sales_data = filtered_df.drop(columns=['Publisher']).sum().reset_index()\n", " sales_data.columns = ['Sales Type', 'Sales']\n", " \n", " plt.figure(figsize=(10, 5))\n", " sns.barplot(x='Sales Type', y='Sales', data=sales_data)\n", " plt.title(f'Sales by Region for {publisher}')\n", " plt.show()\n", "\n", "# Display the dropdown\n", "display(publisher_dropdown)\n", "\n", "# Link the dropdown to the plot function\n", "widgets.interact(plot_sales, publisher=publisher_dropdown)" ] }, { "cell_type": "code", "execution_count": null, "id": "3acf566d-6b90-4a00-a801-3144800a3cf7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "3961db3d-0b36-497c-85ac-26d3f18e2b9a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "ffa48ce4-12d2-4d96-b28f-004007e1c066", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "2aba129a-24e6-424c-beec-258751fb6569", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.11" } }, "nbformat": 4, "nbformat_minor": 5 }