QuantumLearner commited on
Commit
0e084ea
·
verified ·
1 Parent(s): 56966d8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +179 -0
app.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import pandas as pd
4
+ import os
5
+
6
+ # ---------------------------
7
+ # Global Variables (Backend)
8
+ # ---------------------------
9
+ API_KEY = os.getenv("FMP_API_KEY")
10
+ DEFAULT_RSS_PAGE = 0
11
+ DEFAULT_SEARCH_NAME = "enotap"
12
+ DEFAULT_CIK = "0001916078"
13
+ NUM_PAGES = 10 # if applicable, can be used in other endpoints
14
+
15
+ # ---------------------------
16
+ # Initialize Session State
17
+ # ---------------------------
18
+ if "run_rss" not in st.session_state:
19
+ st.session_state.run_rss = False
20
+ if "run_search" not in st.session_state:
21
+ st.session_state.run_search = False
22
+ if "run_cik" not in st.session_state:
23
+ st.session_state.run_cik = False
24
+
25
+ if "rss_page" not in st.session_state:
26
+ st.session_state.rss_page = DEFAULT_RSS_PAGE
27
+ if "search_name" not in st.session_state:
28
+ st.session_state.search_name = DEFAULT_SEARCH_NAME
29
+ if "cik_value" not in st.session_state:
30
+ st.session_state.cik_value = DEFAULT_CIK
31
+
32
+ # ---------------------------
33
+ # CACHED API FUNCTIONS
34
+ # ---------------------------
35
+ @st.cache_data(show_spinner=False)
36
+ def fetch_crowdfunding_rss(page: int) -> pd.DataFrame:
37
+ """
38
+ Fetch the crowdfunding RSS feed data from the given page.
39
+ """
40
+ url = f"https://financialmodelingprep.com/api/v4/crowdfunding-offerings-rss-feed?page={page}&apikey={API_KEY}"
41
+ response = requests.get(url)
42
+ response.raise_for_status()
43
+ data = response.json()
44
+ if not data:
45
+ return pd.DataFrame()
46
+ return pd.DataFrame(data)
47
+
48
+ @st.cache_data(show_spinner=False)
49
+ def fetch_crowdfunding_search(name: str) -> pd.DataFrame:
50
+ """
51
+ Search for crowdfunding campaigns by name.
52
+ """
53
+ url = f"https://financialmodelingprep.com/api/v4/crowdfunding-offerings/search?name={name}&apikey={API_KEY}"
54
+ response = requests.get(url)
55
+ response.raise_for_status()
56
+ data = response.json()
57
+ if not data:
58
+ return pd.DataFrame()
59
+ return pd.DataFrame(data)
60
+
61
+ @st.cache_data(show_spinner=False)
62
+ def fetch_crowdfunding_by_cik(cik: str) -> pd.DataFrame:
63
+ """
64
+ Fetch all crowdfunding campaigns launched by a company identified by its CIK.
65
+ """
66
+ url = f"https://financialmodelingprep.com/api/v4/crowdfunding-offerings?cik={cik}&apikey={API_KEY}"
67
+ response = requests.get(url)
68
+ response.raise_for_status()
69
+ data = response.json()
70
+ if not data:
71
+ return pd.DataFrame()
72
+ return pd.DataFrame(data)
73
+
74
+ # ---------------------------
75
+ # MAIN APP
76
+ # ---------------------------
77
+ def main():
78
+ st.set_page_config(page_title="Crowdfunding Campaigns Dashboard", layout="wide")
79
+ st.title("Crowdfunding Campaigns Dashboard")
80
+ st.write(
81
+ "This dashboard provides access to real-time crowdfunding campaign data. "
82
+ "Use the side menu to select one of the three pages below. "
83
+ "Each page shows a DataFrame with details fetched from Financial Modeling Prep."
84
+ )
85
+
86
+ # Sidebar: Navigation and Options
87
+ with st.sidebar.expander("Navigation and Options", expanded=True):
88
+ page = st.radio(
89
+ "Select Page",
90
+ ("Crowdfunding RSS Feed", "Crowdfunding Search", "Crowdfunding By CIK"),
91
+ help="Choose the page you wish to view. The RSS Feed shows a live feed; Search allows searching by campaign or company name; and By CIK shows campaigns launched by a company using its CIK."
92
+ )
93
+
94
+ if page == "Crowdfunding RSS Feed":
95
+ rss_page = st.number_input(
96
+ "RSS Feed Page",
97
+ min_value=0,
98
+ value=DEFAULT_RSS_PAGE,
99
+ help="Enter the page number to fetch from the crowdfunding RSS feed."
100
+ )
101
+ st.session_state.rss_page = rss_page
102
+ if st.button("Run Crowdfunding RSS Feed"):
103
+ st.session_state.run_rss = True
104
+ elif page == "Crowdfunding Search":
105
+ search_name = st.text_input(
106
+ "Campaign/Company Name",
107
+ value=DEFAULT_SEARCH_NAME,
108
+ help="Enter the name to search for crowdfunding campaigns (e.g., enotap)."
109
+ )
110
+ st.session_state.search_name = search_name
111
+ if st.button("Run Crowdfunding Search"):
112
+ st.session_state.run_search = True
113
+ else: # Crowdfunding By CIK
114
+ cik_value = st.text_input(
115
+ "Company CIK",
116
+ value=DEFAULT_CIK,
117
+ help="Enter the CIK of the company to fetch its crowdfunding campaigns."
118
+ )
119
+ st.session_state.cik_value = cik_value
120
+ if st.button("Run Crowdfunding By CIK"):
121
+ st.session_state.run_cik = True
122
+
123
+ # Page Output
124
+ if page == "Crowdfunding RSS Feed":
125
+ st.header("Crowdfunding RSS Feed")
126
+ st.write(
127
+ "This page displays the latest crowdfunding campaigns from an RSS feed. "
128
+ "The data includes various details such as company name, form type, offering amount, and more."
129
+ )
130
+ if st.session_state.run_rss:
131
+ df_rss = fetch_crowdfunding_rss(st.session_state.rss_page)
132
+ if df_rss.empty:
133
+ st.error("No data returned from the Crowdfunding RSS Feed.")
134
+ else:
135
+ st.dataframe(df_rss, use_container_width=True)
136
+ else:
137
+ st.info("Enter the page number in the sidebar and click 'Run Crowdfunding RSS Feed'.")
138
+
139
+ elif page == "Crowdfunding Search":
140
+ st.header("Crowdfunding Search")
141
+ st.write(
142
+ "This page allows you to search for crowdfunding campaigns by company or campaign name. "
143
+ "The table below displays the matching campaigns with key details."
144
+ )
145
+ if st.session_state.run_search:
146
+ df_search = fetch_crowdfunding_search(st.session_state.search_name)
147
+ if df_search.empty:
148
+ st.error("No crowdfunding campaigns found for the specified name.")
149
+ else:
150
+ st.dataframe(df_search, use_container_width=True)
151
+ else:
152
+ st.info("Enter a name in the sidebar and click 'Run Crowdfunding Search'.")
153
+
154
+ else: # Crowdfunding By CIK
155
+ st.header("Crowdfunding By CIK")
156
+ st.write(
157
+ "This page displays all crowdfunding campaigns launched by a company identified by its CIK. "
158
+ "The table below contains detailed information for each campaign."
159
+ )
160
+ if st.session_state.run_cik:
161
+ df_cik = fetch_crowdfunding_by_cik(st.session_state.cik_value)
162
+ if df_cik.empty:
163
+ st.error("No crowdfunding campaigns found for the specified CIK.")
164
+ else:
165
+ st.dataframe(df_cik, use_container_width=True)
166
+ else:
167
+ st.info("Enter a CIK in the sidebar and click 'Run Crowdfunding By CIK'.")
168
+
169
+ if __name__ == "__main__":
170
+ main()
171
+
172
+
173
+ hide_streamlit_style = """
174
+ <style>
175
+ #MainMenu {visibility: hidden;}
176
+ footer {visibility: hidden;}
177
+ </style>
178
+ """
179
+ st.markdown(hide_streamlit_style, unsafe_allow_html=True)