-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallbacks_sankey.py
246 lines (206 loc) · 8.67 KB
/
callbacks_sankey.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
from dash.dependencies import Input, Output, ALL, State, MATCH, ALLSMALLER
from app import app
import EnergyFlows
import figures
import pandas as pd
from dash import html
from dash import dcc
Country_List = ['Samoa','Nauru','Vanuatu','Palau','Kiribati','Cook Islands','Solomon Islands','Tonga','New Caledonia','French Polynesia','Micronesia','Niue','Tuvalu','PNG','Fiji']
@app.callback(
[Output('Sankey_figure', 'figure'),
Output('Sankey_elec_figure', 'figure')],
[Input("select-year", "value"),
Input("select-country", "value"),
]
)
def sensor_checklist(year,country):
return figures.Generate_Sankey(year,country)[0],figures.Generate_Sankey(year,country)[1]
@app.callback(
[Output('select-to', 'options'),
Output('select-to', 'value')
],
Input("select-from", "value"),
)
def update_options3(from_):
items = []
df = pd.read_csv("Data/Sankey/csv/{}/{}.csv".format(2019, 'PNG'))
df = df[df[' (from)'] == from_]
for i in df[' (to)']:
items.append(i)
product_list = set(items)
options = [{"label": i, "value": i} for i in product_list]
return options,options[0]['label']
@app.callback(
[
Output('Hidden-Div_trend', "children"),
Output('dynamic_callback_container', 'children')],
[Input('update-button-cross-country-figure', 'n_clicks'),
Input('update-button-sankey-clear-canvas', 'n_clicks')],
[State("select-from", "value"),
State("select-to", "value"),
State("radio-normalization-sankey", "value"),
State("export-df-sankey-cross-country", "value"),
State('Hidden-Div_trend', "children"),
State('dynamic_callback_container', 'children')
]
)
def update_cross_country_comparison(n_clicks,clear_canvas,from_,to_,normalization,export_df,hidden_div,div_children):
import re
if n_clicks != hidden_div[0]:
values = []
normalized_values = []
df_cross_country = pd.DataFrame()
for country in Country_List:
df1 = pd.read_csv("Data/Sankey/csv/{}/{}.csv".format(2019, country))
df = df1[(df1[' (from)'] == from_)&(df1[' (to)']==to_)].reset_index()
a = df[' (weight)']
if normalization == ' (from)':
denominator_df = df1[df1[normalization]==from_]
denominator = denominator_df[' (weight)'].sum()
elif normalization == ' (to)':
denominator_df = df1[df1[normalization]==to_]
denominator = denominator_df[' (weight)'].sum()
elif normalization == 1:
denominator = 1
if len(a) == 0:
a = 0
else:
a=a[0]
if denominator == 0:
normalized_value = 0
elif denominator > 0:
normalized_value = 100*a/denominator
normalized_values.append(round(normalized_value,1))
values.append(a)
df_cross_country['Country'] = Country_List
df_cross_country['Values'] = values
if normalization != 1:
df_cross_country['Values'] = normalized_values
fig = figures.cross_country_sankey(df_cross_country,from_,to_,normalization)
new_child = html.Div(
style={'display': 'inline-block', 'outline': 'thin lightgrey solid', 'padding': 10},
children=[
dcc.Graph(
id={
'type': 'dynamic-graph',
'index': n_clicks
},
figure=fig,
),
]
)
div_children.append(new_child)
if export_df !=[]:
# from_ = re.sub("\s\s+" , " ", from_)
# to_ = re.sub("\s\s+" , " ", to_)
# df_cross_country.to_csv("Results/cross_country/cross_country_sankey.csv".format(from_.replace(" ", "_"),
# to_.replace(" ", "_"),
# str(normalization).replace(" ", "_")))
pass # This is disabled for the online version. This feature is used for generating reports
elif clear_canvas != hidden_div[1]:
div_children = []
hidden_div = [n_clicks,clear_canvas]
return hidden_div,div_children
# relative comparison of different rows
@app.callback(
[
Output('Hidden_Div_breakdown', "children"),
Output('dynamic_callback_container_energy_breakdown', 'children')],
[Input('update-button-cross-country-flow', 'n_clicks'),
Input('update-button-flow-clear', 'n_clicks')],
[State("select-flow-provider", "value"),
State("select-flow-cunsumer", "value"),
State("select-flow-provider-source", "value"),
State("destination-carrier", "value"),
State('Hidden_Div_breakdown', "children"),
State('dynamic_callback_container_energy_breakdown', 'children'),
State("y_axis_title","value")
]
)
def update_cross_country_comparison(n_clicks,clear_canvas,from_,consumer_list,carrier,carrier_destination,hidden_div,div_children,y_axis_title):
if n_clicks != hidden_div[0]:
fig = figures.dynamic_breakdown_figure_generation(y_axis_title=y_axis_title,from_=from_,list_of_consumers=consumer_list,carrier=carrier,destination_carrier=carrier_destination)
new_child = html.Div(
style={ 'outline': 'thin lightgrey solid', 'padding': 5,'marginLeft': 10, 'marginRight': 10,},#'display': 'inline-block',
children=[
dcc.Graph(
id={
'type': 'dynamic-graph',
'index': n_clicks
},
figure=fig,
),
]
)
div_children.append(new_child)
if figures.mode == 'report':
fig.write_image("Results/high_res_figs/from{}_to_{}.png".format(from_,consumer_list[0]), scale=7,width=1500)
elif clear_canvas != hidden_div[1]:
div_children = []
hidden_div = [n_clicks,clear_canvas]
return hidden_div,div_children
# Breakdown of one row
@app.callback(
[
Output('Hidden_Div_breakdown_by_source', "children"),
Output('dynamic_callback_container_energy_breakdown_by_source', 'children')],
[Input('update-button-sector-breakdown', 'n_clicks'),
Input('update-button-sector-breakdown-clear', 'n_clicks')],
[State("select-row-for-breakdown", "value"),
State('Hidden_Div_breakdown_by_source', "children"),
State('dynamic_callback_container_energy_breakdown_by_source', 'children'),
]
)
def update_cross_country_comparison(n_clicks,clear_canvas,row,hidden_div,div_children):
if n_clicks != hidden_div[0]:
fig = figures.dynamic_breakdown_of_one_row(row=row)
new_child = html.Div(
style={ 'outline': 'thin lightgrey solid', 'padding': 5,'marginLeft': 10, 'marginRight': 10,},#'display': 'inline-block',
children=[
dcc.Graph(
id={
'type': 'dynamic-graph',
'index': n_clicks
},
figure=fig,
),
]
)
div_children.append(new_child)
elif clear_canvas != hidden_div[1]:
div_children = []
hidden_div = [n_clicks,clear_canvas]
return hidden_div,div_children
@app.callback(
[
Output('Hidden-Div_dynamic_column', "children"),
Output('dynamic_callback_container_dynamic_column', 'children')],
[Input('update-button-dynamic-column', 'n_clicks'),
Input('update-button-dynamic-column-clear', 'n_clicks')],
[State("select-rows-for-column-comparison", "value"),
State("select-dynamic-column", "value"),
State("y_axis_title_dynamic_column", "value"),
State('Hidden-Div_dynamic_column', "children"),
State('dynamic_callback_container_dynamic_column', 'children'),
]
)
def update_dynamic_column(n_clicks,clear_canvas,provider,column,y_axis_title,hidden_div,div_children):
if n_clicks != hidden_div[0]:
fig = figures.dynamic_one_column_multiple_source(provider=provider,column=column,y_axis_title=y_axis_title)
new_child = html.Div(
style={ 'outline': 'thin lightgrey solid', 'padding': 5,'marginLeft': 10, 'marginRight': 10,},#'display': 'inline-block',
children=[
dcc.Graph(
id={
'type': 'dynamic-graph',
'index': n_clicks
},
figure=fig,
),
]
)
div_children.append(new_child)
elif clear_canvas != hidden_div[1]:
div_children = []
hidden_div = [n_clicks,clear_canvas]
return hidden_div,div_children