-
Notifications
You must be signed in to change notification settings - Fork 1
/
app03.py
64 lines (51 loc) · 1.65 KB
/
app03.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Example of correctly filtering titanic dataset."""
from pathlib import Path
from typing import Iterable, Mapping
import dash
import dash_core_components as dcc
from dash.dependencies import Input, Output
import dash_html_components as html
import dash_table
import pandas as pd
# create the app
app = dash.Dash(__name__)
# load out initial dataset
titanic_path = Path("./datasets/titanic.csv")
assert titanic_path.exists(), "Cannot find titanic dataset."
df = pd.read_csv(titanic_path)
app.layout = html.Div(
children=[
html.H1("Titanic Dataset"),
html.H5("Filter by sex:"),
dcc.Dropdown(
id="my-dropdown",
options=[{"label": "All", "value": "both"}]
+ [{"label": sex, "value": sex} for sex in df.Sex.unique()],
value="both",
),
html.Div(id="my-div"),
dash_table.DataTable(
id="my-table",
columns=[{"name": i, "id": i} for i in df.columns],
data=[],
),
]
)
@app.callback(
Output(component_id="my-table", component_property="data"),
[Input(component_id="my-dropdown", component_property="value")],
)
def provide_passengers(sex: str) -> Iterable[Mapping]:
if sex == "both":
return df.to_dict("rows")
return df[df.Sex == sex].to_dict("rows")
@app.callback(
Output(component_id="my-div", component_property="children"),
[Input(component_id="my-dropdown", component_property="value")],
)
def update_output_div(sex: str) -> str:
if sex == "both":
return "Showing all sexes."
return f"Showing all {sex}s."
if __name__ == "__main__":
app.run_server(debug=True, host="0.0.0.0")