-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample5.py
More file actions
85 lines (69 loc) · 1.91 KB
/
example5.py
File metadata and controls
85 lines (69 loc) · 1.91 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import pandas as pd
from dash import dcc, html, Dash
from dash.dependencies import Input, Output
import plotly.graph_objects as go
import plotly.express as px
df = pd.read_csv("bike-accidents.csv")
df["HORA"] = df["HORA"].map(lambda time: int(time.split(":")[0]))
app = Dash(__name__)
districts = [{"label": district, "value": district} for district in df["DISTRITO"].unique()]
app.layout = html.Div(children = [
html.H1("Bicimad accidents"),
dcc.Dropdown(
id="district",
options=districts,
value=districts[0]["value"]
),
html.H2("By time of day"),
dcc.Graph(
id="by-time",
figure={
"data": [],
"layout": {
"title": "By time"
}
}
),
html.H2("By genre"),
dcc.Graph(
id="by-genre",
figure={
"data": [],
"layout": {
"title": "By genre"
}
}
)
])
xs = list(sorted(df["HORA"].unique()))
@app.callback(
Output(component_id="by-time",component_property="figure"),
[Input(component_id="district", component_property="value")]
)
def update_by_time(district):
my_df = df[df["DISTRITO"] == district]
hours = my_df["HORA"].value_counts().to_dict()
values = []
for hour in range(0, 24):
if hour in hours:
values.append(hours[hour])
else:
values.append(0)
figure = go.Figure(
data = [
go.Scatter(
x = list(range(0, 24)),
y = values
)
]
)
return figure
@app.callback(
Output(component_id="by-genre",component_property="figure"),
[Input(component_id="district", component_property="value")]
)
def update_by_genre_and_role(district):
my_df = df[df["DISTRITO"] == district]
figure = px.bar(my_df, x="SEXO", y="TIPO PERSONA", color="TIPO PERSONA")
return figure
app.run_server()