eshan6704 commited on
Commit
1b7b6e5
·
verified ·
1 Parent(s): 7ce3177

Update qresult.py

Browse files
Files changed (1) hide show
  1. qresult.py +90 -73
qresult.py CHANGED
@@ -1,82 +1,99 @@
1
  # qresult.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_qresult(symbol):
55
  yfsymbol = f"{symbol}.NS"
 
56
  try:
57
  ticker = yf.Ticker(yfsymbol)
58
- df = ticker.quarterly_financials
59
- if df.empty:
60
- content_html = f"<h1>No quarterly 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>Quarterly 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>Quarterly Results for {symbol}</title>
75
- {STYLE_BLOCK}
76
- </head>
77
- <body>
78
- {content_html}
79
- </body>
80
- </html>
81
- """
82
- return full_html
 
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
  yfsymbol = f"{symbol}.NS"
14
+
15
  try:
16
  ticker = yf.Ticker(yfsymbol)
17
+
18
+ # Fetch data
19
+ q = ticker.quarterly_financials
20
+ y = ticker.financials
21
+ bs = ticker.balance_sheet
22
+ cf = ticker.cashflow
23
+
24
+ if q is None or q.empty:
25
+ return wrap_html(
26
+ f"Quarterly Results: {symbol}",
27
+ "<h2>No quarterly data available</h2>"
28
+ )
29
+
30
+ # Transpose for nicer table format
31
+ q_t = q.T
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
+ # Format numeric values
37
+ def format_df(df):
38
+ df_formatted = df.copy()
39
+ for col in df_formatted.columns:
40
+ df_formatted[col] = df_formatted[col].apply(
41
+ lambda x: format_large_number(x) if isinstance(x, (int, float)) else x
42
+ )
43
+ # Fix date index
44
+ df_formatted.index = [
45
+ format_timestamp_to_date(i.timestamp()) if hasattr(i, "timestamp") else str(i)
46
+ for i in df_formatted.index
47
+ ]
48
+ return df_formatted
49
+
50
+ q_html = format_df(q_t).to_html(classes="styled-table", border=0)
51
+
52
+ y_html = ""
53
+ if y_t is not None:
54
+ y_html = format_df(y_t).to_html(classes="styled-table", border=0)
55
+
56
+ bs_html = ""
57
+ if bs_t is not None:
58
+ bs_html = format_df(bs_t).to_html(classes="styled-table", border=0)
59
+
60
+ cf_html = ""
61
+ if cf_t is not None:
62
+ cf_html = format_df(cf_t).to_html(classes="styled-table", border=0)
63
+
64
+ # Build sections
65
+ content = f"""
66
+ <div class='big-box'>
67
+ <h2>Quarterly Results</h2>
68
+ {q_html}
69
+ </div>
70
+ """
71
+
72
+ if y_html:
73
+ content += f"""
74
+ <div class='big-box'>
75
+ <h2>Annual Results</h2>
76
+ {y_html}
77
+ </div>
78
+ """
79
+
80
+ if bs_html:
81
+ content += f"""
82
+ <div class='big-box'>
83
+ <h2>Balance Sheet</h2>
84
+ {bs_html}
85
+ </div>
86
+ """
87
+
88
+ if cf_html:
89
+ content += f"""
90
+ <div class='big-box'>
91
+ <h2>Cash Flow</h2>
92
+ {cf_html}
93
+ </div>
94
+ """
95
+
96
+ return wrap_html(f"Quarterly Results — {symbol}", content)
97
+
98
  except Exception as e:
99
+ return wrap_html("Error", f"<h3>Error fetching results</h3><p>{str(e)}</p>")