Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
|
| 3 |
hide_streamlit_style = """
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
# Global API key and default parameters
|
| 6 |
+
API_KEY = os.getenv("FMP_API_KEY")
|
| 7 |
+
DEFAULT_COMPANY = "syros"
|
| 8 |
+
NUM_PAGES = 10 # Backend variable: number of pages to fetch
|
| 9 |
+
|
| 10 |
+
# Initialize session state run flags and inputs if not already set
|
| 11 |
+
if "run_mna_feed" not in st.session_state:
|
| 12 |
+
st.session_state.run_mna_feed = False
|
| 13 |
+
if "run_mna_search" not in st.session_state:
|
| 14 |
+
st.session_state.run_mna_search = False
|
| 15 |
+
if "search_company" not in st.session_state:
|
| 16 |
+
st.session_state.search_company = DEFAULT_COMPANY
|
| 17 |
+
if "feed_date" not in st.session_state:
|
| 18 |
+
st.session_state.feed_date = pd.to_datetime("2025-01-01").date()
|
| 19 |
+
|
| 20 |
+
##############################################
|
| 21 |
+
# CACHED FUNCTIONS
|
| 22 |
+
##############################################
|
| 23 |
+
@st.cache_data(show_spinner=False)
|
| 24 |
+
def fetch_mna_feed(num_pages=NUM_PAGES) -> pd.DataFrame:
|
| 25 |
+
"""
|
| 26 |
+
Fetch the general M&A RSS feed data from multiple pages.
|
| 27 |
+
"""
|
| 28 |
+
frames = []
|
| 29 |
+
for page in range(num_pages):
|
| 30 |
+
url = f"https://financialmodelingprep.com/api/v4/mergers-acquisitions-rss-feed?page={page}&apikey={API_KEY}"
|
| 31 |
+
response = requests.get(url)
|
| 32 |
+
response.raise_for_status()
|
| 33 |
+
data = response.json()
|
| 34 |
+
if not data:
|
| 35 |
+
break
|
| 36 |
+
frames.append(pd.DataFrame(data))
|
| 37 |
+
if frames:
|
| 38 |
+
df = pd.concat(frames, ignore_index=True)
|
| 39 |
+
# Convert transactionDate to datetime if present
|
| 40 |
+
if "transactionDate" in df.columns:
|
| 41 |
+
df["transactionDate"] = pd.to_datetime(df["transactionDate"], errors="coerce")
|
| 42 |
+
return df
|
| 43 |
+
return pd.DataFrame()
|
| 44 |
+
|
| 45 |
+
@st.cache_data(show_spinner=False)
|
| 46 |
+
def fetch_mna_search(company_name: str) -> pd.DataFrame:
|
| 47 |
+
"""
|
| 48 |
+
Fetch M&A news filtered by company name using the search endpoint.
|
| 49 |
+
"""
|
| 50 |
+
url = f"https://financialmodelingprep.com/api/v4/mergers-acquisitions/search?name={company_name}&apikey={API_KEY}"
|
| 51 |
+
response = requests.get(url)
|
| 52 |
+
response.raise_for_status()
|
| 53 |
+
data = response.json()
|
| 54 |
+
if not data:
|
| 55 |
+
return pd.DataFrame()
|
| 56 |
+
return pd.DataFrame(data)
|
| 57 |
+
|
| 58 |
+
##############################################
|
| 59 |
+
# MAIN APP
|
| 60 |
+
##############################################
|
| 61 |
+
def main():
|
| 62 |
+
st.set_page_config(page_title="M&A Feed Dashboard", layout="wide")
|
| 63 |
+
st.title("M&A Feed Dashboard")
|
| 64 |
+
st.write(
|
| 65 |
+
"This dashboard provides a real-time stream of Mergers & Acquisitions news and announcements. "
|
| 66 |
+
"Use the side menu below to choose between viewing the full M&A RSS feed or searching for M&A news by company name."
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# Sidebar (inside an expander) for navigation and options
|
| 70 |
+
with st.sidebar.expander("Navigation and Options", expanded=True):
|
| 71 |
+
page = st.radio(
|
| 72 |
+
"Select Page",
|
| 73 |
+
("M&A Feed", "M&A Search"),
|
| 74 |
+
help="Choose 'M&A Feed' to view the complete RSS feed or 'M&A Search' to search for news by company name."
|
| 75 |
+
)
|
| 76 |
+
if page == "M&A Feed":
|
| 77 |
+
feed_date = st.date_input(
|
| 78 |
+
"From Date",
|
| 79 |
+
value=pd.to_datetime("2025-01-01").date(),
|
| 80 |
+
help="Select a starting date. Only M&A transactions on or after this date will be shown."
|
| 81 |
+
)
|
| 82 |
+
st.session_state.feed_date = feed_date
|
| 83 |
+
if st.button("Run M&A Feed"):
|
| 84 |
+
st.session_state.run_mna_feed = True
|
| 85 |
+
else:
|
| 86 |
+
company = st.text_input(
|
| 87 |
+
"Company Name",
|
| 88 |
+
value=DEFAULT_COMPANY,
|
| 89 |
+
help="Enter the company name to search for M&A news (default is 'syros')."
|
| 90 |
+
)
|
| 91 |
+
st.session_state.search_company = company
|
| 92 |
+
if st.button("Run M&A Search"):
|
| 93 |
+
st.session_state.run_mna_search = True
|
| 94 |
+
|
| 95 |
+
# Display page content based on the selected page
|
| 96 |
+
if page == "M&A Feed":
|
| 97 |
+
st.header("M&A RSS Feed")
|
| 98 |
+
st.write(
|
| 99 |
+
"Below is the latest M&A RSS feed data filtered by transaction date. "
|
| 100 |
+
"Only M&A transactions on or after the selected date are shown. "
|
| 101 |
+
"The table displays details such as the acquiring company, target company, transaction date, and URL."
|
| 102 |
+
)
|
| 103 |
+
if st.session_state.run_mna_feed:
|
| 104 |
+
df_feed = fetch_mna_feed()
|
| 105 |
+
if df_feed.empty:
|
| 106 |
+
st.error("No data returned from the M&A feed.")
|
| 107 |
+
else:
|
| 108 |
+
# Filter the data based on the selected date if transactionDate exists
|
| 109 |
+
if "transactionDate" in df_feed.columns:
|
| 110 |
+
filtered_df = df_feed[df_feed["transactionDate"].dt.date >= st.session_state.feed_date]
|
| 111 |
+
else:
|
| 112 |
+
filtered_df = df_feed
|
| 113 |
+
if filtered_df.empty:
|
| 114 |
+
st.error("No M&A transactions found on or after the selected date.")
|
| 115 |
+
else:
|
| 116 |
+
st.dataframe(filtered_df, use_container_width=True)
|
| 117 |
+
else:
|
| 118 |
+
st.info("Click 'Run M&A Feed' in the sidebar to fetch data.")
|
| 119 |
+
else:
|
| 120 |
+
st.header("M&A Search")
|
| 121 |
+
st.write(
|
| 122 |
+
"Search for M&A news by company name. "
|
| 123 |
+
"The table below displays the matching announcements, including details such as the acquiring company, "
|
| 124 |
+
"target company, transaction date, and announcement URL."
|
| 125 |
+
)
|
| 126 |
+
if st.session_state.run_mna_search:
|
| 127 |
+
df_search = fetch_mna_search(st.session_state.search_company)
|
| 128 |
+
if df_search.empty:
|
| 129 |
+
st.error("No M&A news found for the specified company name.")
|
| 130 |
+
else:
|
| 131 |
+
st.dataframe(df_search, use_container_width=True)
|
| 132 |
+
else:
|
| 133 |
+
st.info("Enter a company name in the sidebar and click 'Run M&A Search' to fetch data.")
|
| 134 |
+
|
| 135 |
+
if __name__ == "__main__":
|
| 136 |
+
main()
|
| 137 |
|
| 138 |
|
| 139 |
hide_streamlit_style = """
|