{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "150de2e4-622f-4d0f-91a2-ad47dd16c53a", "metadata": {}, "outputs": [], "source": [ "# Vizro-AI setup\n", "\n", "import vizro_ai\n", "from dotenv import load_dotenv\n", "from vizro import Vizro\n", "from vizro_ai import VizroAI\n", "\n", "# Ensure the API key is in .env\n", "load_dotenv()\n", "\n", "# Choose your model\n", "\n", "vizro_ai = VizroAI(model=\"gpt-4-turbo\")\n", "\n", "import pandas as pd\n", "\n", "df = pd.read_csv(\"filtered_books.csv\")\n", "df[\"Date Read\"] = pd.to_datetime(df[\"Date Read\"], dayfirst=True)\n", "\n", "# Data cleanup\n", "# Specify columns to check for missing values\n", "columns_to_check = [\n", " \"Title\",\n", " \"Author\",\n", " \"ISBN\",\n", " \"My Rating\",\n", " \"Average Rating\",\n", " \"Number of Pages\",\n", " \"Original Publication Year\",\n", " \"Date Read\",\n", "]\n", "df_cleaned = df.dropna(subset=columns_to_check)" ] }, { "cell_type": "code", "execution_count": 2, "id": "1cbbea13-28f1-47fc-9348-9e41ba178463", "metadata": {}, "outputs": [], "source": [ "# Specify the prompt here\n", "user_question = \"\"\"\n", "Create a dashboard with 3 pages, one for each chart.\n", "\n", "On the first page, plot a chart with the title \"Sequence of reading\" .\n", "It is a scatter chart. Use the x axis to show the date a book was read. Plot it at y=1.\n", "\n", "Add a date picker filter so the user can adjust the range of dates for the Date Read on the x axis.\n", "\n", "On the second page, plot a chart with the title \"Pages and Book totals\" .\n", "It shows the cumulative total number of pages read by summing the Number of Pages of each book read in each year, using the Date Read data.\n", "Plot date on the x axis and the number of pages on the y axis using a scale on the left hand side of the chart.\n", "Superimpose a bar chart showing the total books read for each year, taking data from the Date Read column.\n", "\n", "Show the total books read using the right hand side of the chart which can be a different scale to the y axis shown on the left hand side.\n", "\n", "On the third page, for each row, create a dumbbell chart to show the difference between My Rating and Average Rating for each book.\n", "Use shapes to add the horizontal lines between markers. Omit the legend. Don't show any row where My Rating is 0.\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 3, "id": "14e9f5de-1ace-44cd-8b3a-3f1ee805fe2e", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6e9ee132cfb34f2580a909f391ad60dd", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Store df info: 0%| | 0/1 [00:00<?, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "df_name: book_reading_data\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6695c7d33ed94a76b099df1b3a9e4d9b", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Generate dashboard plan: 0%| | 0/2 [00:00<?, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "fc86ba573acc474abd167410a975f5ca", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Building page: Rating Comparison: 0%| | 0/5 [00:00<?, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "5c911c5bd1514ff7ba4a2bb40aeb85c9", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Building page: Pages and Book Totals: 0%| | 0/5 [00:00<?, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "89e1a4f904b1497e8a334546d7f5c028", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Building page: Sequence of Reading: 0%| | 0/5 [00:00<?, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "38daa73439c446b48d6b0c07b002e9f4", "version_major": 2, "version_minor": 0 }, "text/plain": [] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "539cc8f826b34366a45a1fc097670fb2", "version_major": 2, "version_minor": 0 }, "text/plain": [] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "74415162dee74c1b90e4156457f01bd9", "version_major": 2, "version_minor": 0 }, "text/plain": [] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "e829bf5da11b4f2faad09f1edfec0ee7", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Currently Building ... [Page] <Pages and Book Totals> components: 0%| | 0/1 [00:00<?, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "28059fe9bf9441b1b8c78ed04a1a8cba", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Currently Building ... [Page] <Rating Comparison> components: 0%| | 0/1 [00:00<?, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "28b377d144574f52beb7091fd33ffbe3", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Currently Building ... [Page] <Sequence of Reading> components: 0%| | 0/1 [00:00<?, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "fd398990ab9f473c9b1919524d95c990", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Currently Building ... [Page] <Sequence of Reading> controls: 0%| | 0/1 [00:00<?, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "/Users/jo_stichbury/Documents/GitHub/vizro/vizro-core/vizro/lib/python3.10/site-packages/pydantic/v1/schema.py:631: UserWarning:\n", "\n", "CapturedCallable function is excluded from the schema.\n", "\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a39e34543f4b4b119be7fa4846a45e96", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Currently Building ... [Page] <Pages and Book Totals> controls: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "3d9567ab8ad6439885f9387bf0f7ebfe", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Currently Building ... [Page] <Rating Comparison> controls: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "WARNING:root:Could not extract source for <function sequence_reading at 0x13f4d2950>. Definition will not be included.\n", "WARNING:root:Could not extract source for <function pages_books_totals at 0x13f5a9fc0>. Definition will not be included.\n", "WARNING:root:Could not extract source for <function rating_comparison at 0x13f60dd80>. Definition will not be included.\n" ] }, { "data": { "text/html": [ "\n", " <iframe\n", " width=\"100%\"\n", " height=\"650\"\n", " src=\"http://127.0.0.1:8006/\"\n", " frameborder=\"0\"\n", " allowfullscreen\n", " \n", " ></iframe>\n", " " ], "text/plain": [ "<IPython.lib.display.IFrame at 0x13f5d8f40>" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "############ Imports ##############\n", "import vizro.models as vm\n", "from vizro.models.types import capture\n", "import pandas as pd\n", "import plotly.graph_objects as go\n", "from vizro.models.types import capture\n", "\n", "\n", "####### Function definitions ######\n", "@capture(\"graph\")\n", "def rating_comparison(data_frame):\n", " # Filter out rows where 'My Rating' is 0\n", " df_filtered = data_frame[data_frame[\"My Rating\"] != 0]\n", "\n", " # Create a figure\n", " fig = go.Figure()\n", "\n", " # Add traces for 'My Rating' and 'Average Rating'\n", " for index, row in df_filtered.iterrows():\n", " fig.add_trace(\n", " go.Scatter(\n", " x=[row[\"My Rating\"], row[\"Average Rating\"]],\n", " y=[row[\"Title\"], row[\"Title\"]],\n", " mode=\"lines+markers\",\n", " name=\"\",\n", " marker=dict(size=10),\n", " line=dict(color=\"gray\", width=2),\n", " )\n", " )\n", "\n", " # Update layout to hide the legend\n", " fig.update_layout(showlegend=False)\n", "\n", " return fig\n", "\n", "\n", "@capture(\"graph\")\n", "def sequence_reading(data_frame):\n", " # Sorting data by 'Date Read'\n", " sorted_df = data_frame.sort_values(\"Date Read\")\n", " # Creating a scatter plot\n", " fig = go.Figure()\n", " fig.add_trace(\n", " go.Scatter(\n", " x=sorted_df[\"Date Read\"],\n", " y=[1] * len(sorted_df),\n", " mode=\"markers\",\n", " marker=dict(color=\"blue\", size=10),\n", " name=\"Books Read\",\n", " )\n", " )\n", " fig.update_layout(\n", " title=\"Sequence of Books Read Over Time\",\n", " xaxis_title=\"Date Read\",\n", " yaxis_title=\"Sequence\",\n", " yaxis=dict(showticklabels=False),\n", " )\n", " return fig\n", "\n", "\n", "@capture(\"graph\")\n", "def pages_books_totals(data_frame):\n", " # Prepare data\n", " data_frame[\"Date Read\"] = pd.to_datetime(data_frame[\"Date Read\"])\n", " data_frame.sort_values(\"Date Read\", inplace=True)\n", " data_frame[\"Cumulative Pages\"] = data_frame[\"Number of Pages\"].cumsum()\n", " yearly_books = data_frame.groupby(data_frame[\"Date Read\"].dt.year).size()\n", "\n", " # Create figure\n", " fig = go.Figure()\n", "\n", " # Add cumulative pages line\n", " fig.add_trace(\n", " go.Scatter(\n", " x=data_frame[\"Date Read\"],\n", " y=data_frame[\"Cumulative Pages\"],\n", " mode=\"lines\",\n", " name=\"Cumulative Pages Read\",\n", " )\n", " )\n", "\n", " # Add books read per year bar\n", " fig.add_trace(\n", " go.Bar(\n", " x=yearly_books.index, y=yearly_books, name=\"Books Read per Year\", yaxis=\"y2\"\n", " )\n", " )\n", "\n", " # Layout adjustments\n", " fig.update_layout(\n", " title=\"Cumulative Pages Read and Books Read per Year\",\n", " xaxis_title=\"Date Read\",\n", " yaxis_title=\"Total Pages Read\",\n", " yaxis2=dict(title=\"Books Read per Year\", overlaying=\"y\", side=\"right\"),\n", " )\n", "\n", " return fig\n", "\n", "\n", "####### Data Manager Settings #####\n", "#######!!! UNCOMMENT BELOW !!!#####\n", "# from vizro.managers import data_manager\n", "# data_manager[\"book_reading_data\"] = ===> Fill in here <===\n", "\n", "\n", "########### Model code ############\n", "model = vm.Dashboard(\n", " pages=[\n", " vm.Page(\n", " components=[\n", " vm.Graph(\n", " id=\"sequence_reading\",\n", " figure=sequence_reading(data_frame=\"book_reading_data\"),\n", " )\n", " ],\n", " title=\"Sequence of Reading\",\n", " layout=vm.Layout(grid=[[0]]),\n", " controls=[\n", " vm.Filter(\n", " type=\"filter\",\n", " column=\"Date Read\",\n", " targets=[\"sequence_reading\"],\n", " selector=vm.DatePicker(type=\"date_picker\", range=True),\n", " )\n", " ],\n", " ),\n", " vm.Page(\n", " components=[\n", " vm.Graph(\n", " id=\"pages_books_totals\",\n", " figure=pages_books_totals(data_frame=\"book_reading_data\"),\n", " )\n", " ],\n", " title=\"Pages and Book Totals\",\n", " layout=vm.Layout(grid=[[0]]),\n", " controls=[],\n", " ),\n", " vm.Page(\n", " components=[\n", " vm.Graph(\n", " id=\"rating_comparison\",\n", " figure=rating_comparison(data_frame=\"book_reading_data\"),\n", " )\n", " ],\n", " title=\"Rating Comparison\",\n", " layout=vm.Layout(grid=[[0]]),\n", " controls=[],\n", " ),\n", " ],\n", " title=\"Book Reading Analysis Dashboard\",\n", ")\n", "\n" ] } ], "source": [ "result = vizro_ai.dashboard([df_cleaned], user_question, return_elements=True)\n", "Vizro().build(result.dashboard).run(port=8006)\n", "print(result.code)" ] }, { "cell_type": "code", "execution_count": null, "id": "c893170d-756b-4ca8-a3a2-d3646ac8e595", "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.10.15" } }, "nbformat": 4, "nbformat_minor": 5 }