Last active
January 9, 2021 17:26
-
-
Save Caellwyn/f378b4b6cf7b6d3d4306129536bedf9e to your computer and use it in GitHub Desktop.
dash-example.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "dash-example.ipynb", | |
"private_outputs": true, | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyNv5kgYOj/yrQuVhmWAnAsz", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/Caellwyn/f378b4b6cf7b6d3d4306129536bedf9e/dash-example.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "qfLxr9yruVkG" | |
}, | |
"source": [ | |
"# pip install dash\r\n", | |
"import dash\r\n", | |
"import dash_core_components as dcc\r\n", | |
"import dash_html_components as html\r\n", | |
"from plotly import graph_objects as go\r\n", | |
"import pandas as pd\r\n", | |
"from src import *\r\n", | |
"\r\n", | |
"# get data\r\n", | |
"full_df = get_covid_data()\r\n", | |
"unique_countries = sorted(full_df['CountryName'].unique())\r\n", | |
"\r\n", | |
"# set initial app settings\r\n", | |
"external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']\r\n", | |
"app = dash.Dash(__name__, external_stylesheets=external_stylesheets)\r\n", | |
"server = app.server\r\n", | |
"\r\n", | |
"# display layout and components\r\n", | |
"app.layout = html.Div([\r\n", | |
" html.H2('COVID-19 Cumulative Deaths or Positive Cases'),\r\n", | |
" html.H4('To Display'),\r\n", | |
" html.Div(id='stat-value'),\r\n", | |
" dcc.Checklist(\r\n", | |
" id='checklist-stat',\r\n", | |
" options=[{'label':i,'value':i} for i in \r\n", | |
" ['Cumulative Cases','Cumulative Deaths', 'Daily Cases', 'Daily Deaths']]\r\n", | |
" ),\r\n", | |
" html.H4('Country'),\r\n", | |
" html.Div(id='country-value'),\r\n", | |
" dcc.Dropdown(\r\n", | |
" id='dropdown-country',\r\n", | |
" options=[{'label': i, 'value': i} for i in unique_countries],\r\n", | |
" value='None'\r\n", | |
" ),\r\n", | |
" html.H4('State'),\r\n", | |
" html.Div(id='state-value'),\r\n", | |
" dcc.Dropdown(id = 'dropdown-state',\r\n", | |
" options = [{'label':'None', 'value':'None'}],\r\n", | |
" value = 'None'\r\n", | |
" ),\r\n", | |
" html.H4('Graph'),\r\n", | |
" html.Div(id='graph')\r\n", | |
"])\r\n", | |
"\r\n", | |
"# set state options according to chosen country\r\n", | |
"@app.callback([dash.dependencies.Output('dropdown-state', 'options'),\r\n", | |
" dash.dependencies.Output('dropdown-state', 'value')],\r\n", | |
" [dash.dependencies.Input('dropdown-country', 'value')])\r\n", | |
"def add_states(country_value, df=full_df):\r\n", | |
" country_df = full_df.loc[full_df['CountryName'] == country_value]\r\n", | |
" state_df = country_df.loc[country_df['Jurisdiction'] \r\n", | |
" == 'STATE_TOTAL'].dropna()\r\n", | |
" global country\r\n", | |
" country = country_value\r\n", | |
" # If there is state information available for the country, update the\r\n", | |
" # state-dropdown options with those states and set the current value to None\r\n", | |
" # If not, remove the state options and set the value to None\r\n", | |
" if len(state_df) > 0:\r\n", | |
" return [{'label':'None','value':'None'}] + [{'label':i,'value':i} \\\r\n", | |
" for i in sorted(state_df['RegionName'].dropna().unique())], \r\n", | |
" 'None'\r\n", | |
" else:\r\n", | |
" return [{'label':'None', 'value':'None'}], 'None'\r\n", | |
"\r\n", | |
"\r\n", | |
"#create graph using user assigned values\r\n", | |
"@app.callback(dash.dependencies.Output('graph', 'children'),\r\n", | |
" [dash.dependencies.Input('dropdown-state','value'),\r\n", | |
" dash.dependencies.Input('dropdown-country','value'),\r\n", | |
" dash.dependencies.Input('checklist-stat','value')])\r\n", | |
"def display_value(state, country, stats, df=full_df):\r\n", | |
" if country != 'None':\r\n", | |
" return dcc.Graph(id='stat-graph',\r\n", | |
" figure = graph_stat(df, state=state,\r\n", | |
" country=country,\r\n", | |
" stats=stats))\r\n", | |
"\r\n", | |
"if __name__ == '__main__':\r\n", | |
" app.run_server(debug=False)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "P9fe-AOeyrVH" | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment