File size: 1,981 Bytes
8fbc680
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# balance.py
import yfinance as yf
import pandas as pd
import pandas.api.types as ptypes
import datetime

# --- CSS for this module ---
STYLE_BLOCK = """
<style>
.styled-table {
  border-collapse: collapse;
  margin: 10px 0;
  font-size: 0.9em;
  font-family: sans-serif;
  width: 100%;
  box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.styled-table th, .styled-table td {
  padding: 8px 10px;
  border: 1px solid #ddd;
}
.styled-table tbody tr:nth-child(even) {
  background-color: #f9f9f9;
}
.card {
  width: 95%;
  margin: 10px auto;
  padding: 15px;
  border: 1px solid #ddd;
  border-radius: 8px;
  background: #fafafa;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.card h2 {
  margin-top:0;
}
</style>
"""

def format_large_number(num):
    if not isinstance(num, (int, float)):
        return num
    sign = '-' if num < 0 else ''
    num = abs(float(num))
    if num >= 1_000_000_000_000:
        return f"{sign}{num / 1_000_000_000_000:.2f} LCr"
    elif num >= 10_000_000:
        return f"{sign}{num / 10_000_000:.2f} Cr"
    elif num >= 100_000:
        return f"{sign}{num / 100_000:.2f} Lac"
    else:
        return f"{sign}{num:,.0f}"

def fetch_balance(symbol):
    yfsymbol = f"{symbol}.NS"
    try:
        ticker = yf.Ticker(yfsymbol)
        df = ticker.balance_sheet
        if df.empty:
            content_html = f"<h1>No balance sheet available for {symbol}</h1>"
        else:
            # Format numeric columns
            for col in df.columns:
                if ptypes.is_numeric_dtype(df[col]):
                    df[col] = df[col].apply(format_large_number)
            content_html = f"<div class='card'><h2>Balance Sheet</h2>{df.to_html(classes='styled-table', border=0)}</div>"
    except Exception as e:
        content_html = f"<h1>Error</h1><p>{str(e)}</p>"

    full_html = f"""
<!DOCTYPE html>
<html>
<head>
    <title>Balance Sheet for {symbol}</title>
    {STYLE_BLOCK}
</head>
<body>
    {content_html}
</body>
</html>
"""
    return full_html