-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
103 lines (84 loc) · 2.67 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
import os
import json
import tools
import fileops
import inegi
from flask import Flask, render_template, request, redirect, url_for, Response
app = Flask(__name__)
def add_cors_headers(response):
response.headers['Access-Control-Allow-Origin'] = '*'
if request.method == 'OPTIONS':
response.headers['Access-Control-Allow-Methods'] = 'DELETE, \
GET,POST, PUT'
headers = request.headers.get('Access-Control-Request-Headers')
if headers:
response.headers['Access-Control-Allow-Headers'] = headers
return response
app.after_request(add_cors_headers)
'''
Routing for your application.
'''
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
file = request.files['file']
filename = fileops.save_file(file)
excel_dict = fileops.read_excel(filename)
output_dict = inegi.bulk_coords_convert(excel_dict)
output_csv = fileops.return_csv(output_dict)
return Response(response=output_csv, status=200, mimetype="text/csv")
return '''
<!doctype html>
<title>Sube un excel como este:.</title>
<h1>Sube un archivo</h1>
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>
'''
@app.route('/about/')
def about():
'''
Function: about(). Renders the website's about page.
Route: <API>/about
Method: GET
'''
return render_template('about.html')
@app.route('/query')
def query():
'''
Function:
Route: <API>/
Method: GET
Input parameters:
Usage: /query?q=<String>
Example:/query?q=Garza%20Sada
'''
query = request.args.get('q')
output = inegi.call_inegi(query)
return Response(response=json.dumps(output),
status=200, mimetype="application/json")
@app.route('/intersection')
def intersection():
'''
Function: intersection()
Route: <API>/intersection
Method: GET
Input parameters: calle1, calle2, ciudad
Usage: /intersection?calle1=<String>&calle2=<String>&ciudad=<String>
Example:/intersection?calle1=Garza%20Sada&calle2=Acapulco&ciudad=Monterrey
'''
street1 = request.args.get('calle1')
street2 = request.args.get('calle2')
city = request.args.get('ciudad')
output = inegi.crossing(street1, street2, city)
return Response(response=json.dumps(output),
status=200, mimetype="application/json")
@app.errorhandler(404)
def page_not_found(error):
'''
Function: page_not_found(). Displays our famous HTTP404 error.
'''
return render_template('404.html'), 404
if __name__ == '__main__':
app.run(debug=True)