-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
84 lines (74 loc) · 2.14 KB
/
Copy pathutils.py
File metadata and controls
84 lines (74 loc) · 2.14 KB
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
import streamlit as st
import yfinance as yf
import pandas as pd
@st.cache_data(ttl=3600)
def get_stock_data(symbol: str, period: str) -> pd.DataFrame:
"""
Fetch stock data from Yahoo Finance with caching.
Args:
symbol: Stock symbol
period: Time period for historical data
Returns:
DataFrame with stock price data
"""
try:
stock = yf.Ticker(symbol)
df = stock.history(period=period)
return df if not df.empty else None
except Exception as e:
st.error(f"Error fetching data: {str(e)}")
return None
@st.cache_data(ttl=3600)
def get_dividend_data(symbol: str) -> pd.DataFrame:
"""
Fetch dividend history from Yahoo Finance with caching.
Args:
symbol: Stock symbol
Returns:
DataFrame with dividend history
"""
try:
stock = yf.Ticker(symbol)
dividends = stock.dividends
if not dividends.empty:
df = pd.DataFrame(dividends)
df.index = pd.to_datetime(df.index)
return df
return None
except Exception as e:
st.error(f"Error fetching dividend data: {str(e)}")
return None
@st.cache_data(ttl=3600)
def get_dividend_data(symbol: str) -> pd.DataFrame:
"""
Fetch dividend history from Yahoo Finance with caching.
Args:
symbol: Stock symbol
Returns:
DataFrame with dividend history
"""
try:
stock = yf.Ticker(symbol)
dividends = stock.dividends
if not dividends.empty:
df = pd.DataFrame(dividends)
df.index = pd.to_datetime(df.index)
return df
return None
except Exception as e:
st.error(f"Error fetching dividend data: {str(e)}")
return None
def download_csv(df: pd.DataFrame, filename: str):
"""
Create a download button for CSV export.
Args:
df: DataFrame to export
filename: Name for the downloaded file
"""
csv = df.to_csv(index=True)
st.download_button(
label="Download CSV",
data=csv,
file_name=f"{filename}.csv",
mime="text/csv"
)