Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
206 lines (183 loc) · 6.87 KB

File metadata and controls

206 lines (183 loc) · 6.87 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
from flask import Flask, make_response, request, render_template, jsonify
from flask_dynamo import Dynamo
from flask_bootstrap import Bootstrap
from flask import Markup
import flask
import io
import csv
import pandas as pd
import numpy as np
import os
from flask import send_file
import boto3
from boto3.dynamodb.conditions import Key, Attr
from match import guess_match as gm
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
table = dynamodb.Table('users')
def print_head(df):
"""converts pandas 'head' to html; returns html"""
head = df.head().to_html()
return Markup(head)
def add_to_db(df):
recs = df.to_dict('records')
for rec in recs:
table.put_item(Item = rec)
print('all items added to db')
return None
def query_from_db(k, v):
response = table.scan(
FilterExpression=Attr(k).eq(v)
)
response_dict_lst = response['Items']
top = '<table class="table table-hover"> <thead> <tr>'
col = '<th scope="col">{}</th>'
end_head = '</tr> </thead> <tbody>'
row_begin = '<tr>'
row = '<td>{}</td>'
row_end = '</tr>'
bottom = '</tbody></table>'
cols = ""
middle = ""
for field, value in response_dict_lst[0].items():
cols += col.format(field)
for obj in response_dict_lst:
middle += row_begin
for k, v in obj.items():
middle += row.format(v)
middle += row_end
the_table = top+cols+end_head+middle+bottom
print('the_table:', the_table)
return the_table
def which_fields_matched(df):
result_from_match_py = gm(df)
# result_from_match_py = {'db_email': 'e-mail', 'db_first': 'first_name', 'db_last': 'last_name', 'db_state': 'us_state'}
new_selected_lst = []
selected_lst = ['db_first', 'db_last', 'db_state', 'db_email']
for x in selected_lst:
new_selected_lst.append(result_from_match_py[x])
return new_selected_lst
app = Flask(__name__)
@app.route('/find_users', methods=["POST"])
def find_users():
print('find_users called')
for k, v in request.form.items():
query_key = k
query_value = v
print('k:', k)
print('v:', v)
result_obj = query_from_db(query_key, query_value)
print('this is the result_obj: ', result_obj)
return flask.jsonify(query_result = result_obj)
@app.route('/')
def index():
return flask.render_template('index.html')
@app.route('/uploadcsv', methods=["POST"])
def uploadcsv():
print('type (request)', type (request.files['data_file']))
f = request.files['data_file']
if not f:
return "No file"
df = pd.read_csv(f)
print('df.head(): ', df.head())
df.to_csv('../data/df.csv', index=False)
initial_head = print_head(df)
columns = list(df.columns)
row_one = df.iloc[0]
matched_fields = which_fields_matched(df)
print('matched_fields', matched_fields)
# ex_and_match_select = [row_one, matched_fields]
cols_examples = dict(zip(columns, row_one))
for k, v in cols_examples.items():
if k in matched_fields:
blanks = ['', '', '', '']
blanks[matched_fields.index(k)] = 'selected'
print('blanks:', blanks)
cols_examples[k] = v, [blanks, 'active']
blanks = ['', '', '', '']
else:
cols_examples[k] = v, [blanks, '']
# match_selected = which_selected()
# print('match_selected', match_selected)
# ex_and_match_select = [row_one, match_selected]
# cols_examples = dict(zip(columns, ex_and_match_select))
print('cols_examples:', cols_examples)
return flask.render_template(
'upload.html',
firsthead = initial_head,
cols_examples = cols_examples
)
@app.route('/demo', methods=["GET"])
def demo():
f = '../data/member_list.csv'
if not f:
return "No file"
df = pd.read_csv(f)
print('df.head(): ', df.head())
df.to_csv('../data/df.csv', index=False)
initial_head = print_head(df)
columns = list(df.columns)
row_one = df.iloc[0]
matched_fields = which_fields_matched(df)
print('matched_fields', matched_fields)
# ex_and_match_select = [row_one, matched_fields]
cols_examples = dict(zip(columns, row_one))
for k, v in cols_examples.items():
if k in matched_fields:
blanks = ['', '', '', '']
blanks[matched_fields.index(k)] = 'selected'
print('blanks:', blanks)
cols_examples[k] = v, [blanks, 'active']
blanks = ['', '', '', '']
else:
cols_examples[k] = v, [blanks, '']
# cols_examples = dict(zip(columns, row_one))
print('cols_examples:', cols_examples)
print('columns: ', columns)
return flask.render_template(
'upload.html',
firsthead = initial_head,
cols_examples = cols_examples
)
@app.route('/match_cols', methods=["POST", "GET"])
def match_cols():
if request.method == 'POST':
df = pd.read_csv('../data/df.csv')
db_fields = ['db_first', 'db_last', 'db_state', 'db_email']
cols_to_keep = []
all_cols = list(df.columns)
field_match_dict = {}
form_results = request.form
print('input from form (match cols): ', form_results)
orgUserId = form_results['orguserid']
for k, v in form_results.items():
if v in db_fields:
field_match_dict[k] = v
cols_to_keep.append(k)
print('cols_to_keep: ', cols_to_keep)
cols_to_drop = set(all_cols) - set(cols_to_keep)
print('cols_to_drop:', cols_to_drop)
df_reduced_cols = df.drop(cols_to_drop, axis=1)
for old, new in field_match_dict.items():
df_reduced_cols.rename(columns={old: new}, inplace=True)
if set(db_fields) != set(df_reduced_cols.columns):
head_new_csv = Markup('<script type="text/javascript">alert ("Fields are not correctly matched - please try again")</script>')
else:
df_reduced_cols.insert(0, 'db_userid', [orgUserId+str(i) for i in range(0, len(df_reduced_cols))])
head_new_csv = print_head(df_reduced_cols)
df_reduced_cols.to_csv('../data/ready_to_import.csv')
print('head_new_csv: ', head_new_csv)
add_to_db(df_reduced_cols)
return flask.jsonify(matching_tbl = head_new_csv)
elif request.method=='GET':
return "This is a GET method"
else:
return("ok")
@app.route('/downloadcsv')
def downloadcsv():
return send_file('../data/ready_to_import.csv',
mimetype='text/csv',
attachment_filename='ready_to_import.csv',
as_attachment=True)
if __name__ == "__main__":
Bootstrap(app)
app.run(host='0.0.0.0', port=8080, debug=True, use_reloader=False)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.