File size: 3,217 Bytes
f0757c1
 
 
 
 
1c43187
f0757c1
c1f85eb
f0757c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108ea29
f0757c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7750441
f0757c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7750441
efee7dc
f0757c1
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import streamlit as st
import datetime
import requests
import pandas as pd
from streamlit_calendar import calendar
import os

API_KEY = os.getenv("FMP_API_KEY")

def fetch_economic_calendar(from_date, to_date):
    from_str = from_date.strftime("%Y-%m-%d")
    to_str = to_date.strftime("%Y-%m-%d")
    url = (
        f"https://financialmodelingprep.com/api/v3/economic_calendar"
        f"?from={from_str}&to={to_str}&apikey={API_KEY}"
    )
    resp = requests.get(url)
    if resp.status_code == 200:
        return resp.json()
    return []

def main():
    st.set_page_config(page_title="Economic Calendar", layout="wide")

    if "econ_data" not in st.session_state:
        st.session_state["econ_data"] = []

    st.title("Economic Calendar")
    st.write("This tool displays upcoming economic events. Set parameters and click run.")

    st.sidebar.title("Input Parameters")

    with st.sidebar.expander("How to Use", expanded=False):
        st.write("Select a date range. Then click the button to fetch events.")

    today = datetime.date.today()
    one_month_later = today + datetime.timedelta(days=30)

    with st.sidebar.expander("Parameters", expanded=True):
        from_date = st.date_input("From Date", value=today)
        to_date = st.date_input("To Date", value=one_month_later)

    if st.sidebar.button("Retrieve Calendar"):
        data = fetch_economic_calendar(from_date, to_date)
        event_list = []
        for item in data:
            dt_str = item.get("date", "")
            if dt_str:
                # Replacing space with 'T' for proper ISO format
                iso_dt = dt_str.replace(" ", "T")
            else:
                iso_dt = f"{from_date}T00:00:00"
            event_entry = {
                "start": iso_dt,
                "end": iso_dt,
                "title": f"{item.get('event', '')} ({item.get('country', '')})",
                "color": "#3D9DF3",
            }
            event_entry.update(item)
            event_list.append(event_entry)

        st.session_state["econ_data"] = event_list

    #st.subheader("Economic Calendar Results")

    data_econ = st.session_state["econ_data"]
    if data_econ:
        calendar_events = [
            {
                "title": ev["title"],
                "start": ev["start"],
                "end": ev["end"],
                "color": ev["color"],
            }
            for ev in data_econ
        ]
        cal_options = {
            "initialView": "dayGridMonth",
            "headerToolbar": {
                "left": "today prev,next",
                "center": "title",
                "right": "dayGridDay,dayGridWeek,dayGridMonth",
            },
            "navLinks": True,
        }
        calendar(events=calendar_events, options=cal_options, key="econ_cal")
        st.write("Data Table")
        st.dataframe(pd.DataFrame(data_econ), use_container_width=True)
    else:
        #st.write("No data found. Adjust the date range and click again.")
        st.write(" ")

if __name__ == "__main__":
    main()
    
hide_streamlit_style = """
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)