Update qresult.py
Browse files- qresult.py +20 -89
qresult.py
CHANGED
|
@@ -1,104 +1,35 @@
|
|
| 1 |
# qresult.py
|
| 2 |
import yfinance as yf
|
| 3 |
import pandas as pd
|
| 4 |
-
from common import
|
| 5 |
-
format_large_number,
|
| 6 |
-
format_timestamp_to_date,
|
| 7 |
-
format_number,
|
| 8 |
-
wrap_html,
|
| 9 |
-
STYLE_BLOCK
|
| 10 |
-
)
|
| 11 |
|
| 12 |
def fetch_qresult(symbol):
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
try:
|
| 16 |
ticker = yf.Ticker(yfsymbol)
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
y = ticker.financials
|
| 21 |
-
bs = ticker.balance_sheet
|
| 22 |
-
cf = ticker.cashflow
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
y_t = y.T if y is not None else None
|
| 33 |
-
bs_t = bs.T if bs is not None else None
|
| 34 |
-
cf_t = cf.T if cf is not None else None
|
| 35 |
-
|
| 36 |
-
# ------------------------
|
| 37 |
-
def format_df(df):
|
| 38 |
-
df_formatted = df.copy()
|
| 39 |
-
|
| 40 |
-
# Format numeric columns
|
| 41 |
-
for col in df_formatted.columns:
|
| 42 |
-
df_formatted[col] = df_formatted[col].apply(
|
| 43 |
-
lambda x: format_number(x) if isinstance(x, (int, float)) else x
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
# Fix index (assume index is datetime-like)
|
| 47 |
-
df_formatted.index = [
|
| 48 |
-
format_timestamp_to_date(i.timestamp()) if hasattr(i, "timestamp") else str(i)
|
| 49 |
-
for i in df_formatted.index
|
| 50 |
-
]
|
| 51 |
-
|
| 52 |
-
return df_formatted
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
q_html = format_df(q_t).to_html(classes="styled-table", border=0)
|
| 56 |
-
|
| 57 |
-
y_html = ""
|
| 58 |
-
if y_t is not None:
|
| 59 |
-
y_html = format_df(y_t).to_html(classes="styled-table", border=0)
|
| 60 |
-
|
| 61 |
-
bs_html = ""
|
| 62 |
-
if bs_t is not None:
|
| 63 |
-
bs_html = format_df(bs_t).to_html(classes="styled-table", border=0)
|
| 64 |
-
|
| 65 |
-
cf_html = ""
|
| 66 |
-
if cf_t is not None:
|
| 67 |
-
cf_html = format_df(cf_t).to_html(classes="styled-table", border=0)
|
| 68 |
-
|
| 69 |
-
# Build sections
|
| 70 |
-
content = f"""
|
| 71 |
-
<div class='big-box'>
|
| 72 |
-
<h2>Quarterly Results</h2>
|
| 73 |
-
{q_html}
|
| 74 |
-
</div>
|
| 75 |
-
"""
|
| 76 |
-
|
| 77 |
-
if y_html:
|
| 78 |
-
content += f"""
|
| 79 |
-
<div class='big-box'>
|
| 80 |
-
<h2>Annual Results</h2>
|
| 81 |
-
{y_html}
|
| 82 |
-
</div>
|
| 83 |
-
"""
|
| 84 |
-
|
| 85 |
-
if bs_html:
|
| 86 |
-
content += f"""
|
| 87 |
-
<div class='big-box'>
|
| 88 |
-
<h2>Balance Sheet</h2>
|
| 89 |
-
{bs_html}
|
| 90 |
-
</div>
|
| 91 |
-
"""
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
<div class='big-box'>
|
| 96 |
-
<h2>Cash Flow</h2>
|
| 97 |
-
{cf_html}
|
| 98 |
-
</div>
|
| 99 |
-
"""
|
| 100 |
|
| 101 |
-
|
|
|
|
| 102 |
|
| 103 |
except Exception as e:
|
| 104 |
-
return wrap_html(
|
|
|
|
| 1 |
# qresult.py
|
| 2 |
import yfinance as yf
|
| 3 |
import pandas as pd
|
| 4 |
+
from common import make_table, wrap_html, format_large_number, html_error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def fetch_qresult(symbol):
|
| 7 |
+
"""
|
| 8 |
+
Fetch quarterly financials for a stock symbol and return HTML
|
| 9 |
+
"""
|
| 10 |
+
yfsymbol = symbol + ".NS"
|
| 11 |
try:
|
| 12 |
ticker = yf.Ticker(yfsymbol)
|
| 13 |
+
df = ticker.quarterly_financials
|
| 14 |
|
| 15 |
+
if df.empty:
|
| 16 |
+
return wrap_html(f"<h1>No quarterly results available for {symbol}</h1>")
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Format numeric columns
|
| 19 |
+
df_formatted = df.copy()
|
| 20 |
+
for col in df_formatted.columns:
|
| 21 |
+
df_formatted[col] = df_formatted[col].apply(
|
| 22 |
+
lambda x: format_large_number(x) if isinstance(x, (int, float)) else x
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# Format index (dates)
|
| 26 |
+
df_formatted.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_formatted.index]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# Convert to pretty HTML table
|
| 29 |
+
table_html = make_table(df_formatted)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# Wrap into full HTML page
|
| 32 |
+
return wrap_html(table_html, title=f"{symbol} - Quarterly Results")
|
| 33 |
|
| 34 |
except Exception as e:
|
| 35 |
+
return wrap_html(html_error(f"Failed to fetch quarterly results: {e}"))
|