An introduction to the Dash web application framework. Dash is used to create browser-based interactive data visualization interfaces with Python. My goal is for you to understand how to use Dash. To open links below…
Video Layout:
00:00 - Introduction to Dash
01:57 - 3 pillars of Dash
05:17 - the Data
06:35 - the Code
08:38 - App Layout section
13:31 - the Callback
26:18 - Tutorial Challenge
28:35 - Plotly-Dash Community Forum
The Data:
https://drive.google.com/file/d/19xeIpigGi9ti51_GfJuRCb9GKv4nR0dB/view
Dash App Gallery:
https://dash-gallery.plotly.host/Portal/
Dash Components:
https://dash.plotly.com/dash-core-com…
Plotly Graphs:
https://plotly.com/python/
The Callback:
https://dash.plotly.com/basic-callbacks
Subscribe: https://www.youtube.com/@CharmingData/featured
Source Code:
import pandas as pd
import plotly.express as px # (version 4.7.0 or higher)
import plotly.graph_objects as go
from dash import Dash, dcc, html, Input, Output # pip install dash (version 2.0.0 or higher)
app = Dash(__name__)
# -- Import and clean data (importing csv into pandas)
# df = pd.read_csv("intro_bees.csv")
df = pd.read_csv("https://raw.githubusercontent.com/Coding-with-Adam/Dash-by-Plotly/master/Other/Dash_Introduction/intro_bees.csv")
df = df.groupby(['State', 'ANSI', 'Affected by', 'Year', 'state_code'])[['Pct of Colonies Impacted']].mean()
df.reset_index(inplace=True)
print(df[:5])
# ------------------------------------------------------------------------------
# App layout
app.layout = html.Div([
html.H1("Web Application Dashboards with Dash", style={'text-align': 'center'}),
dcc.Dropdown(id="slct_year",
options=[
{"label": "2015", "value": 2015},
{"label": "2016", "value": 2016},
{"label": "2017", "value": 2017},
{"label": "2018", "value": 2018}],
multi=False,
value=2015,
style={'width': "40%"}
),
html.Div(id='output_container', children=[]),
html.Br(),
dcc.Graph(id='my_bee_map', figure={})
])
# ------------------------------------------------------------------------------
# Connect the Plotly graphs with Dash Components
@app.callback(
[Output(component_id='output_container', component_property='children'),
Output(component_id='my_bee_map', component_property='figure')],
[Input(component_id='slct_year', component_property='value')]
)
def update_graph(option_slctd):
print(option_slctd)
print(type(option_slctd))
container = "The year chosen by user was: {}".format(option_slctd)
dff = df.copy()
dff = dff[dff["Year"] == option_slctd]
dff = dff[dff["Affected by"] == "Varroa_mites"]
# Plotly Express
fig = px.choropleth(
data_frame=dff,
locationmode='USA-states',
locations='state_code',
scope="usa",
color='Pct of Colonies Impacted',
hover_data=['State', 'Pct of Colonies Impacted'],
color_continuous_scale=px.colors.sequential.YlOrRd,
labels={'Pct of Colonies Impacted': '% of Bee Colonies'},
template='plotly_dark'
)
# Plotly Graph Objects (GO)
# fig = go.Figure(
# data=[go.Choropleth(
# locationmode='USA-states',
# locations=dff['state_code'],
# z=dff["Pct of Colonies Impacted"].astype(float),
# colorscale='Reds',
# )]
# )
#
# fig.update_layout(
# title_text="Bees Affected by Mites in the USA",
# title_xanchor="center",
# title_font=dict(size=24),
# title_x=0.5,
# geo=dict(scope='usa'),
# )
return container, fig
# ------------------------------------------------------------------------------
if __name__ == '__main__':
app.run_server(debug=True)
The Code:
https://github.com/Coding-with-Adam/Dash-by-Plotly/blob/master/Other/Dash_Introduction/intro.py
#python #data-science