Update app.py
Browse files
app.py
CHANGED
|
@@ -29,34 +29,7 @@ s3 = boto3.client(
|
|
| 29 |
aws_secret_access_key=AWS_SECRET_KEY,
|
| 30 |
)
|
| 31 |
|
| 32 |
-
LOG_FILE = "fetch_log.csv"
|
| 33 |
-
|
| 34 |
-
def b2_file(file_path=None, bucket_name=BUCKET_NAME, upload=True, download_path=None):
|
| 35 |
-
"""
|
| 36 |
-
Universal upload/download function for Backblaze B2.
|
| 37 |
-
"""
|
| 38 |
-
try:
|
| 39 |
-
if upload:
|
| 40 |
-
if not file_path or not os.path.isfile(file_path):
|
| 41 |
-
raise ValueError("Valid local file_path must be provided for upload")
|
| 42 |
-
with open(file_path, "rb") as f:
|
| 43 |
-
data = f.read()
|
| 44 |
-
file_name = os.path.basename(file_path)
|
| 45 |
-
s3.put_object(Bucket=bucket_name, Key=file_name, Body=data)
|
| 46 |
-
return True
|
| 47 |
-
else:
|
| 48 |
-
if not file_path:
|
| 49 |
-
raise ValueError("Bucket file name must be provided for download")
|
| 50 |
-
if not download_path:
|
| 51 |
-
raise ValueError("download_path must be provided for download")
|
| 52 |
-
obj = s3.get_object(Bucket=bucket_name, Key=file_path)
|
| 53 |
-
data = obj['Body'].read()
|
| 54 |
-
with open(download_path, "wb") as f:
|
| 55 |
-
f.write(data)
|
| 56 |
-
return True
|
| 57 |
-
except Exception as e:
|
| 58 |
-
print(f"Error in B2 operation: {e}")
|
| 59 |
-
return False
|
| 60 |
|
| 61 |
# ======================================================
|
| 62 |
# Scrollable HTML wrapper
|
|
@@ -112,20 +85,41 @@ INDEX_REQ = [
|
|
| 112 |
"index_pe_pb_div", "index_total_returns"
|
| 113 |
]
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
# ======================================================
|
| 116 |
# Data Fetcher
|
| 117 |
# ======================================================
|
| 118 |
def fetch_data(mode, req_type, name, date_str):
|
| 119 |
mode = mode or "stock"
|
| 120 |
req_type = req_type.lower() if req_type else "info"
|
| 121 |
-
name = name.strip() if name else "ITC"
|
| 122 |
date_str = date_str.strip() if date_str else yesterday_str()
|
| 123 |
date_start = last_year_date(date_str)
|
| 124 |
client = "gradio"
|
| 125 |
|
| 126 |
-
#
|
| 127 |
-
# INDEX MODE
|
| 128 |
-
# =====================
|
| 129 |
if mode == "index":
|
| 130 |
if req_type == "indices":
|
| 131 |
result = build_indices_html()
|
|
@@ -164,9 +158,7 @@ def fetch_data(mode, req_type, name, date_str):
|
|
| 164 |
else:
|
| 165 |
result = wrap(f"<h3>No handler for {req_type}</h3>")
|
| 166 |
|
| 167 |
-
#
|
| 168 |
-
# STOCK MODE
|
| 169 |
-
# =====================
|
| 170 |
elif mode == "stock":
|
| 171 |
if req_type == "daily":
|
| 172 |
result = wrap(fetch_daily(name))
|
|
@@ -198,9 +190,7 @@ def fetch_data(mode, req_type, name, date_str):
|
|
| 198 |
else:
|
| 199 |
result = wrap(f"<h3>No valid mode: {mode}</h3>")
|
| 200 |
|
| 201 |
-
#
|
| 202 |
-
# Log fetch AFTER serving API
|
| 203 |
-
# =====================
|
| 204 |
try:
|
| 205 |
# Read existing log
|
| 206 |
try:
|
|
@@ -226,7 +216,6 @@ def fetch_data(mode, req_type, name, date_str):
|
|
| 226 |
df.to_csv(csv_buffer, index=False)
|
| 227 |
csv_buffer.seek(0)
|
| 228 |
s3.put_object(Bucket=BUCKET_NAME, Key=LOG_FILE, Body=csv_buffer.getvalue())
|
| 229 |
-
print("Fetch logged successfully.")
|
| 230 |
except Exception as e:
|
| 231 |
print(f"Error logging fetch: {e}")
|
| 232 |
|
|
@@ -247,9 +236,10 @@ with gr.Blocks(title="Stock / Index App") as iface:
|
|
| 247 |
|
| 248 |
output = gr.HTML(label="Output")
|
| 249 |
|
| 250 |
-
#
|
| 251 |
-
|
| 252 |
-
|
|
|
|
| 253 |
fetch_btn.click(fetch_data, inputs=[mode_input, req_type_input, name_input, date_input], outputs=output)
|
| 254 |
|
| 255 |
# ======================================================
|
|
|
|
| 29 |
aws_secret_access_key=AWS_SECRET_KEY,
|
| 30 |
)
|
| 31 |
|
| 32 |
+
LOG_FILE = "fetch_log.csv"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# ======================================================
|
| 35 |
# Scrollable HTML wrapper
|
|
|
|
| 85 |
"index_pe_pb_div", "index_total_returns"
|
| 86 |
]
|
| 87 |
|
| 88 |
+
# ======================================================
|
| 89 |
+
# Update UI on mode change (set defaults)
|
| 90 |
+
# ======================================================
|
| 91 |
+
def update_on_mode(mode):
|
| 92 |
+
if mode == "stock":
|
| 93 |
+
return (
|
| 94 |
+
gr.update(choices=STOCK_REQ, value="info"),
|
| 95 |
+
gr.update(value="ITC"),
|
| 96 |
+
gr.update(value=yesterday_str())
|
| 97 |
+
)
|
| 98 |
+
elif mode == "index":
|
| 99 |
+
return (
|
| 100 |
+
gr.update(choices=INDEX_REQ, value="indices"),
|
| 101 |
+
gr.update(value="NIFTY 50"),
|
| 102 |
+
gr.update(value=yesterday_str())
|
| 103 |
+
)
|
| 104 |
+
else:
|
| 105 |
+
return (
|
| 106 |
+
gr.update(choices=[], value=""),
|
| 107 |
+
gr.update(value=""),
|
| 108 |
+
gr.update(value="")
|
| 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 |
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 |
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:
|
|
|
|
| 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 |
|
|
|
|
| 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 |
# ======================================================
|