forked from austinlasseter/flying-dog-beers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
266 lines (219 loc) · 9.31 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import pandas as pd
import numpy as np
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import dash_table
import dash_bootstrap_components as dbc
from urllib.request import urlopen
from dash.dependencies import Input, Output, State
import plotly.express as px
import json
import mexico_content
# this was added by David
df= px.data.gapminder().query('year==2007') # this is plotted, it was 2007
# the style arguments for the sidebar
SIDEBAR_STYLE = {
'position': 'fixed',
'top': 0,
'left': 0,
'bottom': 0,
'width': '20%', # this sizes the left hand side of the board to 20%
'padding': '20px 10px',
'background-color': '#f8f9fa' # this means a light gray
}
# the style arguments for the main content page.
CONTENT_STYLE = {
'margin-left': '25%', # this was 25%
'margin-right': '5%', #this was 5%, by controlling the margins, you control the border
'padding': '20px 10px' # this was 20px 10p The CSS padding properties are used to generate space around an element's content, inside of any defined borders.
# i am not sure what this refers to
}
TEXT_STYLE = {
'textAlign': 'center',
'color': '#191970' # Midnight blue
}
CARD_TEXT_STYLE = {
'textAlign': 'center',
'color': '#0074D9' #Pure (or mostly pure) blue.
}
sidebar = html.Div(
[
html.H2("Menu", className="display-4"),
html.Hr(),
html.P(
"Choose one of the following features", className="lead"
),
dbc.Nav(
[
dbc.NavLink("Search Engine", href="/page-1", id="page-1-link"),
dbc.NavLink("Highlight text", href="/page-2", id="page-2-link"),
dbc.NavLink("Incentives by country", href="/page-3", id="page-3-link"),
],
vertical=True,
pills=True,
),
],
style=SIDEBAR_STYLE,
)
content_first_row = dbc.Row(dbc.Col(html.Div(dcc.Input(id="input-1",placeholder= "Search for the type of policy that you are interested in",style={"width":"100%"})))) #this was 100%, this with controls only the with of the search window
content_second_row = dbc.Row(
[
dbc.Col(
dcc.Graph(id='graph_1'), md=4 #md was 4, i dont know what this is
),
dbc.Col(
dcc.Graph(id='graph_2'), md=4 #md was 4,
),
dbc.Col(
dcc.Graph(id='graph_3'), md=4 #md was 4,
)
]
)
content_third_row = dbc.Row( #this one is active!!!!!!!!!!!
[
dbc.Col(
dcc.Graph(id='graph_4'), md=12,
)
]
)
content_fourth_row = dbc.Row( #this one is not active
[
dbc.Col(
dcc.Graph(id='graph_5'), md=6 #md (, optional): Specify column behaviour on a medium screen. Valid arguments are boolean, an integer in the range 1-12 inclusive, or a dictionary with keys 'offset', 'order', 'size'. See the documentation for more details.
),
dbc.Col(
dcc.Graph(id='graph_6'), md=6
)
]
)
content_fifth_row = dbc.Row( #this one is an experiment, created by David !!!!!!!!!!!
[
dbc.Col(
dash_table.DataTable(id='table_1',
columns=[
{'name': i, 'id': i, 'deletable': False} for i in sorted(df.columns) # it was 'deletable': True
],
page_current=0,
page_size=10, # it was PAGE_SIZE,
page_action='custom', # it was custom
sort_action='custom', #these two have to be 'custom' so that you can manipuate it
sort_mode='single',
sort_by=[]
), md=12,
)
]
)
###Search by region collapse
collapse = html.Div(
[
dbc.Button(
"Submit", # it was México D.F
id="collapse-button",
className="mb-3", #m - for classes that set margin, b - for classes that set margin-bottom or padding-bottom, 3 - (by default) for classes that set the margin or padding to $spacer
color="primary", #this means some kind of blue color
),
dbc.Collapse(
[dbc.Card(dbc.CardBody("Document 1")),dbc.Card(dbc.CardBody("Document 2")),dbc.Card(dbc.CardBody("Document 3"))],
id="collapse",
)
]
)
#Content element
content = html.Div(id="page-content", style=CONTENT_STYLE)
page_1_content = html.Div(
[
html.H2('Economic incentive search for forest land restoration', style=TEXT_STYLE),
html.Hr(), #break
content_first_row, # placeholder= "Search for the type of policy that you are interested in"
html.Hr(), # break
content_third_row,
html.Div(html.H1("Best match")),
html.Div(html.H5("""Some text regarding user's query.""")),
content_fifth_row, # this is experiment added by David!!!!
html.Hr(), # break, this was modified by David
collapse
#content_second_row,
#content_fourth_row
],
style=CONTENT_STYLE
)
app = dash.Dash(__name__,external_stylesheets=[dbc.themes.BOOTSTRAP])
server = app.server
app.layout = html.Div([dcc.Location(id="url"),sidebar, content]) #this is the key of the division!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
# this callback uses the current pathname to set the active state of the
# corresponding nav link to true, allowing users to tell see page they are on
@app.callback(
[Output(f"page-{i}-link", "active") for i in range(1, 4)],
[Input("url", "pathname")],
)
def toggle_active_links(pathname):
if pathname == "/":
# Treat page 1 as the homepage / index
return True, False, False
return [pathname == f"/page-{i}" for i in range(1, 4)]
## URL callback
@app.callback(Output("page-content", "children"), [Input("url", "pathname")])
def render_page_content(pathname):
if pathname in ["/", "/page-1"]:
return page_1_content
elif pathname == "/page-2":
return html.Div("En construcción")
elif pathname == "/page-3":
return mexico_content.content
# If the user tries to reach a different page, return a 404 message
else:
return dbc.Jumbotron(
[
html.H1("404: Not found", className="text-danger"),
html.Hr(),
html.P(f"The pathname {pathname} was not recognised..."),
]
)
if __name__ == '__main__':
app.run_server()
@app.callback(
Output('graph_4', 'figure'),
[Input('submit_button', 'n_clicks')],
[State('dropdown', 'value'), State('range_slider', 'value'), State('check_list', 'value'),
State('radio_items', 'value')
])
def update_graph_4(n_clicks, dropdown_value, range_slider_value, check_list_value,
radio_items_value): # this figure is being called
print(n_clicks)
print(dropdown_value)
print(range_slider_value)
print(check_list_value)
print(radio_items_value) # Sample data and figure
with urlopen(
'https://raw.githubusercontent.com/davidlararamos/mexico_geojson/master/mx_states.json') as response:
mexican_states = json.load(response)
state_id_map = {}
for feature in mexican_states['features']: # we are creating new keys for our dictionary
feature['id'] = feature['properties']['id']
# feature['state_name'] = feature['properties']['state_name']
state_id_map[feature['properties']['state_name']] = feature['id']
url = 'https://es.wikipedia.org/wiki/Anexo:Entidades_federativas_de_M%C3%A9xico_por_PIB'
df = pd.read_html(url)
df_gdp_state = df[1]
df_gdp_state_inter = df_gdp_state[['Estado', '2015p']]
df_gdp_state_inter['Estado'] = df_gdp_state_inter['Estado'].replace(
['Ciudad de México', 'Estado de México', '50x36px Veracruz'], ['Distrito Federal', 'México', 'Veracruz'])
df_gdp_state_corrected = df_gdp_state_inter.rename(columns={"Estado": "State", "2015p": "GDP"})
df_gdp_state_corrected_final = df_gdp_state_corrected.drop(df_gdp_state_corrected.index[0])
df_gdp_state_corrected_final['id'] = df_gdp_state_corrected_final['State'].apply(lambda x: state_id_map[x])
df_gdp_state_corrected_final['GDP_nospace'] = ['2312562', '1230628', '1041797', '889703', '676899', '591414',
'570172', '464771', '401342', '435028', '400916', '409317', '433857',
'423215', '318401', '314979', '290580', '271982', '227598', '224237',
'223435', '213783', '203685', '196744', '169885', '161902', '159797',
'131983', '103406', '92149', '81672', '75578']
df_gdp_state_corrected_final['GDP_int'] = df_gdp_state_corrected_final['GDP_nospace'].astype(int)
df_gdp_state_corrected_final['GDP_int_scale'] = np.log10(df_gdp_state_corrected_final['GDP_int'])
fig = px.choropleth(df_gdp_state_corrected_final, locations='id', geojson=mexican_states, scope='north america',color='GDP_int',
hover_name='State')
fig.update_geos(fitbounds='locations', visible=False)
fig.update_layout({
'height': 600
})
return fig