{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "326d87ce-0d8f-4950-bcb2-2416019e60f5", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idslabelsparents
0AromasAromasNaN
1TastesTastesNaN
2Aromas-EnzymaticEnzymaticAromas
3Aromas-Sugar BrowningSugar BrowningAromas
4Aromas-Dry DistillationDry DistillationAromas
............
91Pungent-ThymeThymeSpicy-Pungent
92Smokey-TarryTarryCarbony-Smokey
93Smokey-Pipe TobaccoPipe TobaccoCarbony-Smokey
94Ashy-BurntBurntCarbony-Ashy
95Ashy-CharredCharredCarbony-Ashy
\n", "

96 rows × 3 columns

\n", "
" ], "text/plain": [ " ids labels parents\n", "0 Aromas Aromas NaN\n", "1 Tastes Tastes NaN\n", "2 Aromas-Enzymatic Enzymatic Aromas\n", "3 Aromas-Sugar Browning Sugar Browning Aromas\n", "4 Aromas-Dry Distillation Dry Distillation Aromas\n", ".. ... ... ...\n", "91 Pungent-Thyme Thyme Spicy-Pungent\n", "92 Smokey-Tarry Tarry Carbony-Smokey\n", "93 Smokey-Pipe Tobacco Pipe Tobacco Carbony-Smokey\n", "94 Ashy-Burnt Burnt Carbony-Ashy\n", "95 Ashy-Charred Charred Carbony-Ashy\n", "\n", "[96 rows x 3 columns]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/sunburst-coffee-flavors-complete.csv')\n", "df" ] }, { "cell_type": "code", "execution_count": 2, "id": "f2a40c25-8816-44be-bcdc-e5f2e86242f5", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "initial ran\n" ] } ], "source": [ "from dash import Dash, html, dcc, exceptions, Input, Output\n", "import itertools as it\n", "import plotly.graph_objects as go\n", "\n", "app = Dash(__name__,prevent_initial_callbacks=True)\n", "\n", "app.layout=html.Div(\n", " [\n", " dcc.Location(id='loc'),\n", " dcc.Graph(id='sunburst')\n", " ] +\n", " list(it.chain.from_iterable(\n", " [\n", " html.Label(f'Level {i}'),\n", " dcc.Dropdown(id=f'select-level-{i}'),\n", " html.P(id=f'msg-{i}',children='Selected: None')\n", " ]\n", " for i in range(5)\n", " ))\n", ")\n", "\n", "@app.callback(\n", " Output('sunburst','figure'),\n", " Output('select-level-0','options'),\n", " Output('select-level-0','value'),\n", " Input('loc','href')\n", ")\n", "def initial(url):\n", " print('initial ran')\n", " opts = df[df.parents.isna()].ids.unique()\n", " fig = go.Figure()\n", "\n", " fig.add_trace(go.Sunburst(\n", " ids=df.ids,\n", " labels=df.labels,\n", " parents=df.parents,\n", "\n", " ))\n", " return (fig,opts,opts[0])\n", "\n", "@app.callback(\n", " Output('msg-0','children'),\n", " Output('select-level-1','options'),\n", " Output('select-level-1','value'),\n", " Input('select-level-0','value')\n", ")\n", "def cb_level_0(value):\n", " opts = df[df.parents == value].ids.unique()\n", " return (f'Selected: {value}',opts,opts[0])\n", "\n", "@app.callback(\n", " Output('msg-1','children'),\n", " Output('select-level-2','options'),\n", " Output('select-level-2','value'),\n", " Input('select-level-1','value')\n", ")\n", "def cb_level_1(value):\n", " opts = df[df.parents == value].ids.unique()\n", " return (f'Selected: {value}',opts,opts[0])\n", "\n", "@app.callback(\n", " Output('msg-2','children'),\n", " Output('select-level-3','options'),\n", " Output('select-level-3','value'),\n", " Input('select-level-2','value')\n", ")\n", "def cb_level_2(value):\n", " opts = df[df.parents == value].ids.unique()\n", " return (f'Selected: {value}',opts,opts[0])\n", "\n", "@app.callback(\n", " Output('msg-3','children'),\n", " Output('select-level-4','options'),\n", " Output('select-level-4','value'),\n", " Input('select-level-3','value')\n", ")\n", "def cb_level_3(value):\n", " opts = df[df.parents == value].ids.unique()\n", " if len(opts) == 0:\n", " opts=['None']\n", " return (f'Selected: {value}',opts,opts[0])\n", " \n", "@app.callback(\n", " Output('msg-4','children'),\n", " Input('select-level-4','value')\n", ")\n", "def cb_level_4(value):\n", " return f'Selected: {value}'\n", "\n", "app.run_server(jupyter_mode='jupyterlab')" ] }, { "cell_type": "code", "execution_count": null, "id": "189bd9bb-3b92-4e8c-bcf1-6898fe023f59", "metadata": {}, "outputs": [], "source": [ "df[df.parents.isna()]" ] }, { "cell_type": "code", "execution_count": null, "id": "a8e4e7f3-f883-4063-bd89-6829bb196eef", "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.5" } }, "nbformat": 4, "nbformat_minor": 5 }