Skip to content

Instantly share code, notes, and snippets.

@thuwarakeshm
Last active April 14, 2022 10:02
Show Gist options
  • Save thuwarakeshm/be9766f38c8e68616dc19553ceed64b1 to your computer and use it in GitHub Desktop.
Save thuwarakeshm/be9766f38c8e68616dc19553ceed64b1 to your computer and use it in GitHub Desktop.
Dash app
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash_html_components.Label import Label
from pandas.io.formats import style
import plotly.express as px
import pandas as pd
from dash.dependencies import Input, Output
app = dash.Dash(
__name__,
)
df = pd.read_csv(
"https://raw.githubusercontent.com/ThuwarakeshM/geting-started-with-plottly-dash/main/life_expectancy.csv"
)
colors = {"background": "#011833", "text": "#7FDBFF"}
app.layout = html.Div(
[
html.H1(
"My Dazzling Dashboard",
),
html.Div(
[
html.Div(
[
html.Label("Developing Status of the Country"),
dcc.Dropdown(
id="status-dropdown",
options=[
{"label": s, "value": s} for s in df.Status.unique()
],
className="dropdown",
),
]
),
html.Div(
[
html.Label("Average schooling years grater than"),
dcc.Dropdown(
id="schooling-dropdown",
options=[
{"label": y, "value": y}
for y in range(
int(df.Schooling.min()), int(df.Schooling.max()) + 1
)
],
className="dropdown",
),
]
),
],
className="row",
),
html.Div(dcc.Graph(id="life-exp-vs-gdp"), className="chart"),
dcc.Slider(
"year-slider",
min=df.Year.min(),
max=df.Year.max(),
step=None,
marks={year: str(year) for year in range(df.Year.min(), df.Year.max() + 1)},
value=df.Year.min(),
),
],
className="container",
)
@app.callback(
Output("life-exp-vs-gdp", "figure"),
Input("year-slider", "value"),
Input("status-dropdown", "value"),
Input("schooling-dropdown", "value"),
)
def update_figure(selected_year, country_status, schooling):
filtered_dataset = df[(df.Year == selected_year)]
if schooling:
filtered_dataset = filtered_dataset[filtered_dataset.Schooling <= schooling]
if country_status:
filtered_dataset = filtered_dataset[filtered_dataset.Status == country_status]
fig = px.scatter(
filtered_dataset,
x="GDP",
y="Life expectancy",
size="Population",
color="continent",
hover_name="Country",
log_x=True,
size_max=60,
)
fig.update_layout(
plot_bgcolor=colors["background"],
paper_bgcolor=colors["background"],
font_color=colors["text"],
)
return fig
if __name__ == "__main__":
app.run_server(debug=True)
@app.callback(
Output("life-exp-vs-gdp", "figure"),
Input("year-slider", "value"),
Input("status-dropdown", "value"),
Input("schooling-dropdown", "value"),
)
def update_figure(selected_year, country_status, schooling):
filtered_dataset = df[(df.Year == selected_year)]
if schooling:
filtered_dataset = filtered_dataset[filtered_dataset.Schooling <= schooling]
if country_status:
filtered_dataset = filtered_dataset[filtered_dataset.Status == country_status]
fig = px.scatter(
filtered_dataset,
x="GDP",
y="Life expectancy",
size="Population",
color="continent",
hover_name="Country",
log_x=True,
size_max=60,
)
return fig
app = dash.Dash(
__name__, external_stylesheets="https://codepen.io/chriddyp/pen/bWLwgP.css"
)
# Alternative way
app.css.append_css({
"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"
})
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd
app = dash.Dash()
df = pd.read_csv(
"https://raw.githubusercontent.com/ThuwarakeshM/geting-started-with-plottly-dash/main/life_expectancy.csv"
)
fig = px.scatter(
df,
x="GDP",
y="Life expectancy",
size="Population",
color="continent",
hover_name="Country",
log_x=True,
size_max=60,
)
app.layout = html.Div([dcc.Graph(id="life-exp-vs-gdp", figure=fig)])
if __name__ == "__main__":
app.run_server(debug=True)
html.H1("My Dazzling Dashboard", style={"color": "#011833"}),
app.layout = html.Div(
[
# Dropdown to filter developing/developed country.
html.Div(
[
dcc.Dropdown(
id="status-dropdown",
options=[{"label": s, "value": s} for s in df.Status.unique()], # Create available options from the dataset
),
]
),
# Dropdown to filter countries with average schooling years.
html.Div(
[
dcc.Dropdown(
id="schooling-dropdown",
options=[
{"label": y, "value": y}
for y in range(
int(df.Schooling.min()), int(df.Schooling.max()) + 1
)
], # add options from the dataset.
),
]
),
# Placeholder to render teh chart.
html.Div(dcc.Graph(id="life-exp-vs-gdp"), className="chart"),
# Slider to select year.
dcc.Slider(
"year-slider",
min=df.Year.min(), # dynamically select minimum and maximum years from the dataset.
max=df.Year.max(),
step=None,
marks={year: str(year) for year in range(df.Year.min(), df.Year.max() + 1)}, # set markers at one year interval.
value=df.Year.min(),
),
],
)
# - app.py
# - assets/
# |-- style.css
# style.css
# -----------------------------------------
# .title { color: #011833 }
# app.py
html.H1("My Dazzling Dashboard", className='title')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment