diff --git a/app.py b/app.py index d981c1d..7a37cb1 100644 --- a/app.py +++ b/app.py @@ -3,23 +3,25 @@ import dash_core_components as dcc import dash_html_components as html -import flask +import flask from flask import render_template import pandas as pd import time import os -server = flask.Flask('app') -server.secret_key = os.environ.get('secret_key', 'secret') +server = flask.Flask("app") +server.secret_key = os.environ.get("secret_key", "secret") -@server.route('/') + +@server.route("/") def index(): - return render_template('index.html') + return render_template("index.html") + -external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] +external_stylesheets = ["https://codepen.io/chriddyp/pen/bWLwgP.css"] -app = dash.Dash('app', server=server, external_stylesheets=external_stylesheets) +app = dash.Dash("app", server=server, external_stylesheets=external_stylesheets) app.scripts.config.serve_locally = False app.config.suppress_callback_exceptions = True -dcc._js_dist[0]['external_url'] = 'https://cdn.plot.ly/plotly-basic-latest.min.js' +dcc._js_dist[0]["external_url"] = "https://cdn.plot.ly/plotly-basic-latest.min.js" diff --git a/apps/admin.py b/apps/admin.py index 36dddf5..3a5d475 100644 --- a/apps/admin.py +++ b/apps/admin.py @@ -6,14 +6,18 @@ def check_auth(username, password): """This function is called to check if a username / password combination is valid. """ - return username == 'admin' and password == 'secret' + return username == "admin" and password == "secret" + def authenticate(): """Sends a 401 response that enables basic auth""" return Response( - 'Could not verify your access level for that URL.\n' - 'You have to login with proper credentials', 401, - {'WWW-Authenticate': 'Basic realm="Login Required"'}) + "Could not verify your access level for that URL.\n" + "You have to login with proper credentials", + 401, + {"WWW-Authenticate": 'Basic realm="Login Required"'}, + ) + def requires_auth(f): @wraps(f) @@ -22,4 +26,5 @@ def decorated(*args, **kwargs): if not auth or not check_auth(auth.username, auth.password): return authenticate() return f(*args, **kwargs) - return decorated \ No newline at end of file + + return decorated diff --git a/apps/agg_eco_activities.py b/apps/agg_eco_activities.py index 0cb73e1..10a88be 100644 --- a/apps/agg_eco_activities.py +++ b/apps/agg_eco_activities.py @@ -12,93 +12,106 @@ from app import app -filename = get_excel('aggregate_economic_activities', 'data/2018/aggregates-economic-activity/S7.1.xlsx') +filename = get_excel( + "aggregate_economic_activities", "data/2018/aggregates-economic-activity/S7.1.xlsx" +) data = pd.read_excel(filename) years = data.iloc[2:3, 2:-2] -year_set = [year for year in list(OrderedDict.fromkeys(years.values[0]).keys()) if type(year) == str] +year_set = [ + year + for year in list(OrderedDict.fromkeys(years.values[0]).keys()) + if type(year) == str +] process = data[5:] headers = data.iloc[4][2:-2] header_set = list(OrderedDict.fromkeys(headers.values).keys()) sections = process.iloc[:, 0] -main_sections = [index for index in sections.index if str(sections[index]).isdigit() or (type(sections[index]) != str and math.isnan(sections[index]))] +main_sections = [ + index + for index in sections.index + if str(sections[index]).isdigit() + or (type(sections[index]) != str and math.isnan(sections[index])) +] section_rows = [data.iloc[idx] for idx in main_sections] labels = [row.iloc[-1] for row in section_rows] labelIds = main_sections def app_layout(): - children = [dcc.Tab(label=label, value=labelIds[idx]) for (idx, label) in enumerate(labels)] + children = [ + dcc.Tab(label=label, value=labelIds[idx]) for (idx, label) in enumerate(labels) + ] return ( - html.Div([ - html.H2('Aggregated Economic Activities') - ]), - html.Div([ - dcc.Dropdown( - id='tabs', - options=[{'label': label, 'value': labelIds[idx]} for (idx, label) in enumerate(labels)], - placeholder="Select a category", - value=labelIds[-1] - ), - dcc.Graph(id='agc-graph'), - generate_table(data) - ], className="container") + html.Div([html.H2("Aggregated Economic Activities")]), + html.Div( + [ + dcc.Dropdown( + id="tabs", + options=[ + {"label": label, "value": labelIds[idx]} + for (idx, label) in enumerate(labels) + ], + placeholder="Select a category", + value=labelIds[-1], + ), + dcc.Graph(id="agc-graph"), + generate_table(data), + ], + className="container", + ), ) def generate_table(dataframe, max_rows=10): - data = pd.read_excel('data/2018/aggregates-economic-activity/S7.1.xlsx', header = None) + data = pd.read_excel( + "data/2018/aggregates-economic-activity/S7.1.xlsx", header=None + ) df = data[3:] 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' - })) - + 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('agc-graph', 'figure'), - [Input('tabs', 'value')]) +@app.callback(Output("agc-graph", "figure"), [Input("tabs", "value")]) def display_content(value): index = int(value) - - year_list = ['Y ' + year for year in year_set] - arrays=[] + + year_list = ["Y " + year for year in year_set] + arrays = [] for i in range(len(header_set)): arrays.append([]) rows = data.iloc[index][2:-2] length = len(header_set) for (idx, column) in enumerate(rows): - arrays[idx%length].append(column) - - graphs = [{ - 'x': year_list, - 'y': array, - 'name': header_set[idx], - 'line': { - 'width': 3, - 'shape': 'spline' + arrays[idx % length].append(column) + + graphs = [ + { + "x": year_list, + "y": array, + "name": header_set[idx], + "line": {"width": 3, "shape": "spline"}, } - } for (idx, array) in enumerate(arrays)] + for (idx, array) in enumerate(arrays) + ] return { - 'data': graphs, - 'layout': { - 'margin': { - 'l': 30, - 'r': 0, - 'b': 30, - 't': 0 - }, - 'name': 'Current Price' - } + "data": graphs, + "layout": { + "margin": {"l": 30, "r": 0, "b": 30, "t": 0}, + "name": "Current Price", + }, } diff --git a/apps/agg_national_accounts.py b/apps/agg_national_accounts.py index 16c8092..eb318d5 100644 --- a/apps/agg_national_accounts.py +++ b/apps/agg_national_accounts.py @@ -11,12 +11,17 @@ from app import app import math -filename = get_excel('aggregate_national_accounts', 'data/2018/economic-aggregates/S1.1.xlsx') +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]))] +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] @@ -31,96 +36,91 @@ # 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") + 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) + 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' - })) + 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')]) +@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] + 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_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] + 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')]) +@app.callback(Output("ana-graph-0", "figure"), [Input("tabs2", "value")]) def display_graph(value): - year_list = ['Y ' + year for year in year_set] + 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' - } + "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' - } + "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' - } + "data": [data_cu, data_co], + "layout": { + "margin": {"l": 30, "r": 0, "b": 30, "t": 0}, + "name": "Current Price", + }, } - diff --git a/apps/cfc_sectors.py b/apps/cfc_sectors.py index f17e6b7..84ec290 100644 --- a/apps/cfc_sectors.py +++ b/apps/cfc_sectors.py @@ -8,7 +8,7 @@ from app import app -data = pd.read_excel('data/2018/economic-aggregates/S1.8.xlsx') +data = pd.read_excel("data/2018/economic-aggregates/S1.8.xlsx") years = data.iloc[5:6, 2:-2] year_set = list(OrderedDict.fromkeys(years.values[0]).keys()) process = data[7:] @@ -22,117 +22,125 @@ def app_layout(): children = [dcc.Tab(label=year, value=year) for year in year_set] categories = labels.copy() - categories.insert(0, 'Main Sections') + categories.insert(0, "Main Sections") label_ids = labelIds.copy() - label_ids.insert(0, '0') + label_ids.insert(0, "0") return ( - html.Div([ - html.H2('CFC Sectors') - ]), - html.Div([ - dcc.Dropdown( - id='cfc-category', - options=[{'label': category, 'value': label_ids[idx]} for (idx, category) in enumerate(categories)], - placeholder="Select a category", - value='0' - ) - ], - style={'width': '30%', 'display': 'block', 'align': 'right', 'margin-left': 'auto', 'margin-right': '0', - 'margin-bottom': '20px'} + html.Div([html.H2("CFC Sectors")]), + html.Div( + [ + dcc.Dropdown( + id="cfc-category", + options=[ + {"label": category, "value": label_ids[idx]} + for (idx, category) in enumerate(categories) + ], + placeholder="Select a category", + value="0", + ) + ], + style={ + "width": "30%", + "display": "block", + "align": "right", + "margin-left": "auto", + "margin-right": "0", + "margin-bottom": "20px", + }, + ), + html.Div( + [ + dcc.Tabs(id="cfc-tabs", value=year_set[-1], children=children), + html.Div(id="cfc-output-tab"), + generate_table(data), + ], + className="container", ), - html.Div([ - dcc.Tabs(id="cfc-tabs", value=year_set[-1], children=children), - html.Div(id='cfc-output-tab'), - generate_table(data) - ], className="container") ) def generate_table(dataframe, max_rows=10): - data = pd.read_excel('data/2018/economic-aggregates/S1.8.xlsx', header = None) + data = pd.read_excel("data/2018/economic-aggregates/S1.8.xlsx", header=None) df = data[6:] 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' - })) + 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() def filter(year, category, rows, labels, remove=False): - cu_index, co_index = [index for index in years.transpose().index if years[index].iloc[0] == year] + cu_index, co_index = [ + index for index in years.transpose().index if years[index].iloc[0] == year + ] filtered = rows[0:-1] if remove else rows cu_values = [row[cu_index] for row in filtered] co_values = [row[co_index] for row in filtered] data_cu = [ - { - 'values': cu_values, - 'type': 'pie', - 'labels': labels - }, + {"values": cu_values, "type": "pie", "labels": labels}, ] data_co = [ - { - 'values': co_values, - 'type': 'pie', - 'labels': labels - }, + {"values": co_values, "type": "pie", "labels": labels}, ] - return html.Div([ - html.H2('Current Price'), - dcc.Graph( - id='cfc-cp-graph', - figure={ - 'data': data_cu, - 'layout': { - 'margin': { - 'l': 30, - 'r': 0, - 'b': 30, - 't': 0 + return html.Div( + [ + html.H2("Current Price"), + dcc.Graph( + id="cfc-cp-graph", + figure={ + "data": data_cu, + "layout": { + "margin": {"l": 30, "r": 0, "b": 30, "t": 0}, + "name": "Current Price", }, - 'name': 'Current Price' - } - } - ), - html.H2('Constant Price'), - dcc.Graph( - id='cfc-co-graph', - figure={ - 'data': data_co, - 'layout': { - 'margin': { - 'l': 30, - 'r': 0, - 'b': 30, - 't': 0 + }, + ), + html.H2("Constant Price"), + dcc.Graph( + id="cfc-co-graph", + figure={ + "data": data_co, + "layout": { + "margin": {"l": 30, "r": 0, "b": 30, "t": 0}, + "name": "Cost Price", }, - 'name': 'Cost Price' - } - } - ) - ]) + }, + ), + ] + ) -@app.callback(Output('cfc-output-tab', 'children'), - [Input('cfc-tabs', 'value'), Input('cfc-category', 'value')]) +@app.callback( + Output("cfc-output-tab", "children"), + [Input("cfc-tabs", "value"), Input("cfc-category", "value")], +) def display_content(year, category): - if category and category != '0': + if category and category != "0": current_index = float(category) - filtered = [data.iloc[index] for index in sections.index if - float(sections[index]) > current_index and float(sections[index]) < current_index + 1] + filtered = [ + data.iloc[index] + for index in sections.index + if float(sections[index]) > current_index + and float(sections[index]) < current_index + 1 + ] if len(filtered) == 0: - filtered = [data.iloc[index] for index in sections.index if float(sections[index]) == current_index] + filtered = [ + data.iloc[index] + for index in sections.index + if float(sections[index]) == current_index + ] sublabels = [row.iloc[-1] for row in filtered] return filter(year, category, filtered, sublabels) diff --git a/apps/cfc_time_series.py b/apps/cfc_time_series.py index 1a7c9fb..a9e99eb 100644 --- a/apps/cfc_time_series.py +++ b/apps/cfc_time_series.py @@ -8,7 +8,7 @@ from app import app import pandas as pd -data = pd.read_excel('data/2018/economic-aggregates/S1.8.xlsx') +data = pd.read_excel("data/2018/economic-aggregates/S1.8.xlsx") years = data.iloc[5:6, 2:-2] process = data[7:] @@ -20,53 +20,50 @@ def generate_table(dataframe, max_rows=10): - data = pd.read_excel('data/2018/economic-aggregates/S1.8.xlsx', header = None) + data = pd.read_excel("data/2018/economic-aggregates/S1.8.xlsx", header=None) df = data[6:] 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' - })) + 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 = html.Div([ - html.H1('CFC Time Series'), - dcc.Dropdown( - id='cfc-my-dropdown', - options=[{'label': category, 'value': labelIds[idx]} for (idx, category) in enumerate(labels)], - value=labelIds[-1], - style={'margin-bottom': '20px'} - ), - dcc.Graph(id='cfc-time-series', - style={'padding-top': '20px'}), - generate_table(data) -], className="container") +layout = html.Div( + [ + html.H1("CFC Time Series"), + dcc.Dropdown( + id="cfc-my-dropdown", + options=[ + {"label": category, "value": labelIds[idx]} + for (idx, category) in enumerate(labels) + ], + value=labelIds[-1], + style={"margin-bottom": "20px"}, + ), + dcc.Graph(id="cfc-time-series", style={"padding-top": "20px"}), + generate_table(data), + ], + className="container", +) - -@app.callback(Output('cfc-time-series', 'figure'), - [Input('cfc-my-dropdown', 'value')]) +@app.callback(Output("cfc-time-series", "figure"), [Input("cfc-my-dropdown", "value")]) def update_graph(selected_dropdown_value): index = int(selected_dropdown_value) row = data.iloc[index][2:-2] - year_list = ['Y ' + year for year in years.values[0]] + year_list = ["Y " + year for year in years.values[0]] mid = int(len(row) / 2) return { - 'data': [go.Bar( - x=year_list[:mid], - y=row[:mid], - name='Current Price' - ), go.Bar( - x=year_list[mid:], - y=row[mid:], - name='Constant Price' - )], - 'layout': { - 'title': data.iloc[index][-1] - } + "data": [ + go.Bar(x=year_list[:mid], y=row[:mid], name="Current Price"), + go.Bar(x=year_list[mid:], y=row[mid:], name="Constant Price"), + ], + "layout": {"title": data.iloc[index][-1]}, } diff --git a/apps/crop_wise_output.py b/apps/crop_wise_output.py index d321a2a..7d6bf30 100644 --- a/apps/crop_wise_output.py +++ b/apps/crop_wise_output.py @@ -8,7 +8,7 @@ from app import app import pandas as pd -data = pd.read_excel('data/2018/disaggregated-statements/S8.1.2.xlsx') +data = pd.read_excel("data/2018/disaggregated-statements/S8.1.2.xlsx") years = data.iloc[5:6, 2:-2] process = data[7:] @@ -20,52 +20,49 @@ def generate_table(dataframe, max_rows=10): - data = pd.read_excel('data/2018/disaggregated-statements/S8.1.2.xlsx', header = None) + data = pd.read_excel("data/2018/disaggregated-statements/S8.1.2.xlsx", header=None) df = data[6:] 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' - }, - )) + 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 = html.Div([ - html.H2('Crop-wise value of output'), - dcc.Dropdown( - id='my-dropdown', - options=[{'label': category, 'value': labelIds[idx]} for (idx, category) in enumerate(labels)], - value=labelIds[-1], - style={'margin-bottom': '20px'} - ), - dcc.Graph(id='crop_wise_output', - style={'padding-top': '20px'}), - generate_table(data) -], className="container") +layout = html.Div( + [ + html.H2("Crop-wise value of output"), + dcc.Dropdown( + id="my-dropdown", + options=[ + {"label": category, "value": labelIds[idx]} + for (idx, category) in enumerate(labels) + ], + value=labelIds[-1], + style={"margin-bottom": "20px"}, + ), + dcc.Graph(id="crop_wise_output", style={"padding-top": "20px"}), + generate_table(data), + ], + className="container", +) -@app.callback(Output('crop_wise_output', 'figure'), - [Input('my-dropdown', 'value')]) +@app.callback(Output("crop_wise_output", "figure"), [Input("my-dropdown", "value")]) def update_graph(selected_dropdown_value): index = int(selected_dropdown_value) row = data.iloc[index][2:-2] - year_list = ['Y ' + year for year in years.values[0]] + year_list = ["Y " + year for year in years.values[0]] mid = int(len(row) / 2) return { - 'data': [go.Bar( - x=year_list[:mid], - y=row[:mid], - name='Current Price' - ), go.Bar( - x=year_list[mid:], - y=row[mid:], - name='Constant Price' - )], - 'layout': { - 'title': data.iloc[index][-1] - } + "data": [ + go.Bar(x=year_list[:mid], y=row[:mid], name="Current Price"), + go.Bar(x=year_list[mid:], y=row[mid:], name="Constant Price"), + ], + "layout": {"title": data.iloc[index][-1]}, } diff --git a/apps/gcf_sectors.py b/apps/gcf_sectors.py index b77fe55..bdca82c 100644 --- a/apps/gcf_sectors.py +++ b/apps/gcf_sectors.py @@ -10,58 +10,76 @@ from app import app -data = pd.read_excel('data/2018/economic-aggregates/S1.10.xlsx') +data = pd.read_excel("data/2018/economic-aggregates/S1.10.xlsx") 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() or (type(sections[index]) != str and math.isnan(sections[index]))] +main_sections = [ + index + for index in sections.index + if str(sections[index]).isdigit() + or (type(sections[index]) != str and math.isnan(sections[index])) +] rows = [data.iloc[idx] for idx in main_sections] labels = [row.iloc[1] for row in rows[0:-2]] labelIds = [row.iloc[0] for row in rows[0:-2]] def generate_table(dataframe, max_rows=10): - data = pd.read_excel('data/2018/economic-aggregates/S1.10.xlsx', header = None) + data = pd.read_excel("data/2018/economic-aggregates/S1.10.xlsx", header=None) df = data[6:] 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' - })) + 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", + }, + ) def app_layout(): children = [dcc.Tab(label=year, value=year) for year in year_set] categories = labels.copy() - categories.insert(0, 'Main Sections') + categories.insert(0, "Main Sections") label_ids = labelIds.copy() - label_ids.insert(0, '0') + label_ids.insert(0, "0") return ( - html.H2('Gross Capital Formation Sectors'), - html.Div([ - dcc.Dropdown( - id='gcf-category', - options=[{'label': category, 'value': label_ids[idx]} for (idx, category) in enumerate(categories)], - placeholder="Select a category", - value='0' - ) - ], - style={'width': '30%', 'display': 'block', 'align': 'right', 'margin-left': 'auto', 'margin-right': '0', - 'margin-bottom': '20px'} + html.H2("Gross Capital Formation Sectors"), + html.Div( + [ + dcc.Dropdown( + id="gcf-category", + options=[ + {"label": category, "value": label_ids[idx]} + for (idx, category) in enumerate(categories) + ], + placeholder="Select a category", + value="0", + ) + ], + style={ + "width": "30%", + "display": "block", + "align": "right", + "margin-left": "auto", + "margin-right": "0", + "margin-bottom": "20px", + }, + ), + html.Div( + [ + dcc.Tabs(id="gcf-tabs", value=year_set[-1], children=children), + html.Div(id="gcf-output-tab"), + generate_table(data), + ], + className="container", ), - html.Div([ - dcc.Tabs(id="gcf-tabs", value=year_set[-1], children=children), - html.Div(id='gcf-output-tab'), - generate_table(data) - ],className="container") - ) @@ -69,71 +87,67 @@ def app_layout(): def filter(year, category, rows, labels, remove=False): - cu_index, co_index = [index for index in years.transpose().index if years[index].iloc[0] == year] + cu_index, co_index = [ + index for index in years.transpose().index if years[index].iloc[0] == year + ] filtered = rows[0:-2] if remove else rows cu_values = [row[cu_index] for row in filtered] co_values = [row[co_index] for row in filtered] data_cu = [ - { - 'values': cu_values, - 'type': 'pie', - 'labels': labels - }, + {"values": cu_values, "type": "pie", "labels": labels}, ] data_co = [ - { - 'values': co_values, - 'type': 'pie', - 'labels': labels - }, + {"values": co_values, "type": "pie", "labels": labels}, ] - return html.Div([ - html.H2('Current Price'), - dcc.Graph( - id='gcf-cp-graph', - figure={ - 'data': data_cu, - 'layout': { - 'margin': { - 'l': 30, - 'r': 0, - 'b': 30, - 't': 0 + return html.Div( + [ + html.H2("Current Price"), + dcc.Graph( + id="gcf-cp-graph", + figure={ + "data": data_cu, + "layout": { + "margin": {"l": 30, "r": 0, "b": 30, "t": 0}, + "name": "Current Price", }, - 'name': 'Current Price' - } - } - ), - html.H2('Constant Price'), - dcc.Graph( - id='gcf-co-graph', - figure={ - 'data': data_co, - 'layout': { - 'margin': { - 'l': 30, - 'r': 0, - 'b': 30, - 't': 0 + }, + ), + html.H2("Constant Price"), + dcc.Graph( + id="gcf-co-graph", + figure={ + "data": data_co, + "layout": { + "margin": {"l": 30, "r": 0, "b": 30, "t": 0}, + "name": "Cost Price", }, - 'name': 'Cost Price' - } - } - ) - ]) + }, + ), + ] + ) -@app.callback(Output('gcf-output-tab', 'children'), - [Input('gcf-tabs', 'value'), Input('gcf-category', 'value')]) +@app.callback( + Output("gcf-output-tab", "children"), + [Input("gcf-tabs", "value"), Input("gcf-category", "value")], +) def display_content(year, category): - if category and category != '0': + if category and category != "0": current_index = float(category) - filtered = [data.iloc[index] for index in sections.index if - float(sections[index]) > current_index and float(sections[index]) < current_index + 1] + filtered = [ + data.iloc[index] + for index in sections.index + if float(sections[index]) > current_index + and float(sections[index]) < current_index + 1 + ] if len(filtered) == 0: - filtered = [data.iloc[index] for index in sections.index if float(sections[index]) == current_index] + filtered = [ + data.iloc[index] + for index in sections.index + if float(sections[index]) == current_index + ] sublabels = [row.iloc[1] for row in filtered] return filter(year, category, filtered, sublabels) diff --git a/apps/gcf_time_series.py b/apps/gcf_time_series.py index 132e4f3..f386ab3 100644 --- a/apps/gcf_time_series.py +++ b/apps/gcf_time_series.py @@ -9,12 +9,17 @@ from app import app import pandas as pd -data = pd.read_excel('data/2018/economic-aggregates/S1.10.xlsx') +data = pd.read_excel("data/2018/economic-aggregates/S1.10.xlsx") years = data.iloc[5:6, 2:-2] process = data[7:] sections = process.iloc[:, 0] -main_sections = [index for index in sections.index if str(sections[index]).isdigit() or (type(sections[index]) != str and math.isnan(sections[index]))] +main_sections = [ + index + for index in sections.index + if str(sections[index]).isdigit() + or (type(sections[index]) != str and math.isnan(sections[index])) +] rows = [data.iloc[idx] for idx in main_sections] labels = [row.iloc[1] for row in rows[0:-1]] labelIds = main_sections @@ -22,51 +27,49 @@ def generate_table(dataframe, max_rows=10): - data = pd.read_excel('data/2018/economic-aggregates/S1.10.xlsx', header = None) + data = pd.read_excel("data/2018/economic-aggregates/S1.10.xlsx", header=None) df = data[6:] 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' - })) + 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 = html.Div([ - html.H2('Gross Capital Formation Timeseries'), - dcc.Dropdown( - id='gcf-my-dropdown', - options=[{'label': category, 'value': labelIds[idx]} for (idx, category) in enumerate(labels)], - value=labelIds[1], - style={'margin-bottom': '20px'} - ), - dcc.Graph(id='gcf-time-series', - style={'padding-top': '20px'}), - generate_table(data) - ], className="container") +layout = html.Div( + [ + html.H2("Gross Capital Formation Timeseries"), + dcc.Dropdown( + id="gcf-my-dropdown", + options=[ + {"label": category, "value": labelIds[idx]} + for (idx, category) in enumerate(labels) + ], + value=labelIds[1], + style={"margin-bottom": "20px"}, + ), + dcc.Graph(id="gcf-time-series", style={"padding-top": "20px"}), + generate_table(data), + ], + className="container", +) -@app.callback(Output('gcf-time-series', 'figure'), - [Input('gcf-my-dropdown', 'value')]) +@app.callback(Output("gcf-time-series", "figure"), [Input("gcf-my-dropdown", "value")]) def update_graph(selected_dropdown_value): index = int(selected_dropdown_value) row = data.iloc[index][2:-2] - year_list = ['Y ' + year for year in years.values[0]] + year_list = ["Y " + year for year in years.values[0]] mid = int(len(row) / 2) return { - 'data': [go.Bar( - x=year_list[:mid], - y=row[:mid], - name='Current Price' - ), go.Bar( - x=year_list[mid:], - y=row[mid:], - name='Constant Price' - )], - 'layout': { - 'title': data.iloc[index][1] - } + "data": [ + go.Bar(x=year_list[:mid], y=row[:mid], name="Current Price"), + go.Bar(x=year_list[mid:], y=row[mid:], name="Constant Price"), + ], + "layout": {"title": data.iloc[index][1]}, } diff --git a/apps/gva_sectors.py b/apps/gva_sectors.py index 6e448c1..a4eb24f 100644 --- a/apps/gva_sectors.py +++ b/apps/gva_sectors.py @@ -10,7 +10,7 @@ from app import app -filename = get_excel('gva_sectors', 'data/2018/economic-aggregates/S1.6.xlsx') +filename = get_excel("gva_sectors", "data/2018/economic-aggregates/S1.6.xlsx") data = pd.read_excel(filename) years = data.iloc[5:6, 2:-2] @@ -26,115 +26,125 @@ def app_layout(): children = [dcc.Tab(label=year, value=year) for year in year_set] categories = labels.copy() - categories.insert(0, 'Main Sections') + categories.insert(0, "Main Sections") label_ids = labelIds.copy() - label_ids.insert(0, '0') + label_ids.insert(0, "0") return ( - html.H2('Gross Value Added Sectors'), - html.Div([ - dcc.Dropdown( - id='category', - options=[{'label': category, 'value': label_ids[idx]} for (idx, category) in enumerate(categories)], - placeholder="Select a category", - value='0' - ) - ], - style={'width': '30%', 'display': 'block', 'align': 'right', 'margin-left': 'auto', 'margin-right': '0', - 'margin-bottom': '20px'} + html.H2("Gross Value Added Sectors"), + html.Div( + [ + dcc.Dropdown( + id="category", + options=[ + {"label": category, "value": label_ids[idx]} + for (idx, category) in enumerate(categories) + ], + placeholder="Select a category", + value="0", + ) + ], + style={ + "width": "30%", + "display": "block", + "align": "right", + "margin-left": "auto", + "margin-right": "0", + "margin-bottom": "20px", + }, + ), + html.Div( + [ + dcc.Tabs(id="tabs", value=year_set[-1], children=children), + html.Div(id="output-tab"), + generate_table(data), + ], + className="container", ), - html.Div([ - dcc.Tabs(id="tabs", value=year_set[-1], children=children), - html.Div(id='output-tab'), - generate_table(data) - ], className="container") ) def generate_table(dataframe, max_rows=10): - data = pd.read_excel(filename, header = None) + data = pd.read_excel(filename, header=None) df = data[6:] 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' - })) + 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() def filter(year, category, rows, labels, remove=False): - cu_index, co_index = [index for index in years.transpose().index if years[index].iloc[0] == year] + cu_index, co_index = [ + index for index in years.transpose().index if years[index].iloc[0] == year + ] filtered = rows[0:-1] if remove else rows cu_values = [row[cu_index] for row in filtered] co_values = [row[co_index] for row in filtered] data_cu = [ - { - 'values': cu_values, - 'type': 'pie', - 'labels': labels - }, + {"values": cu_values, "type": "pie", "labels": labels}, ] data_co = [ - { - 'values': co_values, - 'type': 'pie', - 'labels': labels - }, + {"values": co_values, "type": "pie", "labels": labels}, ] - return html.Div([ - html.H2('Current Price'), - dcc.Graph( - id='cp-graph', - figure={ - 'data': data_cu, - 'layout': { - 'margin': { - 'l': 30, - 'r': 0, - 'b': 30, - 't': 0 + return html.Div( + [ + html.H2("Current Price"), + dcc.Graph( + id="cp-graph", + figure={ + "data": data_cu, + "layout": { + "margin": {"l": 30, "r": 0, "b": 30, "t": 0}, + "name": "Current Price", }, - 'name': 'Current Price' - } - } - ), - html.H2('Constant Price'), - dcc.Graph( - id='co-graph', - figure={ - 'data': data_co, - 'layout': { - 'margin': { - 'l': 30, - 'r': 0, - 'b': 30, - 't': 0 + }, + ), + html.H2("Constant Price"), + dcc.Graph( + id="co-graph", + figure={ + "data": data_co, + "layout": { + "margin": {"l": 30, "r": 0, "b": 30, "t": 0}, + "name": "Cost Price", }, - 'name': 'Cost Price' - } - } - ) - ]) + }, + ), + ] + ) -@app.callback(Output('output-tab', 'children'), - [Input('tabs', 'value'), Input('category', 'value')]) +@app.callback( + Output("output-tab", "children"), + [Input("tabs", "value"), Input("category", "value")], +) def display_content(year, category): - if category and category != '0': + if category and category != "0": current_index = float(category) - filtered = [data.iloc[index] for index in sections.index if - float(sections[index]) > current_index and float(sections[index]) < current_index + 1] + filtered = [ + data.iloc[index] + for index in sections.index + if float(sections[index]) > current_index + and float(sections[index]) < current_index + 1 + ] if len(filtered) == 0: - filtered = [data.iloc[index] for index in sections.index if float(sections[index]) == current_index] + filtered = [ + data.iloc[index] + for index in sections.index + if float(sections[index]) == current_index + ] sublabels = [row.iloc[-1] for row in filtered] return filter(year, category, filtered, sublabels) diff --git a/apps/gva_time_series.py b/apps/gva_time_series.py index 05e884a..d93cddb 100644 --- a/apps/gva_time_series.py +++ b/apps/gva_time_series.py @@ -9,7 +9,7 @@ from app import app import pandas as pd -filename = get_excel('gva_time_series', 'data/2018/economic-aggregates/S1.6.xlsx') +filename = get_excel("gva_time_series", "data/2018/economic-aggregates/S1.6.xlsx") data = pd.read_excel(filename) years = data.iloc[5:6, 2:-2] @@ -23,51 +23,49 @@ def generate_table(dataframe, max_rows=10): - data = pd.read_excel(filename, header = None) + data = pd.read_excel(filename, header=None) df = data[6:] 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' - }, - )) + 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 = html.Div([ - html.H2('Gross Value Added Timeseries'), - dcc.Dropdown( - id='my-dropdown', - options=[{'label': category, 'value': labelIds[idx]} for (idx, category) in enumerate(labels)], - value=labelIds[-1], - style={'margin-bottom': '20px'} - ), - dcc.Graph(id='gva-time-series', - style={'padding-top': '20px'}), - generate_table(data) -], className="container") +layout = html.Div( + [ + html.H2("Gross Value Added Timeseries"), + dcc.Dropdown( + id="my-dropdown", + options=[ + {"label": category, "value": labelIds[idx]} + for (idx, category) in enumerate(labels) + ], + value=labelIds[-1], + style={"margin-bottom": "20px"}, + ), + dcc.Graph(id="gva-time-series", style={"padding-top": "20px"}), + generate_table(data), + ], + className="container", +) -@app.callback(Output('gva-time-series', 'figure'), - [Input('my-dropdown', 'value')]) + +@app.callback(Output("gva-time-series", "figure"), [Input("my-dropdown", "value")]) def update_graph(selected_dropdown_value): index = int(selected_dropdown_value) row = process.iloc[index, 2:-2].values - year_list = ['Y ' + year for year in years.values[0]] + year_list = ["Y " + year for year in years.values[0]] mid = int(len(row) / 2) return { - 'data': [go.Bar( - x=year_list[:mid], - y=row[:mid], - name='Current Price' - ), go.Bar( - x=year_list[mid:], - y=row[mid:], - name='Constant Price' - )], - 'layout': { - 'title': data.iloc[index][-1] - } + "data": [ + go.Bar(x=year_list[:mid], y=row[:mid], name="Current Price"), + go.Bar(x=year_list[mid:], y=row[mid:], name="Constant Price"), + ], + "layout": {"title": data.iloc[index][-1]}, } diff --git a/apps/home.py b/apps/home.py index d074eb4..dd3f874 100644 --- a/apps/home.py +++ b/apps/home.py @@ -5,41 +5,25 @@ from app import app -layout = html.Div([ - html.Div([ - dcc.Link('Go to GVA Sector', href='/gva-sectors') - ]), - html.Div([ - dcc.Link('Go to GVA Time Series', href='/gva-time-series') - ]), - html.Div([ - dcc.Link('Go to Agg. National Accounts', href='/agg_national_accounts') - ]), - html.Div([ - dcc.Link('Go to Agg. Economic Activities', href='/agg-eco-activities') - ]), - html.Div([ - dcc.Link('Go to NVA Sector', href='/nv_eco') - ]), - html.Div([ - dcc.Link('Go to CFC Sector', href='/cfc_sectors') - ]), - html.Div([ - dcc.Link('Go to CFC Time Series', href='/cfc_time_series') - ]), - html.Div([ - dcc.Link('Go to NV Time Series', href='/nv_time_series') - ]), - html.Div([ - dcc.Link('Go to GCF Time Series', href='/gcf_time_series') - ]), - html.Div([ - dcc.Link('Go to GCF Sector', href='/gcf_sectors') - ]), - html.Div([ - dcc.Link('Go to Households Private consumption', href='/household') - ]), - html.Div([ - dcc.Link('Go to Disaggregated Crop Statements', href='/crop_wise_output') - ]) -]) +layout = html.Div( + [ + html.Div([dcc.Link("Go to GVA Sector", href="/gva-sectors")]), + html.Div([dcc.Link("Go to GVA Time Series", href="/gva-time-series")]), + html.Div( + [dcc.Link("Go to Agg. National Accounts", href="/agg_national_accounts")] + ), + html.Div( + [dcc.Link("Go to Agg. Economic Activities", href="/agg-eco-activities")] + ), + html.Div([dcc.Link("Go to NVA Sector", href="/nv_eco")]), + html.Div([dcc.Link("Go to CFC Sector", href="/cfc_sectors")]), + html.Div([dcc.Link("Go to CFC Time Series", href="/cfc_time_series")]), + html.Div([dcc.Link("Go to NV Time Series", href="/nv_time_series")]), + html.Div([dcc.Link("Go to GCF Time Series", href="/gcf_time_series")]), + html.Div([dcc.Link("Go to GCF Sector", href="/gcf_sectors")]), + html.Div([dcc.Link("Go to Households Private consumption", href="/household")]), + html.Div( + [dcc.Link("Go to Disaggregated Crop Statements", href="/crop_wise_output")] + ), + ] +) diff --git a/apps/household.py b/apps/household.py index 25c1f40..79e844f 100644 --- a/apps/household.py +++ b/apps/household.py @@ -10,11 +10,15 @@ from app import app import pandas as pd -filename = get_excel('household', 'data/2018/households/S5.2.xlsx') +filename = get_excel("household", "data/2018/households/S5.2.xlsx") data = pd.read_excel(filename) years = data.iloc[3:4, 2:-2] -year_set = [year for year in list(OrderedDict.fromkeys(years.values[0]).keys()) if type(year) == str] +year_set = [ + year + for year in list(OrderedDict.fromkeys(years.values[0]).keys()) + if type(year) == str +] process = data[6:] headers = data.iloc[5][2:-2] @@ -24,57 +28,58 @@ def generate_table(dataframe, max_rows=10): - data = pd.read_excel('data/2018/households/S5.2.xlsx', header = None) + data = pd.read_excel("data/2018/households/S5.2.xlsx", header=None) df = data[3:] 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' - })) + 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 = html.Div([ - html.H1('Private final consumption expenditure classified by item'), - dcc.Dropdown( - id='house-dropdown', - options=[{'label': i, 'value': labelIds[idx]} for (idx,i) in enumerate(labels)], - value=labelIds[-1], - style={'margin-bottom': '20px'} - ), - dcc.Graph(id='household-bar-graph', - style={'padding-top': '20px'}), - generate_table(data) - ], className="container") +layout = html.Div( + [ + html.H1("Private final consumption expenditure classified by item"), + dcc.Dropdown( + id="house-dropdown", + options=[ + {"label": i, "value": labelIds[idx]} for (idx, i) in enumerate(labels) + ], + value=labelIds[-1], + style={"margin-bottom": "20px"}, + ), + dcc.Graph(id="household-bar-graph", style={"padding-top": "20px"}), + generate_table(data), + ], + className="container", +) -@app.callback(Output('household-bar-graph', 'figure'), - [Input('house-dropdown', 'value')]) + +@app.callback( + Output("household-bar-graph", "figure"), [Input("house-dropdown", "value")] +) def update_graph(selected_dropdown_value): index = int(selected_dropdown_value) rows = data.iloc[index][2:-2].values - year_list = ['Y ' + year for year in year_set] + year_list = ["Y " + year for year in year_set] - arrays=[] + arrays = [] for i in range(len(header_set)): arrays.append([]) length = len(header_set) for (idx, column) in enumerate(rows): - arrays[idx%length].append(column) - - graphs = [go.Bar( - x=year_list, - y=array, - name=header_set[idx] - ) for (idx, array) in enumerate(arrays)] + arrays[idx % length].append(column) + + graphs = [ + go.Bar(x=year_list, y=array, name=header_set[idx]) + for (idx, array) in enumerate(arrays) + ] - return { - 'data': graphs, - 'layout': { - 'title': 'test' - } - } + return {"data": graphs, "layout": {"title": "test"}} diff --git a/apps/nv_eco.py b/apps/nv_eco.py index 74e2628..9999a6a 100644 --- a/apps/nv_eco.py +++ b/apps/nv_eco.py @@ -9,11 +9,11 @@ from app import app -data = pd.read_excel('data/2018/economic-aggregates/S1.7r.xlsx') -years = data.iloc[5:6,2:-2] +data = pd.read_excel("data/2018/economic-aggregates/S1.7r.xlsx") +years = data.iloc[5:6, 2:-2] year_set = list(OrderedDict.fromkeys(years.values[0]).keys()) process = data[7:] -sections = process.iloc[:,0] +sections = process.iloc[:, 0] main_sections = [index for index in sections.index if sections[index].isdigit()] rows = [data.iloc[idx] for idx in main_sections] labels = [row.iloc[-1] for row in rows[0:-1]] @@ -21,111 +21,126 @@ def generate_table(dataframe, max_rows=10): - data = pd.read_excel('data/2018/economic-aggregates/S1.7r.xlsx', header = None) + data = pd.read_excel("data/2018/economic-aggregates/S1.7r.xlsx", header=None) df = data[6:] 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' - })) + 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", + }, + ) def app_layout(): children = [dcc.Tab(label=year, value=year) for year in year_set] categories = labels.copy() - categories.insert(0, 'Main Sections') + categories.insert(0, "Main Sections") label_ids = labelIds.copy() - label_ids.insert(0, '0') - - return( - html.Div([ - dcc.Dropdown( - id='nv-category', - options=[{'label': category, 'value': label_ids[idx]} for (idx, category) in enumerate(categories)], - placeholder="Select a category", - value='0' - ) + label_ids.insert(0, "0") + + return ( + html.Div( + [ + dcc.Dropdown( + id="nv-category", + options=[ + {"label": category, "value": label_ids[idx]} + for (idx, category) in enumerate(categories) + ], + placeholder="Select a category", + value="0", + ) ], - style={'width': '30%', 'display': 'block', 'align': 'right', 'margin-left': 'auto', 'margin-right': '0', 'margin-bottom': '20px'} - ), - html.Div([ - dcc.Tabs(id="nv-tabs", value=year_set[-1], children=children), - html.Div(id='nv-output-tab'), - generate_table(data) - ], className="container") + style={ + "width": "30%", + "display": "block", + "align": "right", + "margin-left": "auto", + "margin-right": "0", + "margin-bottom": "20px", + }, + ), + html.Div( + [ + dcc.Tabs(id="nv-tabs", value=year_set[-1], children=children), + html.Div(id="nv-output-tab"), + generate_table(data), + ], + className="container", + ), ) -layout=app_layout() + +layout = app_layout() + def filter(year, category, rows, labels, remove=False): - cu_index, co_index = [index for index in years.transpose().index if years[index].iloc[0] == year] - + cu_index, co_index = [ + index for index in years.transpose().index if years[index].iloc[0] == year + ] + filtered = rows[0:-1] if remove else rows cu_values = [row[cu_index] for row in filtered] co_values = [row[co_index] for row in filtered] data_cu = [ - { - 'values':cu_values, - 'type': 'pie', - 'labels': labels - }, + {"values": cu_values, "type": "pie", "labels": labels}, ] data_co = [ - { - 'values':co_values, - 'type': 'pie', - 'labels': labels - }, + {"values": co_values, "type": "pie", "labels": labels}, ] - return html.Div([ - html.H2('Current Price'), - dcc.Graph( - id='nv-cp-graph', - figure={ - 'data': data_cu, - 'layout': { - 'margin': { - 'l': 30, - 'r': 0, - 'b': 30, - 't': 0 + return html.Div( + [ + html.H2("Current Price"), + dcc.Graph( + id="nv-cp-graph", + figure={ + "data": data_cu, + "layout": { + "margin": {"l": 30, "r": 0, "b": 30, "t": 0}, + "name": "Current Price", }, - 'name': 'Current Price' - } - } - ), - html.H2('Constant Price'), - dcc.Graph( - id='nv-co-graph', - figure={ - 'data': data_co, - 'layout': { - 'margin': { - 'l': 30, - 'r': 0, - 'b': 30, - 't': 0 + }, + ), + html.H2("Constant Price"), + dcc.Graph( + id="nv-co-graph", + figure={ + "data": data_co, + "layout": { + "margin": {"l": 30, "r": 0, "b": 30, "t": 0}, + "name": "Cost Price", }, - 'name': 'Cost Price' - } - } - ) - ]) - -@app.callback(Output('nv-output-tab', 'children'), - [Input('nv-tabs', 'value'), Input('nv-category', 'value')]) + }, + ), + ] + ) + + +@app.callback( + Output("nv-output-tab", "children"), + [Input("nv-tabs", "value"), Input("nv-category", "value")], +) def display_content(year, category): - if category and category != '0': + if category and category != "0": current_index = float(category) - filtered = [data.iloc[index] for index in sections.index if float(sections[index]) > current_index and float(sections[index]) < current_index + 1] + filtered = [ + data.iloc[index] + for index in sections.index + if float(sections[index]) > current_index + and float(sections[index]) < current_index + 1 + ] if len(filtered) == 0: - filtered = [data.iloc[index] for index in sections.index if float(sections[index]) == current_index] + filtered = [ + data.iloc[index] + for index in sections.index + if float(sections[index]) == current_index + ] sublabels = [row.iloc[-1] for row in filtered] diff --git a/apps/nv_time_series.py b/apps/nv_time_series.py index 65c4413..cbf5588 100644 --- a/apps/nv_time_series.py +++ b/apps/nv_time_series.py @@ -8,7 +8,7 @@ from app import app import pandas as pd -data = pd.read_excel('data/2018/economic-aggregates/S1.7r.xlsx') +data = pd.read_excel("data/2018/economic-aggregates/S1.7r.xlsx") years = data.iloc[5:6, 2:-2] process = data[7:] @@ -20,51 +20,51 @@ def generate_table(dataframe, max_rows=10): - data = pd.read_excel('data/2018/aggregates-economic-activity/S7.1.xlsx', header = None) + data = pd.read_excel( + "data/2018/aggregates-economic-activity/S7.1.xlsx", header=None + ) df = data[3:] 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' - })) + 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 = html.Div([ - html.H1('NVA Time Series'), - dcc.Dropdown( - id='nv-my-dropdown', - options=[{'label': category, 'value': labelIds[idx]} for (idx, category) in enumerate(labels)], - value=labelIds[-1], - style={'margin-bottom': '20px'} - ), - dcc.Graph(id='nv-time-series', - style={'padding-top': '20px'}), - generate_table(data) -], className="container") +layout = html.Div( + [ + html.H1("NVA Time Series"), + dcc.Dropdown( + id="nv-my-dropdown", + options=[ + {"label": category, "value": labelIds[idx]} + for (idx, category) in enumerate(labels) + ], + value=labelIds[-1], + style={"margin-bottom": "20px"}, + ), + dcc.Graph(id="nv-time-series", style={"padding-top": "20px"}), + generate_table(data), + ], + className="container", +) -@app.callback(Output('nv-time-series', 'figure'), - [Input('nv-my-dropdown', 'value')]) +@app.callback(Output("nv-time-series", "figure"), [Input("nv-my-dropdown", "value")]) def update_graph(selected_dropdown_value): index = int(selected_dropdown_value) row = data.iloc[index][2:-2] - year_list = ['Y ' + year for year in years.values[0]] + year_list = ["Y " + year for year in years.values[0]] mid = int(len(row) / 2) return { - 'data': [go.Bar( - x=year_list[:mid], - y=row[:mid], - name='Current Price' - ), go.Bar( - x=year_list[mid:], - y=row[mid:], - name='Constant Price' - )], - 'layout': { - 'title': data.iloc[index][-1] - } + "data": [ + go.Bar(x=year_list[:mid], y=row[:mid], name="Current Price"), + go.Bar(x=year_list[mid:], y=row[mid:], name="Constant Price"), + ], + "layout": {"title": data.iloc[index][-1]}, } diff --git a/index.py b/index.py index 76f7fb1..b8947d4 100644 --- a/index.py +++ b/index.py @@ -6,88 +6,98 @@ from flask import Flask, request, redirect, url_for, render_template, flash from werkzeug.utils import secure_filename from app import app, server -from apps import home, crop_wise_output, gva_sectors, agg_national_accounts, gva_time_series, agg_eco_activities, \ - cfc_sectors, nv_eco, cfc_time_series, nv_time_series, household, gcf_sectors, gcf_time_series +from apps import ( + home, + crop_wise_output, + gva_sectors, + agg_national_accounts, + gva_time_series, + agg_eco_activities, + cfc_sectors, + nv_eco, + cfc_time_series, + nv_time_series, + household, + gcf_sectors, + gcf_time_series, +) from apps.admin import requires_auth -UPLOAD_FOLDER = 'data/uploads' -ALLOWED_EXTENSIONS = set(['xlsx']) -external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css'] +UPLOAD_FOLDER = "data/uploads" +ALLOWED_EXTENSIONS = set(["xlsx"]) +external_stylesheets = ["https://codepen.io/chriddyp/pen/bWLwgP.css"] -server.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER +server.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER def allowed_file(filename): - return '.' in filename and \ - filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS + return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS -app.layout = html.Div([ - dcc.Location(id='url', refresh=False), - html.Div(id='page-content') -]) +app.layout = html.Div( + [dcc.Location(id="url", refresh=False), html.Div(id="page-content")] +) -@server.route('/admin', methods=['GET', 'POST']) +@server.route("/admin", methods=["GET", "POST"]) @requires_auth def admin(): - if request.method == 'POST': - option = request.form.get('options') - UPLOAD_FOLDER = 'data/uploads/{}'.format(option) - server.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER + if request.method == "POST": + option = request.form.get("options") + UPLOAD_FOLDER = "data/uploads/{}".format(option) + server.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER # print(option) # check if the post request has the file part - if 'file' not in request.files: - return 'No file part' + if "file" not in request.files: + return "No file part" return redirect(request.url) - file = request.files['file'] + file = request.files["file"] # if user does not select file, browser also # submit a empty part without filename - if file.filename == '': - return 'No file selected' + if file.filename == "": + return "No file selected" return redirect(request.url) if file and allowed_file(file.filename): filename = secure_filename(file.filename) - file.save(os.path.join(server.config['UPLOAD_FOLDER'], filename)) - flash('File successfully uploaded') - return redirect(url_for('admin')) - return render_template('upload.html') + file.save(os.path.join(server.config["UPLOAD_FOLDER"], filename)) + flash("File successfully uploaded") + return redirect(url_for("admin")) + return render_template("upload.html") -@app.callback(Output('page-content', 'children'), - [Input('url', 'pathname')]) +@app.callback(Output("page-content", "children"), [Input("url", "pathname")]) def display_page(pathname): - if pathname == '/': + if pathname == "/": return None - elif pathname == '/agg_national_accounts': + elif pathname == "/agg_national_accounts": return agg_national_accounts.layout - elif pathname == '/gcf_sectors': + elif pathname == "/gcf_sectors": return gcf_sectors.layout - elif pathname == '/gcf_time_series': + elif pathname == "/gcf_time_series": return gcf_time_series.layout - elif pathname == '/gva-sectors': + elif pathname == "/gva-sectors": return gva_sectors.layout - elif pathname == '/gva-time-series': + elif pathname == "/gva-time-series": return gva_time_series.layout - elif pathname == '/agg-eco-activities': + elif pathname == "/agg-eco-activities": return agg_eco_activities.layout - elif pathname == '/nv_eco': + elif pathname == "/nv_eco": return nv_eco.layout - elif pathname == '/cfc_sectors': + elif pathname == "/cfc_sectors": return cfc_sectors.layout - elif pathname == '/cfc_time_series': + elif pathname == "/cfc_time_series": return cfc_time_series.layout - elif pathname == '/nv_time_series': + elif pathname == "/nv_time_series": return nv_time_series.layout - elif pathname == '/household': + elif pathname == "/household": return household.layout - elif pathname == '/crop_wise_output': + elif pathname == "/crop_wise_output": return crop_wise_output.layout - elif pathname == '/admin': + elif pathname == "/admin": return admin.layout else: - return '404' + return "404" -if __name__ == '__main__': - app.run_server(debug=True, host='0.0.0.0') +if __name__ == "__main__": + app.run_server(debug=True, host="0.0.0.0") diff --git a/utils.py b/utils.py index 789f190..d345ad6 100644 --- a/utils.py +++ b/utils.py @@ -1,6 +1,7 @@ import os import glob + def get_excel(folder_name, default_file=False): files = glob.glob("data/uploads/{}/*.xlsx".format(folder_name)) if len(files) == 0: