-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
133 lines (96 loc) · 3.01 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
from flask import Flask, jsonify
from flask_cors import CORS
import csv
import json
app = Flask(__name__)
CORS(app)
@app.route("/api/data", methods=["GET"])
def get_data():
data = {"message": "Hello This is Flask API"}
return jsonify(data)
@app.route("/get/data", methods=["GET"])
def get_api_data():
csv_filename = (
"data//CommodityData//Yam (Ratalu)//Gujarat//Ahmedabad//Ahmedabad.csv"
)
selected_columns = [
"Min Price (Rs./Quintal)",
"Max Price (Rs./Quintal)",
"Modal Price (Rs./Quintal)",
"Reported Date",
]
data = []
with open(csv_filename, "r") as csvfile:
csv_reader = csv.DictReader(csvfile)
for row in csv_reader:
selected_data = {column: row[column] for column in selected_columns}
data.append(selected_data)
return data
@app.route("/get_commodities", methods=["GET"])
def get_commodities():
commodities = []
with open("./commodities.json", "r") as file:
data = json.load(file)
for a in data:
id = a["commodity_id"]
c = a["commodity"]
commodities.append({"id": id, "commodity": c})
return commodities
@app.route("/get_states", methods=["GET"])
def get_states():
states = []
with open("./markets.json", "r") as file:
data = json.load(file)
for a in data:
s = a["state"]
states.append(s)
return states
@app.route("/get_districts/<state>", methods=["GET"])
def get_districts(state):
states = []
districts = []
with open("./markets.json", "r") as file:
data = json.load(file)
for a in data:
s = a["state"]
states.append(s)
if s == state:
for b in a["districts"]:
districts.append(b["district"])
return districts
@app.route("/get_markets/<state>/<district>", methods=["GET"])
def get_markets(state, district):
states = []
markets = []
with open("./markets.json", "r") as file:
data = json.load(file)
for a in data:
s = a["state"]
states.append(s)
if s == state:
for b in a["districts"]:
d = b["district"]
if d == district:
for c in b["markets"]:
markets.append(c["market"])
return markets
@app.route("/getData/<commodity>/<state>/<district>/<mandi>", methods=["get", "POST"])
def getData(commodity, state, district, mandi):
csv_filename = f"data//CommodityData//{commodity}//{state}//{district}//{mandi}.csv"
print(csv_filename)
selected_columns = [
"Min Price (Rs./Quintal)",
"Max Price (Rs./Quintal)",
"Modal Price (Rs./Quintal)",
"Reported Date",
]
data = []
with open(csv_filename, "r") as csvfile:
csv_reader = csv.DictReader(csvfile)
for row in csv_reader:
selected_data = {column: row[column] for column in selected_columns}
data.append(selected_data)
if data:
return data
else:
return ""