forked from prateekj117/sih-2019
-
Notifications
You must be signed in to change notification settings - Fork 3
/
agg_national_accounts.py
126 lines (107 loc) · 3.48 KB
/
agg_national_accounts.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
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from collections import OrderedDict
import dash_table
from utils import get_excel
import pandas as pd
from app import app
import math
filename = get_excel(
"aggregate_national_accounts", "data/2018/economic-aggregates/S1.1.xlsx"
)
data = pd.read_excel(filename)
main_sections = data.iloc[5:-1, -2]
main_index = [
index
for index in main_sections.index
if (type(main_sections[index]) != str and math.isnan(main_sections[index]))
]
section_rows = [data.iloc[idx] for idx in main_index]
labels = [row.iloc[-1] for row in section_rows]
years = data.iloc[5:6, 2:-2]
year_set = list(OrderedDict.fromkeys(years.values[0]).keys())
# process = data[7:]
# sections = process.iloc[:, 0]
# main_sections = [index for index in sections.index if str(sections[index]).isdigit()]
# rows = [data.iloc[idx] for idx in main_sections]
# labels = data[8:14].iloc[:, -1]
# labelIds = labels.index
# label_set = list(OrderedDict.fromkeys(labels.values).keys())
def app_layout():
return (
html.Div([html.H2("Aggregated National Accounts")]),
html.Div(
[
dcc.Dropdown(
id="tabs",
options=[
{"label": label, "value": idx}
for (idx, label) in enumerate(labels)
],
placeholder="Select a category",
value=0,
),
html.Div(id="tab-container"),
dcc.Graph(id="ana-graph-0"),
generate_table(data),
],
className="container",
),
)
def generate_table(dataframe, max_rows=10):
data = pd.read_excel(filename, header=None)
df = data[6:-1]
df.columns = df.iloc[0].fillna(value=pd.Series(range(100)))
return dash_table.DataTable(
data=df.to_dict("rows"),
columns=[{"id": c, "name": c} for c in df.columns],
style_table={
"height": "400px",
"overflowY": "scroll",
"border": "thin lightgrey solid",
},
)
layout = app_layout()
@app.callback(Output("tab-container", "children"), [Input("tabs", "value")])
def dropdown(value):
value = int(value)
low_limit = main_index[value]
high_limit = (
len(data) - 1 if (value + 1) >= len(main_index) else main_index[value + 1]
)
sub_index = range(low_limit + 1, high_limit)
sub_section_rows = [data.iloc[idx] for idx in sub_index]
sub_labels = [row.iloc[-1] for row in sub_section_rows]
return dcc.Dropdown(
id="tabs2",
options=[
{"label": label, "value": sub_index[idx]}
for (idx, label) in enumerate(sub_labels)
],
value=sub_index[0],
)
@app.callback(Output("ana-graph-0", "figure"), [Input("tabs2", "value")])
def display_graph(value):
year_list = ["Y " + year for year in year_set]
filtered = data.iloc[value, 2:-2]
data_cu = {
"x": year_list,
"y": filtered[:6],
"name": "Current Price",
"line": {"width": 3, "shape": "spline"},
}
data_co = {
"x": year_list,
"y": filtered[6:],
"name": "Constant Price",
"line": {"width": 3, "shape": "spline"},
}
return {
"data": [data_cu, data_co],
"layout": {
"margin": {"l": 30, "r": 0, "b": 30, "t": 0},
"name": "Current Price",
},
}