Spaces:
Sleeping
Sleeping
File size: 6,565 Bytes
d8f5cc7 |
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# info.py
import yfinance as yf
from common import format_large_number, format_timestamp_to_date
def fetch_info(symbol):
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 {
display: block;
width: 95%;
margin: 10px auto;
padding: 15px;
border: 1px solid #ddd;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
background: #fafafa;
}
.card-category-title {
font-size: 1.1em;
color: #222;
margin: 0 0 8px;
border-bottom: 1px solid #eee;
padding-bottom: 5px;
}
.card-content-grid {
display: flex;
flex-wrap: wrap;
gap: 15px;
}
.key-value-pair {
flex: 1 1 calc(20% - 15px);
box-sizing: border-box;
min-width: 150px;
background: #fff;
padding: 10px;
border: 1px solid #e0e0e0;
border-radius: 5px;
box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}
.key-value-pair h3 {
font-size: 0.95em;
color: #444;
margin: 0 0 5px 0;
border-bottom: none;
padding-bottom: 0;
}
.key-value-pair p {
font-size: 0.9em;
color: #555;
margin: 0;
font-weight: bold;
}
.big-box {
width:95%;
margin:20px auto;
padding:20px;
border:1px solid #ccc;
border-radius:8px;
background:#fff;
box-shadow:0 2px 8px rgba(0,0,0,0.1);
font-size:0.95em;
line-height:1.4em;
max-height:400px;
overflow-y:auto;
}
</style>
"""
yfsymbol = symbol + ".NS"
content_html = "<h1>No info available</h1>"
try:
ticker = yf.Ticker(yfsymbol)
info = ticker.info
if info:
long_summary = info.pop("longBusinessSummary", None)
officers = info.pop("companyOfficers", None)
info_categories = {
"Company Overview": [
"longName", "symbol", "exchange", "quoteType", "sector", "industry",
"fullTimeEmployees", "website", "address1", "city", "state", "zip", "country", "phone"
],
"Valuation Metrics": [
"marketCap", "enterpriseValue", "trailingPE", "forwardPE", "pegRatio",
"priceToSalesTrailing12Months", "enterpriseToRevenue", "enterpriseToEbitda"
],
"Key Financials": [
"fiftyTwoWeekHigh", "fiftyTwoWeekLow", "fiftyDayAverage", "twoHundredDayAverage",
"trailingAnnualDividendRate", "trailingAnnualDividendYield", "dividendRate", "dividendYield",
"exDividendDate", "lastSplitFactor", "lastSplitDate", "lastDividendValue", "payoutRatio",
"beta", "sharesOutstanding", "impliedSharesOutstanding"
],
"Operational Details": [
"auditRisk", "boardRisk", "compensationRisk", "shareHolderRightsRisk", "overallRisk",
"governanceEpochDate", "compensationAsOfEpochDate"
],
"Trading Information": [
"open", "previousClose", "dayLow", "dayHigh", "volume", "averageVolume", "averageVolume10days",
"fiftyTwoWeekChange", "SandP52WeekChange", "currency", "regularMarketDayLow",
"regularMarketDayHigh", "regularMarketOpen", "regularMarketPreviousClose",
"regularMarketPrice", "regularMarketVolume", "regularMarketChange", "regularMarketChangePercent"
],
"Analyst & Target": [
"targetMeanPrice", "numberOfAnalystOpinions", "recommendationKey", "recommendationMean"
]
}
categorized_html = ""
for category_name, keys in info_categories.items():
category_html = ""
for key in keys:
if key in info and info[key] not in [None, []]:
value = info[key]
if key in ["exDividendDate","lastSplitDate","governanceEpochDate","compensationAsOfEpochDate"]:
value = format_timestamp_to_date(value)
elif key in ["marketCap","enterpriseValue","fullTimeEmployees","volume","averageVolume","averageVolume10days","sharesOutstanding","impliedSharesOutstanding","regularMarketVolume"]:
value = format_large_number(value)
elif isinstance(value,(int,float)):
if 'percent' in key.lower() or 'ratio' in key.lower() or 'yield' in key.lower() or 'beta' in key.lower() or 'payoutratio' in key.lower():
value = f"{value:.2%}"
elif 'price' in key.lower() or 'dividend' in key.lower() or 'average' in key.lower():
value = f"{value:.2f}"
else:
value = f"{value:,.0f}"
category_html += f"<div class='key-value-pair'><h3>{key.replace('_',' ').title()}</h3><p>{value}</p></div>"
if category_html:
categorized_html += f"<h2 class='card-category-title'>{category_name}</h2><div class='card'><div class='card-content-grid'>{category_html}</div></div>"
extra_sections = ""
if long_summary:
extra_sections += f"<div class='big-box'><h2>Business Summary</h2><p>{long_summary}</p></div>"
if officers:
officer_rows = "".join(
f"<tr><td>{o.get('name','')}</td><td>{o.get('title','')}</td><td>{o.get('age','')}</td></tr>"
for o in officers
)
officer_table = f"<table class='styled-table'><tr><th>Name</th><th>Title</th><th>Age</th></tr>{officer_rows}</table>"
extra_sections += f"<div class='big-box'><h2>Company Officers</h2>{officer_table}</div>"
content_html = f"{categorized_html}{extra_sections}"
except Exception as e:
content_html = f"<h1>Error</h1><p>{e}</p>"
return f"<!DOCTYPE html><html><head>{STYLE_BLOCK}</head><body>{content_html}</body></html>"
|