Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
import requests
|
4 |
+
import msal
|
5 |
+
|
6 |
+
# π€ Load environment variables (Ensure these are set!)
|
7 |
+
APPLICATION_ID_KEY = os.getenv('APPLICATION_ID_KEY')
|
8 |
+
CLIENT_SECRET_KEY = os.getenv('CLIENT_SECRET_KEY')
|
9 |
+
AUTHORITY_URL = 'https://login.microsoftonline.com/common' # Use 'common' for multi-tenant apps
|
10 |
+
REDIRECT_URI = 'http://localhost:8501' # π Make sure this matches your app's redirect URI
|
11 |
+
|
12 |
+
# π― Define the scopes your app will need
|
13 |
+
SCOPES = ['User.Read', 'Calendars.ReadWrite']
|
14 |
+
|
15 |
+
# π οΈ Initialize the MSAL client
|
16 |
+
def get_msal_app():
|
17 |
+
return msal.ConfidentialClientApplication(
|
18 |
+
client_id=APPLICATION_ID_KEY,
|
19 |
+
client_credential=CLIENT_SECRET_KEY,
|
20 |
+
authority=AUTHORITY_URL
|
21 |
+
)
|
22 |
+
|
23 |
+
# π Acquire access token using authorization code
|
24 |
+
def get_access_token(code):
|
25 |
+
client_instance = get_msal_app()
|
26 |
+
result = client_instance.acquire_token_by_authorization_code(
|
27 |
+
code=code,
|
28 |
+
scopes=SCOPES,
|
29 |
+
redirect_uri=REDIRECT_URI
|
30 |
+
)
|
31 |
+
if 'access_token' in result:
|
32 |
+
return result['access_token']
|
33 |
+
else:
|
34 |
+
st.error('Error acquiring token: ' + str(result.get('error_description')))
|
35 |
+
st.stop()
|
36 |
+
|
37 |
+
# πββοΈ Main application function
|
38 |
+
def main():
|
39 |
+
st.title("π¦ Simple Streamlit App with MS Graph API")
|
40 |
+
|
41 |
+
# π Sidebar navigation
|
42 |
+
st.sidebar.title("Navigation")
|
43 |
+
menu = st.sidebar.radio("Go to", [
|
44 |
+
"1οΈβ£ Dashboard with Widgets",
|
45 |
+
"π Landing Page",
|
46 |
+
"π
Upcoming Events",
|
47 |
+
"π Schedule",
|
48 |
+
"π Agenda",
|
49 |
+
"π Event Details",
|
50 |
+
"β Add Event",
|
51 |
+
"π Filter By"
|
52 |
+
])
|
53 |
+
|
54 |
+
# π Authentication
|
55 |
+
if 'access_token' not in st.session_state:
|
56 |
+
# π΅οΈββοΈ Check for authorization code in query parameters
|
57 |
+
query_params = st.experimental_get_query_params()
|
58 |
+
if 'code' in query_params:
|
59 |
+
code = query_params['code'][0]
|
60 |
+
st.write('π Acquiring access token...')
|
61 |
+
access_token = get_access_token(code)
|
62 |
+
st.session_state['access_token'] = access_token
|
63 |
+
st.experimental_rerun() # Reload the app to clear the code from URL
|
64 |
+
else:
|
65 |
+
# π’ Prompt user to log in
|
66 |
+
client_instance = get_msal_app()
|
67 |
+
authorization_url = client_instance.get_authorization_request_url(
|
68 |
+
scopes=SCOPES,
|
69 |
+
redirect_uri=REDIRECT_URI
|
70 |
+
)
|
71 |
+
st.write('π Please [click here]({}) to log in and authorize the app.'.format(authorization_url))
|
72 |
+
st.stop()
|
73 |
+
else:
|
74 |
+
# π₯³ User is authenticated, proceed with the app
|
75 |
+
access_token = st.session_state['access_token']
|
76 |
+
headers = {'Authorization': 'Bearer ' + access_token}
|
77 |
+
|
78 |
+
# π€ Greet the user
|
79 |
+
response = requests.get('https://graph.microsoft.com/v1.0/me', headers=headers)
|
80 |
+
if response.status_code == 200:
|
81 |
+
user_info = response.json()
|
82 |
+
st.sidebar.write(f"π Hello, {user_info['displayName']}!")
|
83 |
+
else:
|
84 |
+
st.error('Failed to fetch user info.')
|
85 |
+
st.write(response.text)
|
86 |
+
|
87 |
+
# ποΈ Handle menu options
|
88 |
+
if menu == "1οΈβ£ Dashboard with Widgets":
|
89 |
+
st.header("1οΈβ£ Dashboard with Widgets")
|
90 |
+
st.write("Widgets will be displayed here. ποΈ")
|
91 |
+
# Add your widgets here
|
92 |
+
|
93 |
+
elif menu == "π Landing Page":
|
94 |
+
st.header("π Landing Page")
|
95 |
+
st.write("Welcome to the app! π₯³")
|
96 |
+
# Add landing page content here
|
97 |
+
|
98 |
+
elif menu == "π
Upcoming Events":
|
99 |
+
st.header("π
Upcoming Events")
|
100 |
+
events = get_upcoming_events(access_token)
|
101 |
+
for event in events:
|
102 |
+
st.write(f"π {event['subject']} on {event['start']['dateTime']}")
|
103 |
+
# Display upcoming events
|
104 |
+
|
105 |
+
elif menu == "π Schedule":
|
106 |
+
st.header("π Schedule")
|
107 |
+
schedule = get_schedule(access_token)
|
108 |
+
st.write(schedule)
|
109 |
+
# Display schedule
|
110 |
+
|
111 |
+
elif menu == "π Agenda":
|
112 |
+
st.header("π Agenda")
|
113 |
+
st.write("Your agenda for today. π")
|
114 |
+
# Display agenda
|
115 |
+
|
116 |
+
elif menu == "π Event Details":
|
117 |
+
st.header("π Event Details")
|
118 |
+
event_id = st.text_input("Enter Event ID")
|
119 |
+
if event_id:
|
120 |
+
event_details = get_event_details(access_token, event_id)
|
121 |
+
st.write(event_details)
|
122 |
+
# Display event details based on ID
|
123 |
+
|
124 |
+
elif menu == "β Add Event":
|
125 |
+
st.header("β Add Event")
|
126 |
+
event_subject = st.text_input("Event Subject")
|
127 |
+
event_start = st.date_input("Event Start Date")
|
128 |
+
event_start_time = st.time_input("Event Start Time")
|
129 |
+
event_end = st.date_input("Event End Date")
|
130 |
+
event_end_time = st.time_input("Event End Time")
|
131 |
+
if st.button("Add Event"):
|
132 |
+
event_details = {
|
133 |
+
"subject": event_subject,
|
134 |
+
"start": {
|
135 |
+
"dateTime": f"{event_start}T{event_start_time}",
|
136 |
+
"timeZone": "UTC"
|
137 |
+
},
|
138 |
+
"end": {
|
139 |
+
"dateTime": f"{event_end}T{event_end_time}",
|
140 |
+
"timeZone": "UTC"
|
141 |
+
}
|
142 |
+
}
|
143 |
+
add_event(access_token, event_details)
|
144 |
+
st.success("Event added successfully! π")
|
145 |
+
# Form to add new event
|
146 |
+
|
147 |
+
elif menu == "π Filter By":
|
148 |
+
st.header("π Filter Events")
|
149 |
+
filter_criteria = st.text_input("Enter filter criteria")
|
150 |
+
if filter_criteria:
|
151 |
+
filtered_events = filter_events(access_token, filter_criteria)
|
152 |
+
for event in filtered_events:
|
153 |
+
st.write(f"π
{event['subject']} on {event['start']['dateTime']}")
|
154 |
+
# Filter events based on criteria
|
155 |
+
|
156 |
+
else:
|
157 |
+
st.write("Please select a menu option.")
|
158 |
+
|
159 |
+
# π
Function to get upcoming events
|
160 |
+
def get_upcoming_events(access_token):
|
161 |
+
headers = {'Authoriza
|