Skip to content

Commit

Permalink
Format code with Black
Browse files Browse the repository at this point in the history
  • Loading branch information
deepsource-autofix[bot] committed Jul 6, 2020
1 parent 136e5ea commit 6ea0170
Show file tree
Hide file tree
Showing 17 changed files with 818 additions and 756 deletions.
18 changes: 10 additions & 8 deletions app.py
Expand Up @@ -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"
15 changes: 10 additions & 5 deletions apps/admin.py
Expand Up @@ -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)
Expand All @@ -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

return decorated
117 changes: 65 additions & 52 deletions apps/agg_eco_activities.py
Expand Up @@ -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",
},
}
126 changes: 63 additions & 63 deletions apps/agg_national_accounts.py
Expand Up @@ -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]
Expand All @@ -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",
},
}

0 comments on commit 6ea0170

Please sign in to comment.