Skip to content

Instantly share code, notes, and snippets.

@MagedMohamedTurk
Forked from thuwarakeshm/app.py
Last active April 14, 2022 10:17
Show Gist options
  • Save MagedMohamedTurk/8eb973d6fb4b5102eec0f89bf4e712b8 to your computer and use it in GitHub Desktop.
Save MagedMohamedTurk/8eb973d6fb4b5102eec0f89bf4e712b8 to your computer and use it in GitHub Desktop.
Dash app
import dash
from dash import dcc, html
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(
id="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