-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
138 lines (108 loc) · 3.86 KB
/
main.py
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
#!/usr/bin/env python3
"""
The main entrypoint for the NiceGUI-based application for my quantified self
project that analyzes and visualizes various datasets I have collected about
myself such as local weather, exercise, time-tracking, and many others.
Execute this simply by: `python main.py`.
Remember to set ui.run(reload=False) when pushing to production.
"""
# =============================================================================
# MARK: imports
# =============================================================================
# Standard library imports
import os
import asyncio
import logging
# import datetime as dt
# Third party imports
import schedule
from dotenv import load_dotenv
from nicegui import ui, app
# Local imports
# import design as ds
# import logging_config # noqa: F401, pylint: disable=unused-import
import homepage
# =============================================================================
# MARK: Preamble
# =============================================================================
# Load environment variables
load_dotenv()
ENV = os.environ["ENVI"].strip().lower()
# Setup logging
logger = logging.getLogger(__name__)
def startup_sequence() -> None:
"""
This function contains the startup sequence for the application. It is
called at the beginning of the main() function.
"""
if ENV == "production":
logger.info("Running in production mode.")
else:
logger.info("Running in development mode.")
logger.info("Starting the application...")
# Schedule the jobs
# ...
logger.info("Application started.")
async def run_jobs() -> None:
"""
Asynchronous function that schedules and runs jobs based on a predefined schedule.
This function calculates the number of seconds until the next job is scheduled to run,
sleeps for that amount of time, and then runs the pending job. It continues this process
until there are no more jobs scheduled.
"""
# Get the number of seconds until the next job
seconds = schedule.idle_seconds()
# Run the job scheduler
while seconds is not None:
# If there are seconds to wait
if seconds > 0:
# Sleep exactly the right amount of time to the next job
await asyncio.sleep(seconds)
schedule.run_pending()
seconds = schedule.idle_seconds()
# # Way: Run the job scheduler continuously
# while True:
# schedule.run_pending()
# await asyncio.sleep(0.1)
# =============================================================================
# MARK: main()
# =============================================================================
def main() -> None:
"""
This is the main function. Need I say more?
"""
# Run the startup sequence
app.on_startup(startup_sequence)
app.on_startup(run_jobs)
# Add static files
app.add_static_files("/static", "static")
# Placeholder functionality
# ui.button("Click me!", on_click=lambda: ui.notify("Hello, World!"))
homepage.create()
if ENV == "production":
if (
os.environ.get("NATIVE") is not None
and os.environ.get("NATIVE").strip().lower() == "true"
):
ui.run(
reload=False,
native=True,
favicon="static/favicon.ico",
title="how is jskherman?",
storage_secret=os.environ.get("STORAGE_SECRET"),
)
else:
ui.run(
reload=False,
favicon="static/favicon.ico",
title="how is jskherman?",
storage_secret=os.environ.get("STORAGE_SECRET"),
)
else:
ui.run(
favicon="static/favicon.ico",
title="how is jskherman?",
storage_secret=os.environ.get("STORAGE_SECRET"),
)
if __name__ in {"__main__", "__mp_main__"}:
main()