File size: 943 Bytes
916c61b
 
8bac24c
916c61b
 
8bac24c
916c61b
 
 
8bac24c
916c61b
8bac24c
916c61b
8bac24c
 
 
 
 
 
 
 
 
 
 
 
 
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
# dividend.py
import yfinance as yf
from common import make_table, wrap_html, format_large_number, html_error

def fetch_dividend(symbol):
    yfsymbol = symbol + ".NS"
    try:
        ticker = yf.Ticker(yfsymbol)
        df = ticker.dividends.to_frame('Dividend')

        if df.empty:
            return wrap_html(f"<h1>No dividend history available for {symbol}</h1>")

        df_formatted = df.copy()
        for col in df_formatted.columns:
            df_formatted[col] = df_formatted[col].apply(
                lambda x: format_large_number(x) if isinstance(x, (int, float)) else x
            )

        df_formatted.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_formatted.index]
        table_html = make_table(df_formatted)

        return wrap_html(table_html, title=f"{symbol} - Dividend History")

    except Exception as e:
        return wrap_html(html_error(f"Failed to fetch dividend history: {e}"))