rajkhanke commited on
Commit
ca0b105
·
verified ·
1 Parent(s): 1082a43

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +128 -0
  2. final_price_data.csv +0 -0
  3. templates/index.html +224 -0
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ from flask import Flask, render_template, request, jsonify
4
+ import requests
5
+ import pandas as pd
6
+ from datetime import datetime
7
+
8
+ app = Flask(__name__)
9
+ api_key = os.getenv("API_KEY")
10
+ # Function to fetch data from the API
11
+ def fetch_market_data(state=None, district=None, market=None, commodity=None):
12
+ api_key = api_key
13
+ base_url = "https://api.data.gov.in/resource/9ef84268-d588-465a-a308-a864a43d0070"
14
+ params = {
15
+ "api-key": api_key,
16
+ "format": "json",
17
+ "limit": 15000,
18
+ }
19
+
20
+ if state:
21
+ params["filters[state.keyword]"] = state
22
+ if district:
23
+ params["filters[district.keyword]"] = district
24
+ if market:
25
+ params["filters[market.keyword]"] = market
26
+ if commodity:
27
+ params["filters[commodity.keyword]"] = commodity
28
+
29
+ response = requests.get(base_url, params=params)
30
+
31
+ if response.status_code == 200:
32
+ data = response.json()
33
+ records = data.get("records", [])
34
+ df = pd.DataFrame(records)
35
+ return df
36
+ else:
37
+ return pd.DataFrame()
38
+
39
+ # Fetch initial state data
40
+ @app.route("/")
41
+ def index():
42
+ # Fetch distinct states for dropdown
43
+ market_data = fetch_market_data()
44
+ states = market_data['state'].dropna().unique().tolist()
45
+ today = datetime.today().strftime('%Y-%m-%d')
46
+ return render_template('index.html', states=states, today=today)
47
+
48
+ # Handle AJAX requests to filter market data
49
+ @app.route("/filter_data", methods=["POST"])
50
+ def filter_data():
51
+ state = request.form.get("state")
52
+ district = request.form.get("district")
53
+ market = request.form.get("market")
54
+ commodity = request.form.get("commodity")
55
+
56
+ # Fetch filtered data
57
+ filtered_data = fetch_market_data(state, district, market, commodity)
58
+
59
+ # Generate HTML for the filtered table
60
+ table_html = ""
61
+ for _, row in filtered_data.iterrows():
62
+ table_html += f"""
63
+ <tr>
64
+ <td>{row['state']}</td>
65
+ <td>{row['district']}</td>
66
+ <td>{row['market']}</td>
67
+ <td>{row['commodity']}</td>
68
+ <td>{row['variety']}</td>
69
+ <td>{row['grade']}</td>
70
+ <td>{row['arrival_date']}</td>
71
+ <td>{row['min_price']}</td>
72
+ <td>{row['max_price']}</td>
73
+ <td>{row['modal_price']}</td>
74
+ </tr>
75
+ """
76
+
77
+ # Get top 5 cheapest crops by modal price
78
+ cheapest_crops = filtered_data.sort_values("modal_price", ascending=True).head(5)
79
+ cheapest_html = ""
80
+ for _, row in cheapest_crops.iterrows():
81
+ cheapest_html += f"""
82
+ <tr>
83
+ <td>{row['commodity']}</td>
84
+ <td>{row['modal_price']}</td>
85
+ </tr>
86
+ """
87
+
88
+ # Get top 5 costliest crops by modal price
89
+ costliest_crops = filtered_data.sort_values("modal_price", ascending=False).head(5)
90
+ costliest_html = ""
91
+ for _, row in costliest_crops.iterrows():
92
+ costliest_html += f"""
93
+ <tr>
94
+ <td>{row['commodity']}</td>
95
+ <td>{row['modal_price']}</td>
96
+ </tr>
97
+ """
98
+
99
+ return jsonify({
100
+ "market_html": table_html,
101
+ "cheapest_html": cheapest_html,
102
+ "costliest_html": costliest_html
103
+ })
104
+
105
+ # Handle AJAX requests for dropdown filtering
106
+ @app.route("/get_districts", methods=["POST"])
107
+ def get_districts():
108
+ state = request.form.get("state")
109
+ market_data = fetch_market_data(state=state)
110
+ districts = market_data["district"].dropna().unique().tolist()
111
+ return jsonify(districts)
112
+
113
+ @app.route("/get_markets", methods=["POST"])
114
+ def get_markets():
115
+ district = request.form.get("district")
116
+ market_data = fetch_market_data(district=district)
117
+ markets = market_data["market"].dropna().unique().tolist()
118
+ return jsonify(markets)
119
+
120
+ @app.route("/get_commodities", methods=["POST"])
121
+ def get_commodities():
122
+ market = request.form.get("market")
123
+ market_data = fetch_market_data(market=market)
124
+ commodities = market_data["commodity"].dropna().unique().tolist()
125
+ return jsonify(commodities)
126
+
127
+ if __name__ == "__main__":
128
+ app.run(debug=True)
final_price_data.csv ADDED
The diff for this file is too large to render. See raw diff
 
templates/index.html ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Crop Financial Market Analysis & Prediction</title>
7
+ <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
8
+ <style>
9
+ body {
10
+ background-color: #f9f9f9;
11
+ }
12
+ .container {
13
+ margin-top: 30px;
14
+ }
15
+ h1 {
16
+ color: #4CAF50;
17
+ text-align: center;
18
+ font-weight: bold;
19
+ }
20
+ .form-row {
21
+ margin-bottom: 20px;
22
+ }
23
+ .table-container {
24
+ border: 1px solid #4CAF50;
25
+ border-radius: 8px;
26
+ padding: 15px;
27
+ background-color: #fff;
28
+ max-height: 400px;
29
+ overflow-y: auto;
30
+ width: 100%;
31
+ }
32
+ .table-container table {
33
+ width: 100%;
34
+ margin-bottom: 0;
35
+ }
36
+ .result-section {
37
+ margin-top: 30px;
38
+ }
39
+ .bordered-table {
40
+ border: 1px solid #4CAF50;
41
+ background-color: #fff;
42
+ }
43
+ .bold-header {
44
+ font-weight: bold;
45
+ color: #333;
46
+ }
47
+ th {
48
+ background-color: #4CAF50;
49
+ color: white;
50
+ }
51
+ .chart-container {
52
+ display: flex;
53
+ justify-content: space-between;
54
+ margin-top: 20px;
55
+ }
56
+ .chart {
57
+ width: 48%;
58
+ }
59
+ </style>
60
+ </head>
61
+ <body>
62
+ <div class="container">
63
+ <h1>Crop Financial Market Analysis & Prediction</h1>
64
+ <form id="filterForm">
65
+ <div class="form-row">
66
+ <div class="form-group col-md-3">
67
+ <label for="state">State</label>
68
+ <select class="form-control" id="state" name="state">
69
+ <option value="">Select State</option>
70
+ {% for state in states %}
71
+ <option value="{{ state }}">{{ state }}</option>
72
+ {% endfor %}
73
+ </select>
74
+ </div>
75
+ <div class="form-group col-md-3">
76
+ <label for="district">District</label>
77
+ <select class="form-control" id="district" name="district">
78
+ <option value="">Select District</option>
79
+ </select>
80
+ </div>
81
+ <div class="form-group col-md-3">
82
+ <label for="market">Market</label>
83
+ <select class="form-control" id="market" name="market">
84
+ <option value="">Select Market</option>
85
+ </select>
86
+ </div>
87
+ <div class="form-group col-md-3">
88
+ <label for="commodity">Commodity</label>
89
+ <select class="form-control" id="commodity" name="commodity">
90
+ <option value="">Select Commodity</option>
91
+ </select>
92
+ </div>
93
+ </div>
94
+ </form>
95
+
96
+ <div class="result-section">
97
+ <div class="table-container">
98
+ <h3 class="bold-header">Market Data as of {{ today }}</h3>
99
+ <div class="bordered-table">
100
+ <table class="table table-bordered table-hover">
101
+ <thead>
102
+ <tr>
103
+ <th>State</th>
104
+ <th>District</th>
105
+ <th>Market</th>
106
+ <th>Commodity</th>
107
+ <th>Variety</th>
108
+ <th>Grade</th>
109
+ <th>Arrival Date</th>
110
+ <th>Min Price</th>
111
+ <th>Max Price</th>
112
+ <th>Modal Price</th>
113
+ </tr>
114
+ </thead>
115
+ <tbody id="marketData">
116
+ <!-- Market data will be populated here via AJAX -->
117
+ </tbody>
118
+ </table>
119
+ </div>
120
+ </div>
121
+
122
+ <div class="result-section">
123
+ <h3 class="bold-header">Top 5 Cheapest Crops</h3>
124
+ <div class="bordered-table">
125
+ <table class="table table-bordered table-hover">
126
+ <thead>
127
+ <tr>
128
+ <th>Commodity</th>
129
+ <th>Modal Price</th>
130
+ </tr>
131
+ </thead>
132
+ <tbody id="cheapestCrops">
133
+ <!-- Cheapest crops data will be populated here -->
134
+ </tbody>
135
+ </table>
136
+ </div>
137
+ </div>
138
+
139
+ <div class="result-section">
140
+ <h3 class="bold-header">Top 5 Costliest Crops</h3>
141
+ <div class="bordered-table">
142
+ <table class="table table-bordered table-hover">
143
+ <thead>
144
+ <tr>
145
+ <th>Commodity</th>
146
+ <th>Modal Price</th>
147
+ </tr>
148
+ </thead>
149
+ <tbody id="costliestCrops">
150
+ <!-- Costliest crops data will be populated here -->
151
+ </tbody>
152
+ </table>
153
+ </div>
154
+ </div>
155
+ </div>
156
+ </div>
157
+
158
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
159
+ <script>
160
+ // Fetch districts based on state selection
161
+ $('#state').change(function() {
162
+ let state = $(this).val();
163
+ $.ajax({
164
+ url: '/get_districts',
165
+ method: 'POST',
166
+ data: { state: state },
167
+ success: function(districts) {
168
+ $('#district').empty().append('<option value="">Select District</option>');
169
+ districts.forEach(function(district) {
170
+ $('#district').append('<option value="' + district + '">' + district + '</option>');
171
+ });
172
+ }
173
+ });
174
+ });
175
+
176
+ // Fetch markets based on district selection
177
+ $('#district').change(function() {
178
+ let district = $(this).val();
179
+ $.ajax({
180
+ url: '/get_markets',
181
+ method: 'POST',
182
+ data: { district: district },
183
+ success: function(markets) {
184
+ $('#market').empty().append('<option value="">Select Market</option>');
185
+ markets.forEach(function(market) {
186
+ $('#market').append('<option value="' + market + '">' + market + '</option>');
187
+ });
188
+ }
189
+ });
190
+ });
191
+
192
+ // Fetch commodities based on market selection
193
+ $('#market').change(function() {
194
+ let market = $(this).val();
195
+ $.ajax({
196
+ url: '/get_commodities',
197
+ method: 'POST',
198
+ data: { market: market },
199
+ success: function(commodities) {
200
+ $('#commodity').empty().append('<option value="">Select Commodity</option>');
201
+ commodities.forEach(function(commodity) {
202
+ $('#commodity').append('<option value="' + commodity + '">' + commodity + '</option>');
203
+ });
204
+ }
205
+ });
206
+ });
207
+
208
+ // Filter data and update table upon form change
209
+ $('#filterForm select').change(function() {
210
+ let formData = $('#filterForm').serialize();
211
+ $.ajax({
212
+ url: '/filter_data',
213
+ method: 'POST',
214
+ data: formData,
215
+ success: function(data) {
216
+ $('#marketData').html(data.market_html);
217
+ $('#cheapestCrops').html(data.cheapest_html);
218
+ $('#costliestCrops').html(data.costliest_html);
219
+ }
220
+ });
221
+ });
222
+ </script>
223
+ </body>
224
+ </html>