Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import datetime
|
| 3 |
+
import requests
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from streamlit_calendar import calendar
|
| 6 |
+
|
| 7 |
+
API_KEY = "b431ec171262073909ebf8c0c4afba71"
|
| 8 |
+
|
| 9 |
+
def fetch_economic_calendar(from_date, to_date):
|
| 10 |
+
from_str = from_date.strftime("%Y-%m-%d")
|
| 11 |
+
to_str = to_date.strftime("%Y-%m-%d")
|
| 12 |
+
url = (
|
| 13 |
+
f"https://financialmodelingprep.com/api/v3/economic_calendar"
|
| 14 |
+
f"?from={from_str}&to={to_str}&apikey={API_KEY}"
|
| 15 |
+
)
|
| 16 |
+
resp = requests.get(url)
|
| 17 |
+
if resp.status_code == 200:
|
| 18 |
+
return resp.json()
|
| 19 |
+
return []
|
| 20 |
+
|
| 21 |
+
def main():
|
| 22 |
+
st.set_page_config(page_title="Economic Calendar", layout="wide")
|
| 23 |
+
|
| 24 |
+
if "econ_data" not in st.session_state:
|
| 25 |
+
st.session_state["econ_data"] = []
|
| 26 |
+
|
| 27 |
+
st.title("Economic Calendar")
|
| 28 |
+
st.write("This app displays upcoming economic events.")
|
| 29 |
+
|
| 30 |
+
st.sidebar.title("Input Parameters")
|
| 31 |
+
|
| 32 |
+
with st.sidebar.expander("How to Use", expanded=False):
|
| 33 |
+
st.write("Select a date range. Then click the button to fetch events.")
|
| 34 |
+
|
| 35 |
+
today = datetime.date.today()
|
| 36 |
+
one_month_later = today + datetime.timedelta(days=30)
|
| 37 |
+
|
| 38 |
+
with st.sidebar.expander("Parameters", expanded=True):
|
| 39 |
+
from_date = st.date_input("From Date", value=today)
|
| 40 |
+
to_date = st.date_input("To Date", value=one_month_later)
|
| 41 |
+
|
| 42 |
+
if st.sidebar.button("Retrieve Calendar"):
|
| 43 |
+
data = fetch_economic_calendar(from_date, to_date)
|
| 44 |
+
event_list = []
|
| 45 |
+
for item in data:
|
| 46 |
+
dt_str = item.get("date", "")
|
| 47 |
+
if dt_str:
|
| 48 |
+
# Replacing space with 'T' for proper ISO format
|
| 49 |
+
iso_dt = dt_str.replace(" ", "T")
|
| 50 |
+
else:
|
| 51 |
+
iso_dt = f"{from_date}T00:00:00"
|
| 52 |
+
event_entry = {
|
| 53 |
+
"start": iso_dt,
|
| 54 |
+
"end": iso_dt,
|
| 55 |
+
"title": f"{item.get('event', '')} ({item.get('country', '')})",
|
| 56 |
+
"color": "#3D9DF3",
|
| 57 |
+
}
|
| 58 |
+
event_entry.update(item)
|
| 59 |
+
event_list.append(event_entry)
|
| 60 |
+
|
| 61 |
+
st.session_state["econ_data"] = event_list
|
| 62 |
+
|
| 63 |
+
st.subheader("Economic Calendar Results")
|
| 64 |
+
|
| 65 |
+
data_econ = st.session_state["econ_data"]
|
| 66 |
+
if data_econ:
|
| 67 |
+
calendar_events = [
|
| 68 |
+
{
|
| 69 |
+
"title": ev["title"],
|
| 70 |
+
"start": ev["start"],
|
| 71 |
+
"end": ev["end"],
|
| 72 |
+
"color": ev["color"],
|
| 73 |
+
}
|
| 74 |
+
for ev in data_econ
|
| 75 |
+
]
|
| 76 |
+
cal_options = {
|
| 77 |
+
"initialView": "dayGridMonth",
|
| 78 |
+
"headerToolbar": {
|
| 79 |
+
"left": "today prev,next",
|
| 80 |
+
"center": "title",
|
| 81 |
+
"right": "dayGridDay,dayGridWeek,dayGridMonth",
|
| 82 |
+
},
|
| 83 |
+
"navLinks": True,
|
| 84 |
+
}
|
| 85 |
+
calendar(events=calendar_events, options=cal_options, key="econ_cal")
|
| 86 |
+
st.write("Data Table")
|
| 87 |
+
st.dataframe(pd.DataFrame(data_econ), use_container_width=True)
|
| 88 |
+
else:
|
| 89 |
+
st.write("No data found. Adjust the date range and click again.")
|
| 90 |
+
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
main()
|
| 93 |
+
|
| 94 |
+
hide_streamlit_style = """
|
| 95 |
+
<style>
|
| 96 |
+
#MainMenu {visibility: hidden;}
|
| 97 |
+
footer {visibility: hidden;}
|
| 98 |
+
</style>
|
| 99 |
+
"""
|
| 100 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|