Space52 / app.py
QuantumLearner's picture
Update app.py
f505f56 verified
import streamlit as st
import requests
import pandas as pd
import os
# ----------------------------------------
# Backend Variables
# ----------------------------------------
API_KEY = os.getenv("FMP_API_KEY")
DEFAULT_RSS_PAGE = 5 # Fixed at 5, not exposed to the user
DEFAULT_SEARCH_NAME = "NJOY"
DEFAULT_CIK = "0001547416"
# ----------------------------------------
# Initialize Session State Variables
# ----------------------------------------
if "run_rss" not in st.session_state:
st.session_state.run_rss = False
if "run_search" not in st.session_state:
st.session_state.run_search = False
if "run_cik" not in st.session_state:
st.session_state.run_cik = False
if "rss_page" not in st.session_state:
st.session_state.rss_page = DEFAULT_RSS_PAGE
if "search_name" not in st.session_state:
st.session_state.search_name = DEFAULT_SEARCH_NAME
if "cik_value" not in st.session_state:
st.session_state.cik_value = DEFAULT_CIK
# ----------------------------------------
# Cached API Functions
# ----------------------------------------
@st.cache_data(show_spinner=False)
def fetch_equity_live_feed(rss_page: int) -> pd.DataFrame:
"""
Fetch equity offering data from the live feed endpoint for the specified page.
"""
url = f"https://financialmodelingprep.com/api/v4/fundraising-rss-feed?page={rss_page}&apikey={API_KEY}"
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
except:
return pd.DataFrame()
if not data:
return pd.DataFrame()
return pd.DataFrame(data)
@st.cache_data(show_spinner=False)
def fetch_equity_search(name: str) -> pd.DataFrame:
"""
Search for equity offerings by company or offering name.
"""
url = f"https://financialmodelingprep.com/api/v4/fundraising/search?name={name}&apikey={API_KEY}"
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
except:
return pd.DataFrame()
if not data:
return pd.DataFrame()
return pd.DataFrame(data)
@st.cache_data(show_spinner=False)
def fetch_equity_by_cik(cik: str) -> pd.DataFrame:
"""
Fetch all equity offerings for a company identified by its CIK.
"""
url = f"https://financialmodelingprep.com/api/v4/fundraising?cik={cik}&apikey={API_KEY}"
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
except:
return pd.DataFrame()
if not data:
return pd.DataFrame()
return pd.DataFrame(data)
# ----------------------------------------
# MAIN APP
# ----------------------------------------
def main():
st.set_page_config(page_title="Equity Offerings", layout="wide")
st.title("Equity Offerings")
st.write(
"Explore recent equity offering announcements. "
"Use the sidebar to select one of three views. "
"Each section provides a table with the raw data."
)
# Sidebar: Navigation and Options
with st.sidebar.expander("Navigation and Options", expanded=True):
page = st.radio(
"Select Page",
("Equity Offering Live Feed", "Equity Offering Search", "Equity Offering By CIK"),
help="Pick a live feed, do a search by name, or view offerings by CIK."
)
if page == "Equity Offering Live Feed":
# Removed the user input for the page; using the fixed default of 5
if st.button("Run"):
st.session_state.run_rss = True
elif page == "Equity Offering Search":
search_name = st.text_input(
"Search Name",
value=st.session_state.search_name,
help="Enter a name to search for equity offerings."
)
st.session_state.search_name = search_name
if st.button("Run"):
st.session_state.run_search = True
else: # Equity Offering By CIK
cik_value = st.text_input(
"Company CIK",
value=st.session_state.cik_value,
help="Enter a company's CIK to view its equity offerings."
)
st.session_state.cik_value = cik_value
if st.button("Run"):
st.session_state.run_cik = True
# ----------------------------------------
# Page Output
# ----------------------------------------
if page == "Equity Offering Live Feed":
st.header("Equity Offering Live Feed")
st.write(
"Shows the most recent equity offering announcements. "
"View details such as the company name, form type, date, and reference URL."
)
if st.session_state.run_rss:
df_rss = fetch_equity_live_feed(st.session_state.rss_page)
if df_rss.empty:
st.error("No data found for this feed page.")
else:
st.dataframe(df_rss, use_container_width=True)
else:
st.info("Click 'Run' to fetch the live feed.")
elif page == "Equity Offering Search":
st.header("Equity Offering Search")
st.write(
"Search equity offerings by name. "
"Matches are shown in the table with relevant offering details."
)
if st.session_state.run_search:
df_search = fetch_equity_search(st.session_state.search_name)
if df_search.empty:
st.error("No equity offerings found for that search name.")
else:
st.dataframe(df_search, use_container_width=True)
else:
st.info("Enter a name in the sidebar and click 'Run'.")
else: # Equity Offering By CIK
st.header("Equity Offering By CIK")
st.write(
"Displays equity offerings for a specific company, identified by its CIK. "
"The table includes detailed information for each offering."
)
if st.session_state.run_cik:
df_cik = fetch_equity_by_cik(st.session_state.cik_value)
if df_cik.empty:
st.error("No equity offerings found for that CIK.")
else:
st.dataframe(df_cik, use_container_width=True)
else:
st.info("Enter a CIK in the sidebar and click 'Run'.")
if __name__ == "__main__":
main()
hide_streamlit_style = """
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)