eshan6704 commited on
Commit
4d62e6d
·
verified ·
1 Parent(s): 467d84a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -17
app.py CHANGED
@@ -12,7 +12,6 @@ import datetime
12
  from io import BytesIO
13
  from datetime import datetime
14
  import boto3
15
- import os
16
 
17
  # ======================================================
18
  # Backblaze B2 Setup
@@ -86,7 +85,7 @@ INDEX_REQ = [
86
  ]
87
 
88
  # ======================================================
89
- # Update UI on mode change (set defaults)
90
  # ======================================================
91
  def update_on_mode(mode):
92
  if mode == "stock":
@@ -109,17 +108,19 @@ def update_on_mode(mode):
109
  )
110
 
111
  # ======================================================
112
- # Data Fetcher
113
  # ======================================================
114
  def fetch_data(mode, req_type, name, date_str):
115
- mode = mode or "stock"
116
- req_type = req_type.lower() if req_type else "info"
117
- name = name.strip() if name else ("ITC" if mode == "stock" else "NIFTY 50")
118
- date_str = date_str.strip() if date_str else yesterday_str()
 
119
  date_start = last_year_date(date_str)
120
- client = "gradio"
121
 
122
- # ========== INDEX ==========
 
 
123
  if mode == "index":
124
  if req_type == "indices":
125
  result = build_indices_html()
@@ -158,7 +159,6 @@ def fetch_data(mode, req_type, name, date_str):
158
  else:
159
  result = wrap(f"<h3>No handler for {req_type}</h3>")
160
 
161
- # ========== STOCK ==========
162
  elif mode == "stock":
163
  if req_type == "daily":
164
  result = wrap(fetch_daily(name))
@@ -190,9 +190,11 @@ def fetch_data(mode, req_type, name, date_str):
190
  else:
191
  result = wrap(f"<h3>No valid mode: {mode}</h3>")
192
 
193
- # ========== Log fetch AFTER serving API ==========
 
 
194
  try:
195
- # Read existing log
196
  try:
197
  obj = s3.get_object(Bucket=BUCKET_NAME, Key=LOG_FILE)
198
  data = obj['Body'].read()
@@ -204,7 +206,7 @@ def fetch_data(mode, req_type, name, date_str):
204
  df = df.append({
205
  "Sr": len(df)+1,
206
  "Datetime": datetime.now().strftime("%d-%m-%Y %H:%M:%S"),
207
- "Client": client,
208
  "Mode": mode,
209
  "Req_Type": req_type,
210
  "Name": name,
@@ -216,30 +218,36 @@ def fetch_data(mode, req_type, name, date_str):
216
  df.to_csv(csv_buffer, index=False)
217
  csv_buffer.seek(0)
218
  s3.put_object(Bucket=BUCKET_NAME, Key=LOG_FILE, Body=csv_buffer.getvalue())
 
219
  except Exception as e:
220
  print(f"Error logging fetch: {e}")
221
 
 
222
  return result
223
 
224
  # ======================================================
225
  # UI
226
  # ======================================================
227
  with gr.Blocks(title="Stock / Index App") as iface:
 
228
  gr.Markdown("### **Stock / Index Data Fetcher**")
229
 
230
  with gr.Row():
231
  mode_input = gr.Radio(["stock", "index"], label="Mode", value="stock", scale=1)
232
- name_input = gr.Textbox(label="Symbol / Index Name", placeholder="Enter symbol or index", scale=2)
233
- req_type_input = gr.Dropdown(label="Request Type", choices=STOCK_REQ, value="info", allow_custom_value=True, scale=2)
234
  date_input = gr.Textbox(label="Date (DD-MM-YYYY)", placeholder="Leave empty = yesterday", scale=1)
235
  fetch_btn = gr.Button("Fetch", scale=1)
236
 
237
  output = gr.HTML(label="Output")
238
 
239
- # ======= Mode change updates defaults only =======
240
  mode_input.change(update_on_mode, inputs=mode_input, outputs=[req_type_input, name_input, date_input])
241
 
242
- # ======= Only Fetch triggers fetch_data ==========
 
 
 
243
  fetch_btn.click(fetch_data, inputs=[mode_input, req_type_input, name_input, date_input], outputs=output)
244
 
245
  # ======================================================
 
12
  from io import BytesIO
13
  from datetime import datetime
14
  import boto3
 
15
 
16
  # ======================================================
17
  # Backblaze B2 Setup
 
85
  ]
86
 
87
  # ======================================================
88
+ # Update UI based on mode
89
  # ======================================================
90
  def update_on_mode(mode):
91
  if mode == "stock":
 
108
  )
109
 
110
  # ======================================================
111
+ # Data Fetcher with log update at the end
112
  # ======================================================
113
  def fetch_data(mode, req_type, name, date_str):
114
+ req_type = req_type.lower()
115
+ name = name.strip()
116
+ date_str = date_str.strip()
117
+ if not date_str:
118
+ date_str = yesterday_str()
119
  date_start = last_year_date(date_str)
 
120
 
121
+ # ==========================
122
+ # Fetch the requested data
123
+ # ==========================
124
  if mode == "index":
125
  if req_type == "indices":
126
  result = build_indices_html()
 
159
  else:
160
  result = wrap(f"<h3>No handler for {req_type}</h3>")
161
 
 
162
  elif mode == "stock":
163
  if req_type == "daily":
164
  result = wrap(fetch_daily(name))
 
190
  else:
191
  result = wrap(f"<h3>No valid mode: {mode}</h3>")
192
 
193
+ # ==========================
194
+ # Update fetch log at the END
195
+ # ==========================
196
  try:
197
+ # Read existing log from B2
198
  try:
199
  obj = s3.get_object(Bucket=BUCKET_NAME, Key=LOG_FILE)
200
  data = obj['Body'].read()
 
206
  df = df.append({
207
  "Sr": len(df)+1,
208
  "Datetime": datetime.now().strftime("%d-%m-%Y %H:%M:%S"),
209
+ "Client": "gradio",
210
  "Mode": mode,
211
  "Req_Type": req_type,
212
  "Name": name,
 
218
  df.to_csv(csv_buffer, index=False)
219
  csv_buffer.seek(0)
220
  s3.put_object(Bucket=BUCKET_NAME, Key=LOG_FILE, Body=csv_buffer.getvalue())
221
+
222
  except Exception as e:
223
  print(f"Error logging fetch: {e}")
224
 
225
+ # Return the fetched result to Gradio
226
  return result
227
 
228
  # ======================================================
229
  # UI
230
  # ======================================================
231
  with gr.Blocks(title="Stock / Index App") as iface:
232
+
233
  gr.Markdown("### **Stock / Index Data Fetcher**")
234
 
235
  with gr.Row():
236
  mode_input = gr.Radio(["stock", "index"], label="Mode", value="stock", scale=1)
237
+ name_input = gr.Textbox(label="Symbol / Index Name", scale=2)
238
+ req_type_input = gr.Dropdown(label="Request Type", allow_custom_value=True, scale=2)
239
  date_input = gr.Textbox(label="Date (DD-MM-YYYY)", placeholder="Leave empty = yesterday", scale=1)
240
  fetch_btn = gr.Button("Fetch", scale=1)
241
 
242
  output = gr.HTML(label="Output")
243
 
244
+ # Mode change auto defaults
245
  mode_input.change(update_on_mode, inputs=mode_input, outputs=[req_type_input, name_input, date_input])
246
 
247
+ # Initial load defaults
248
+ iface.load(update_on_mode, inputs=mode_input, outputs=[req_type_input, name_input, date_input])
249
+
250
+ # Fetch
251
  fetch_btn.click(fetch_data, inputs=[mode_input, req_type_input, name_input, date_input], outputs=output)
252
 
253
  # ======================================================