This repository was archived by the owner on Apr 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnicegui_planner.py
More file actions
182 lines (164 loc) · 6.23 KB
/
Copy pathnicegui_planner.py
File metadata and controls
182 lines (164 loc) · 6.23 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Import your dependencies
from dotenv import load_dotenv
import os
from nicegui import ui
from datetime import date
from datetime import datetime
from dataclasses import dataclass
from typing import List
from nylas import Client
from nylas.models.messages import ListMessagesQueryParams
import pendulum
# Load your env variables
load_dotenv()
# Initialize Nylas client
nylas = Client(
api_key = os.environ.get("V3_API_KEY")
)
@dataclass # Class decorator
class EventItems:
event_name: str
# To hold events
events: List[EventItems] = [
]
@dataclass # Class decorator
class EmailItems:
email_name: str
# To hold emails
emails: List[EmailItems] = [
]
# Get list of events
def get_events(current_date) -> None:
# Get today's date as Year, month, day
today_date = datetime.strptime(current_date, '%Y-%m-%d').date()
# Today’s date at 12:00:00 am
AFTER = int(datetime(today_date.year, today_date.month,
today_date.day, 0, 0, 0).strftime('%s'))
# Today’s date at 11:59:59 pm
BEFORE = int(datetime(today_date.year, today_date.month,
today_date.day, 23, 59, 59).strftime('%s'))
query_params = {"calendar_id": os.environ.get("GRANT_ID"),
"start": AFTER, "end": BEFORE}
events = nylas.events.list(os.environ.get("GRANT_ID"),
query_params=query_params).data
# Auxiliary variables
event_info = ""
# Loop events
if(len(events) > 0):
for event in events:
match event.when.object:
case "timespan":
event_info = f"From: {datetime.fromtimestamp(event.when.start_time).strftime('%H:%M:%S')} \
To: {datetime.fromtimestamp(event.when.end_time).strftime('%H:%M:%S')} \
| {event.title}"
add(event_info, "event")
case "datespan":
event_info = f"From: {datetime.fromtimestamp(event.when.start_date).strftime('%H:%M:%S')} \
To: {datetime.fromtimestamp(event.when.end_date).strftime('%H:%M:%S')} \
| {event.title}"
add(event_info, "event")
case "date":
event_info = f"On: {event.when.date} | {event.title}"
add(f"All day event | {event['title']}", "event")
else:
add("No events today", "event")
# Get list of emails
def get_emails(current_date) -> None:
# Get today's date as Year, month, day
today_date = datetime.strptime(current_date, '%Y-%m-%d').date()
# Today’s date at 12:00:00 am
AFTER = int(datetime(today_date.year, today_date.month, today_date.day, 0, 0, 0).strftime('%s'))
# Today’s date at 11:59:59 pm
BEFORE = int(datetime(today_date.year, today_date.month,today_date.day, 23, 59, 59).strftime('%s'))
# Create query parameters
query_params = ListMessagesQueryParams(
{'in' : "inbox", 'limit': 5, 'unread': True, 'received_after': AFTER, 'received_before': BEFORE}
)
# Get all emails in the specified range
messages, _, _ = nylas.messages.list(os.environ.get("GRANT_ID"), query_params)
# Auxiliary variables
email_info = ""
# Loop emails
if(len(messages) > 0):
for message in messages:
# Grab the time, who's sending the email and the title of the email
email_info = f"Time: {pendulum.instance(message.date).strftime('%H:%M:%S')} \
| From: {message.from_[0].email} | Title: {message.subject}"
add(email_info, "email")
# No emails?
else:
add("No new emails today", "email")
# Function to add events and emails
def add(name: str, type_ : str) -> None:
# If it's an event, add it to the events list
if type_ == "event":
events.append(EventItems(name))
render_events_list.refresh()
# If it's an email, add it to the email list
else:
emails.append(EmailItems(name))
render_emails_list.refresh()
# Clear the list of events
def clear_events() -> None:
events.clear()
# Clear the list of emails
def clear_emails() -> None:
emails.clear()
# When we click on the calendar
# to select a new date
def handle_input(e):
# We pass the selected date
current_date = e.value
# If for some reason, there's no date
# grab the current date
if current_date is None:
date.today()
current_date = str(date.today())
clear_events()
# Get events for the selected date
get_events(current_date)
clear_emails()
# Get emails for the selected date
get_emails(current_date)
# Decorator to refresh the events list
@ui.refreshable
def render_events_list():
# Title using Tailwind CSS
ui.label('Events').tailwind.font_weight('black').font_size('4xl').text_color('blue-700')
# Loop events
for event in events:
# Put events one after the other
with ui.row().classes('items-center'):
# Detail of event
ui.label(event.event_name).tailwind.font_weight('semibold').font_size('lg')
# Decorator to refresh the emails list
@ui.refreshable
def render_emails_list():
# Title using Tailwind CSS
ui.label('Emails').tailwind.font_weight('black').font_size('4xl').text_color('blue-700')
# Loop emails
for email in emails:
# Put events one after the other
with ui.row().classes('items-center'):
# Detail of event
ui.label(email.email_name).tailwind.font_weight('semibold').font_size('lg')
# Main layout with everything centered
with ui.column().classes('w-full items-center'):
# Get today's date
today = date.today()
current_date = str(date.today())
# Clear all events and emails
clear_events()
clear_emails()
# Set application title
ui.label('Daily Planner').tailwind.font_weight('black').font_size('6xl').text_color('blue-700')
# Create the calendar widget
ui.date(value=today, on_change=handle_input)
# Show all events
get_events(current_date)
render_events_list()
# Show all emails
get_emails(current_date)
render_emails_list()
# Run our application
ui.run(title = 'NiceGUI Planner')