-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
95 lines (66 loc) · 2.87 KB
/
app.py
File metadata and controls
95 lines (66 loc) · 2.87 KB
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
import sys
import os
from flask import Flask, send_file
from flask_restful import reqparse, Api, Resource
import simplejson as json
import pyodbc
import csv
# This is a simplified example that only support GET request.
# It is meant to help you to get you started if you're new to development
# and to show how simple is using Azure SQL with Python
# A more complete example is in "app.py"
# To run this simplified sample follow the README, but instead of running "flask run"
# just run "python ./simple-app.py"
# Enjoy!
# Initialize Flask
app = Flask(__name__)
# Setup Flask Restful framework
api = Api(app)
parser = reqparse.RequestParser()
# Customer Class
class Rowinfo(Resource):
def get(self):
# Create connection to Azure SQL
conn = pyodbc.connect(os.environ['SQLAZURECONNSTR_WWIF'])
cursor = conn.cursor()
cursor.execute("SELECT BatchNumber, Location, Area, RowNumber, MRNote, BMNote, Name, GrillSize, Quantity, WeightPerBag, TotalWeight, Ploidy, CONVERT(CHAR(8), MovementDate, 112) AS MovementDate, Type, BagColor, MeshSize, SupplierName, NoPerBag, KgPerBag, RowID FROM BatchSummaryLive")
rows = [x for x in cursor]
cols = [x[0] for x in cursor.description]
rowarray_list = []
for row in rows:
myRow = {}
for prop, val in zip(cols, row):
myRow[prop] = val
rowarray_list.append(myRow)
result = json.dumps(rowarray_list)
cursor.close()
conn.close()
return result, 200
class RowinfoCSV(Resource):
def get(self):
# Create connection to Azure SQL
conn = pyodbc.connect(os.environ['SQLAZURECONNSTR_WWIF'])
cursor = conn.cursor()
cursor.execute("SELECT 2.0149100 as Latitude, 49.1779458 as Longitude, BatchNumber, Location, Area, RowNumber, MRNote, BMNote, Name, GrillSize, Quantity, WeightPerBag, TotalWeight, Ploidy, CONVERT(CHAR(8), MovementDate, 112) AS MovementDate, Type, BagColor, MeshSize, SupplierName, NoPerBag, KgPerBag, RowID FROM BatchSummaryLive")
csv_file_path = 'trac.csv'
rows = cursor.fetchall()
result = list()
column_names = list()
for i in cursor.description:
column_names.append(i[0])
result.append(column_names)
for row in rows:
result.append(row)
with open(csv_file_path, 'w', newline='') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for row in result:
csvwriter.writerow(row)
cursor.close()
conn.close()
return send_file("trac.csv", mimetype='text/css', as_attachment=True)
# Create API route for JSON & CSV
api.add_resource(Rowinfo, '/rowinfo')
api.add_resource(RowinfoCSV, '/rowinfocsv')
# Start App
if __name__ == '__main__':
app.run()