Create result.py
Browse files
result.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# result.py
|
| 2 |
+
import yfinance as yf
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import pandas.api.types as ptypes
|
| 5 |
+
import datetime
|
| 6 |
+
|
| 7 |
+
# --- CSS for this module ---
|
| 8 |
+
STYLE_BLOCK = """
|
| 9 |
+
<style>
|
| 10 |
+
.styled-table {
|
| 11 |
+
border-collapse: collapse;
|
| 12 |
+
margin: 10px 0;
|
| 13 |
+
font-size: 0.9em;
|
| 14 |
+
font-family: sans-serif;
|
| 15 |
+
width: 100%;
|
| 16 |
+
box-shadow: 0 0 10px rgba(0,0,0,0.1);
|
| 17 |
+
}
|
| 18 |
+
.styled-table th, .styled-table td {
|
| 19 |
+
padding: 8px 10px;
|
| 20 |
+
border: 1px solid #ddd;
|
| 21 |
+
}
|
| 22 |
+
.styled-table tbody tr:nth-child(even) {
|
| 23 |
+
background-color: #f9f9f9;
|
| 24 |
+
}
|
| 25 |
+
.card {
|
| 26 |
+
width: 95%;
|
| 27 |
+
margin: 10px auto;
|
| 28 |
+
padding: 15px;
|
| 29 |
+
border: 1px solid #ddd;
|
| 30 |
+
border-radius: 8px;
|
| 31 |
+
background: #fafafa;
|
| 32 |
+
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
| 33 |
+
}
|
| 34 |
+
.card h2 {
|
| 35 |
+
margin-top:0;
|
| 36 |
+
}
|
| 37 |
+
</style>
|
| 38 |
+
"""
|
| 39 |
+
|
| 40 |
+
def format_large_number(num):
|
| 41 |
+
if not isinstance(num, (int, float)):
|
| 42 |
+
return num
|
| 43 |
+
sign = '-' if num < 0 else ''
|
| 44 |
+
num = abs(float(num))
|
| 45 |
+
if num >= 1_000_000_000_000:
|
| 46 |
+
return f"{sign}{num / 1_000_000_000_000:.2f} LCr"
|
| 47 |
+
elif num >= 10_000_000:
|
| 48 |
+
return f"{sign}{num / 10_000_000:.2f} Cr"
|
| 49 |
+
elif num >= 100_000:
|
| 50 |
+
return f"{sign}{num / 100_000:.2f} Lac"
|
| 51 |
+
else:
|
| 52 |
+
return f"{sign}{num:,.0f}"
|
| 53 |
+
|
| 54 |
+
def fetch_result(symbol):
|
| 55 |
+
yfsymbol = f"{symbol}.NS"
|
| 56 |
+
try:
|
| 57 |
+
ticker = yf.Ticker(yfsymbol)
|
| 58 |
+
df = ticker.financials
|
| 59 |
+
if df.empty:
|
| 60 |
+
content_html = f"<h1>No annual results available for {symbol}</h1>"
|
| 61 |
+
else:
|
| 62 |
+
# Format numeric columns
|
| 63 |
+
for col in df.columns:
|
| 64 |
+
if ptypes.is_numeric_dtype(df[col]):
|
| 65 |
+
df[col] = df[col].apply(format_large_number)
|
| 66 |
+
content_html = f"<div class='card'><h2>Annual Results</h2>{df.to_html(classes='styled-table', border=0)}</div>"
|
| 67 |
+
except Exception as e:
|
| 68 |
+
content_html = f"<h1>Error</h1><p>{str(e)}</p>"
|
| 69 |
+
|
| 70 |
+
full_html = f"""
|
| 71 |
+
<!DOCTYPE html>
|
| 72 |
+
<html>
|
| 73 |
+
<head>
|
| 74 |
+
<title>Annual Results for {symbol}</title>
|
| 75 |
+
{STYLE_BLOCK}
|
| 76 |
+
</head>
|
| 77 |
+
<body>
|
| 78 |
+
{content_html}
|
| 79 |
+
</body>
|
| 80 |
+
</html>
|
| 81 |
+
"""
|
| 82 |
+
return full_html
|