eshan6704 commited on
Commit
8bac24c
·
verified ·
1 Parent(s): 660dee6

Update dividend.py

Browse files
Files changed (1) hide show
  1. dividend.py +17 -73
dividend.py CHANGED
@@ -1,82 +1,26 @@
1
  # dividend.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_dividend(symbol):
55
- yfsymbol = f"{symbol}.NS"
56
  try:
57
  ticker = yf.Ticker(yfsymbol)
58
  df = ticker.dividends.to_frame('Dividend')
 
59
  if df.empty:
60
- content_html = f"<h1>No dividend history 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>Dividend History</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>Dividend History for {symbol}</title>
75
- {STYLE_BLOCK}
76
- </head>
77
- <body>
78
- {content_html}
79
- </body>
80
- </html>
81
- """
82
- return full_html
 
1
  # dividend.py
2
  import yfinance as yf
3
+ from common import make_table, wrap_html, format_large_number, html_error
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  def fetch_dividend(symbol):
6
+ yfsymbol = symbol + ".NS"
7
  try:
8
  ticker = yf.Ticker(yfsymbol)
9
  df = ticker.dividends.to_frame('Dividend')
10
+
11
  if df.empty:
12
+ return wrap_html(f"<h1>No dividend history available for {symbol}</h1>")
 
 
 
 
 
 
 
 
13
 
14
+ df_formatted = df.copy()
15
+ for col in df_formatted.columns:
16
+ df_formatted[col] = df_formatted[col].apply(
17
+ lambda x: format_large_number(x) if isinstance(x, (int, float)) else x
18
+ )
19
+
20
+ df_formatted.index = [str(i.date()) if hasattr(i, "date") else str(i) for i in df_formatted.index]
21
+ table_html = make_table(df_formatted)
22
+
23
+ return wrap_html(table_html, title=f"{symbol} - Dividend History")
24
+
25
+ except Exception as e:
26
+ return wrap_html(html_error(f"Failed to fetch dividend history: {e}"))