-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
164 lines (136 loc) · 5.43 KB
/
app.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# ---------------------------------- imports --------------------------------- #
# creates html elements
from layout import info_card, price_card, other_sliders_card, rating_collapse
# dash
from dash.dependencies import Input, Output, State
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
from dash import html, dcc
import dash
# others
import plotly.graph_objects as go
import numpy as np
# --------------------------------- load data -------------------------------- #
from load_data import with_reviews
# ------------------------------------- < ------------------------------------ #
# ---------------------------------- map fig --------------------------------- #
# needs with_reviews
# creates map figure
from map_fig import create_hovertemplate, create_trace, create_layout, create_fig
# create parts
my_fig = create_fig(create_trace(
with_reviews, create_hovertemplate()), create_layout(with_reviews))
# ------------------------------------- < ------------------------------------ #
# ------------------------------------ app ----------------------------------- #
# Create the Dash app
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
# -------------------------------- app layout -------------------------------- #
# app layout
app.layout = dbc.Container(
[
# Title and description
info_card,
# row
dbc.Row(
[
# col size 3
dbc.Col(
[
price_card,
rating_collapse,
other_sliders_card,
],
width=3,
),
# col size 9
dbc.Col(
# Map
[
dbc.Card(
[
dbc.CardHeader("Map"),
# map that fits inside the card
dbc.CardBody(
[
dcc.Graph(
id="map-graph",
figure=my_fig,
config={"scrollZoom": True},
style={
"height": "100%", "width": "100%", "display": "flex"},
)
]
),
],
style={"height": "100%"},
)
],
width=9,
),
],
),
],
)
# add callbacks to the map - filter data
@app.callback(
Output("map-graph", "figure"),
[
Input("avg_rating-slider", "value"),
Input("total_price-slider", "value"),
Input("person_capacity-slider", "value"),
Input("rating_accuracy-slider", "value"),
Input("rating_checkin-slider", "value"),
Input("rating_cleanliness-slider", "value"),
Input("rating_communication-slider", "value"),
Input("rating_location-slider", "value"),
Input("rating_value-slider", "value"),
Input("review_count-slider", "value"),
],
)
def update_graph(
avg_rating_value,
total_price_value,
person_capacity_value,
rating_accuracy_value,
rating_checkin_value,
rating_cleanliness_value,
rating_communication_value,
rating_location_value,
rating_value_value,
review_count_value,
):
# Filter the data
filtered_df = with_reviews[
(with_reviews["avg_rating"] >= avg_rating_value[0])
& (with_reviews["avg_rating"] <= avg_rating_value[1])
& (with_reviews["total_price"] >= total_price_value[0])
& (with_reviews["total_price"] <= total_price_value[1])
& (with_reviews["person_capacity"] >= person_capacity_value[0])
& (with_reviews["person_capacity"] <= person_capacity_value[1])
& (with_reviews["rating_accuracy"] >= rating_accuracy_value[0])
& (with_reviews["rating_accuracy"] <= rating_accuracy_value[1])
& (with_reviews["rating_checkin"] >= rating_checkin_value[0])
& (with_reviews["rating_checkin"] <= rating_checkin_value[1])
& (with_reviews["rating_cleanliness"] >= rating_cleanliness_value[0])
& (with_reviews["rating_cleanliness"] <= rating_cleanliness_value[1])
& (with_reviews["rating_communication"] >= rating_communication_value[0])
& (with_reviews["rating_communication"] <= rating_communication_value[1])
& (with_reviews["rating_location"] >= rating_location_value[0])
& (with_reviews["rating_location"] <= rating_location_value[1])
& (with_reviews["rating_value"] >= rating_value_value[0])
& (with_reviews["rating_value"] <= rating_value_value[1])
& (with_reviews["review_count"] >= review_count_value[0])
& (with_reviews["review_count"] <= review_count_value[1])
]
return create_fig(create_trace(
filtered_df, create_hovertemplate()), create_layout(filtered_df))
# callback for collapsible
@app.callback(
Output("collapse", "is_open"),
[Input("collapse-button", "n_clicks")],
[State("collapse", "is_open")],
)
def toggle_collapse(n, is_open):
return not is_open if n else is_open
# show app
app.run_server(use_reloader=False)