-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
144 lines (125 loc) · 4.04 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
'''
main python script
'''
# -*- coding: utf-8 -*-
import base64
import datetime
import io
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table_experiments as dt
from dash.dependencies import Input, Output
import pandas as pd
# initialize dash app
app = dash.Dash()
# generates a table (reusable component)
def generate_table(dataframe, max_rows=10):
return html.Table(
# Header
[html.Tr([html.Th(col) for col in dataframe.columns])] +
# Body
[html.Tr([
html.Td(dataframe.iloc[i][col]) for col in dataframe.columns
]) for i in range(min(len(dataframe), max_rows))]
)
# defines front-end layout
app.layout = html.Div(children=[
html.H1(children='Bank Parser Dashboard'),
dcc.Upload(
id='upload-data',
children=html.Div(children=[
'Drag and Drop or ',
html.A('Click to select file.')
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px'
},
# Allow multiple files to be uploaded
multiple=True
),
html.Div(id='output-data-upload'),
html.Div(dt.DataTable(rows=[{}]), style={'display': 'none'}),
html.Div(children='''
For DANY by hackNY :)
'''),
])
def parse_contents(contents, filename, date):
content_type, content_string = contents.split(',')
decoded = base64.b64decode(content_string)
try:
if 'csv' in filename:
# Assume that the user uploaded a CSV file
df = pd.read_csv(
io.StringIO(decoded.decode('utf-8')))
elif 'xls' in filename:
# Assume that the user uploaded an excel file
df = pd.read_excel(io.BytesIO(decoded))
else:
return html.Div('Make sure the file type is .csv or .xls')
except IOError as e:
print(e)
return html.Div([
'Error occured uploading file.'
])
x = [pd.to_datetime(date, format='%m/%d') for date in df['transaction_date']]
y = [abs(float(a)) for a in df['amount']]
return html.Div([
html.H5(filename),
html.H6(datetime.datetime.fromtimestamp(date)),
# graph
dcc.Graph(
id='example-graph',
figure={
'data': [
{'x': x, 'y': y, 'type': 'bar', 'name': 'Transactions'}
],
'layout': {
'title': 'Transactions'
}
}
),
# table
#generate_table(df)
# Use the DataTable prototype component:
# github.com/plotly/dash-table-experiments
dt.DataTable(rows=df.to_dict('records'),
row_selectable=True,
filterable=True,
sortable=True),
html.Hr(), # horizontal line
# For debugging, display the raw contents provided by the web browser
html.Div('Raw Content'),
html.Pre(contents[0:200] + '...', style={
'whiteSpace': 'pre-wrap',
'wordBreak': 'break-all'
})
])
@app.callback(Output('output-data-upload', 'children'),
[Input('upload-data', 'contents'),
Input('upload-data', 'filename'),
Input('upload-data', 'last_modified')])
def update_output(list_of_contents, list_of_names, list_of_dates):
if list_of_contents is None:
return html.Div([
'Parsed empty contents. This file may not be in a good bank statement format.'
])
try:
children = [
parse_contents(c, n, d) for c, n, d in
zip(list_of_contents, list_of_names, list_of_dates)]
return children
except Exception as e:
return html.Div([
'Error occured while parsing contents.'
])
# starts app from command line, run Flask server
if __name__ == '__main__':
app.run_server(debug=True)