{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "2554c072-00bc-4691-a5bc-d36031507d79", "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", " \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", "
countrycontinentyearlifeExppopgdpPercapiso_alphaiso_num
0AfghanistanAsia195228.8018425333779.445314AFG4
1AfghanistanAsia195730.3329240934820.853030AFG4
2AfghanistanAsia196231.99710267083853.100710AFG4
3AfghanistanAsia196734.02011537966836.197138AFG4
4AfghanistanAsia197236.08813079460739.981106AFG4
...........................
1699ZimbabweAfrica198762.3519216418706.157306ZWE716
1700ZimbabweAfrica199260.37710704340693.420786ZWE716
1701ZimbabweAfrica199746.80911404948792.449960ZWE716
1702ZimbabweAfrica200239.98911926563672.038623ZWE716
1703ZimbabweAfrica200743.48712311143469.709298ZWE716
\n", "

1704 rows × 8 columns

\n", "
" ], "text/plain": [ " country continent year lifeExp pop gdpPercap iso_alpha \\\n", "0 Afghanistan Asia 1952 28.801 8425333 779.445314 AFG \n", "1 Afghanistan Asia 1957 30.332 9240934 820.853030 AFG \n", "2 Afghanistan Asia 1962 31.997 10267083 853.100710 AFG \n", "3 Afghanistan Asia 1967 34.020 11537966 836.197138 AFG \n", "4 Afghanistan Asia 1972 36.088 13079460 739.981106 AFG \n", "... ... ... ... ... ... ... ... \n", "1699 Zimbabwe Africa 1987 62.351 9216418 706.157306 ZWE \n", "1700 Zimbabwe Africa 1992 60.377 10704340 693.420786 ZWE \n", "1701 Zimbabwe Africa 1997 46.809 11404948 792.449960 ZWE \n", "1702 Zimbabwe Africa 2002 39.989 11926563 672.038623 ZWE \n", "1703 Zimbabwe Africa 2007 43.487 12311143 469.709298 ZWE \n", "\n", " iso_num \n", "0 4 \n", "1 4 \n", "2 4 \n", "3 4 \n", "4 4 \n", "... ... \n", "1699 716 \n", "1700 716 \n", "1701 716 \n", "1702 716 \n", "1703 716 \n", "\n", "[1704 rows x 8 columns]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from dash import Dash,html, ctx, dcc, dash_table, Input, Output, State\n", "from dash.dash_table.Format import Format, Scheme\n", "import dash_daq as daq\n", "import plotly.express as px\n", "import pandas as pd\n", "\n", "app = Dash(__name__,prevent_initial_callbacks=True)\n", "\n", "df=px.data.gapminder()\n", "df" ] }, { "cell_type": "code", "execution_count": 2, "id": "03b5ec88-9d35-4029-9e64-9f6f34612c49", "metadata": { "tags": [] }, "outputs": [], "source": [ "app.layout=html.Div([\n", " html.H2('Callback Example'),\n", " html.Label('Select a Continent:'),\n", " dcc.Dropdown(\n", " df.continent.unique(),\n", " id = 'select-continent',\n", " ),\n", " dcc.Graph(id='continent-graph'),\n", " dcc.Input(id='message',type='text',value='blah'),\n", " html.H3('Select Countries to Compare'),\n", " html.Label('Select First Country:'),\n", " dcc.Dropdown(id='select-country1'),\n", " html.Label('Select Second Country:'),\n", " dcc.Dropdown(id='select-country2'),\n", " html.Button('Compare',id='compare-button'),\n", " dcc.Graph(id='country-graph'),\n", "])\n", "\n", "@app.callback(\n", " Output('continent-graph','figure'),\n", " Output('select-country1','options'),\n", " Output('select-country2','options'),\n", " Input('select-continent','value')\n", ")\n", "def continent_selected(continent):\n", " data = df[df.continent == continent].groupby('year').mean(numeric_only=True).reset_index()\n", " fig = px.line(data,x='year',y='lifeExp',title=f'Average Life Expectancy in {continent}')\n", " countries = df[df.continent == continent].country.unique()\n", " return (fig,countries,countries)\n", "\n", "@app.callback(\n", " Output('country-graph','figure'),\n", " Input('compare-button','n_clicks'),\n", " State('select-country1','value'),\n", " State('select-country2','value')\n", ")\n", "def compare(button,c1,c2):\n", " subset = df[df.country.isin([c1,c2])]\n", " return px.line(\n", " subset,\n", " x='year',\n", " y='lifeExp',\n", " color='country',\n", " title=f'Life Expectancy for {c1} vs. {c2}'\n", " )\n", "\n", "app.run_server(jupyter_mode='jupyterlab')" ] }, { "cell_type": "code", "execution_count": null, "id": "ffd1a738-fb71-4822-a330-e91c6439de3d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "c0b2ed0f-ec79-4345-a06e-b9e3d6b7c37a", "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 }