Skip to content

Commit 4a0d613

Browse files
committed
Adjusting create_navigation_menu() and Logging
1 parent 8ade4e2 commit 4a0d613

17 files changed

+41
-52
lines changed

src/helper_activities_caching.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from helper_logging import get_logger_from_filename, track_function_usage
1313
from helper_pandas import reorder_cols
1414

15-
logger = get_logger_from_filename(__file__)
15+
LOGGER = get_logger_from_filename(__file__)
1616

1717

1818
DIR_SERVER = "/var/www/virtual/entorb/data-web-pages/strava"
@@ -150,7 +150,7 @@ def cache_all_activities_and_gears_in_year_range(
150150
- Activities using id as index, ordered by start_date_local
151151
- Gears, using id as index, ordered by index
152152
"""
153-
logger.info("cache_all_activities_and_gears_year for user_id=%s", user_id)
153+
LOGGER.info("cache_all_activities_and_gears_year for user_id=%s", user_id)
154154

155155
df = pd.DataFrame(fetch_all_activities(year_start=year_start, year_end=year_end))
156156

src/helper_api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from helper_logging import get_logger_from_filename, track_function_usage
1111

12-
logger = get_logger_from_filename(__file__)
12+
LOGGER = get_logger_from_filename(__file__)
1313

1414

1515
API_RETRIES = 5
@@ -36,7 +36,7 @@ def api_post_oauth(code: str) -> dict:
3636
resp.raise_for_status()
3737
return resp.json()
3838
except requests.RequestException:
39-
logger.exception("Attempt %i failed", attempt)
39+
LOGGER.exception("Attempt %i failed", attempt)
4040
# If it's the last attempt, raise the exception
4141
if attempt + 1 == API_RETRIES:
4242
raise
@@ -73,7 +73,7 @@ def api_post_deauthorize() -> None:
7373
def _api_get(path: str) -> dict | list:
7474
"""Get data from Strava API, used by fetch_* functions."""
7575
path = f"{URL_BASE}/{path}"
76-
logger.info(path)
76+
LOGGER.info(path)
7777

7878
headers = {"Authorization": f"Bearer {st.session_state['TOKEN']}"}
7979

@@ -84,7 +84,7 @@ def _api_get(path: str) -> dict | list:
8484
resp.raise_for_status()
8585
return resp.json()
8686
except requests.RequestException:
87-
logger.exception("Attempt %i failed", attempt)
87+
LOGGER.exception("Attempt %i failed", attempt)
8888
# If it's the last attempt, raise the exception
8989
if attempt + 1 == API_RETRIES:
9090
raise

src/helper_login.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
track_function_usage,
1414
)
1515

16-
logger = get_logger_from_filename(__file__)
16+
LOGGER = get_logger_from_filename(__file__)
1717

1818

1919
@track_function_usage

src/helper_pandas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from helper_logging import get_logger_from_filename, track_function_usage
66

7-
logger = get_logger_from_filename(__file__)
7+
LOGGER = get_logger_from_filename(__file__)
88

99

1010
@track_function_usage

src/helper_ui_components.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@
22

33
import io
44
from pathlib import Path
5-
from typing import TYPE_CHECKING
65

76
import pandas as pd
87
import streamlit as st
98
from streamlit.delta_generator import DeltaGenerator
9+
from streamlit.navigation.page import StreamlitPage
1010

1111
from helper_logging import get_logger_from_filename, track_function_usage
1212

13-
if TYPE_CHECKING:
14-
from streamlit.navigation.page import StreamlitPage
15-
16-
17-
logger = get_logger_from_filename(__file__)
13+
LOGGER = get_logger_from_filename(__file__)
1814

1915

2016
@track_function_usage
21-
def create_navigation_menu() -> str:
17+
def create_navigation_menu() -> StreamlitPage:
2218
"""Create and populate navigation menu."""
2319
lst: list[StreamlitPage] = []
2420
for p in sorted(Path("src/reports").glob("*.py")):
@@ -35,8 +31,7 @@ def create_navigation_menu() -> str:
3531

3632
lst.append(st.Page(page=f"reports/{f}.py", title=t))
3733
pg = st.navigation(lst)
38-
pg.run()
39-
return pg.url_path
34+
return pg
4035

4136

4237
@track_function_usage

src/main.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
MEASURE_MEMORY = True
2121
init_logging()
22-
logger = get_logger_from_filename(__file__)
22+
LOGGER = get_logger_from_filename(__file__)
2323

2424

2525
def set_env() -> None:
@@ -105,17 +105,20 @@ def main() -> None: # noqa: D103
105105
if MEASURE_MEMORY:
106106
tracemalloc.start()
107107
time_start = time()
108-
pagename = create_navigation_menu()
108+
page = create_navigation_menu()
109+
pagename = page.url_path or "main"
110+
st.title(page.title)
111+
112+
page.run()
113+
109114
time_end = time()
110-
if pagename == "":
111-
pagename = "main"
112115
log_line = f"stats: {pagename},{round(time_end - time_start, 1)}s"
113116

114117
if MEASURE_MEMORY:
115118
max_bytes = tracemalloc.get_traced_memory()[0]
116119
tracemalloc.stop()
117120
log_line += f",{round(max_bytes / 1_048_576, 1)}MB"
118-
logger.info(log_line)
121+
LOGGER.info(log_line)
119122

120123

121124
if __name__ == "__main__":
@@ -124,6 +127,6 @@ def main() -> None: # noqa: D103
124127
except Exception as e:
125128
# automatically triggered by logger.exception
126129
# sentry_sdk.capture_exception(e)
127-
logger.exception("Exception:")
130+
LOGGER.exception("Exception:")
128131
st.exception(e)
129132
st.stop()
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
)
88
from helper_logging import get_logger_from_filename
99

10-
st.title(__doc__[:-1]) # type: ignore
11-
logger = get_logger_from_filename(__file__)
10+
LOGGER = get_logger_from_filename(__file__)
1211

1312

1413
col1, _ = st.columns((1, 5))
@@ -41,7 +40,8 @@
4140
else:
4241
st.session_state["years"] = 100
4342

44-
df = cache_all_activities_and_gears()[0]
43+
with st.spinner(text="Fetching your activities", show_time=True):
44+
df = cache_all_activities_and_gears()[0]
4545

4646
# export activity_columns
4747
# lst = sorted(df.columns)

src/reports/r05_current_year.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
from helper_logging import get_logger_from_filename
1212
from helper_ui_components import list_sports
1313

14-
st.title(__doc__[:-1]) # type: ignore
15-
logger = get_logger_from_filename(__file__)
14+
LOGGER = get_logger_from_filename(__file__)
1615

1716

1817
def calc_days_in_year(year: int) -> int:

src/reports/r10_activity_list.py renamed to src/reports/r10_activity_list_and_excel_export.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
from helper_logging import get_logger_from_filename
1111
from helper_ui_components import excel_download_buttons, select_sport
1212

13-
st.title(__doc__[:-1]) # type: ignore
14-
logger = get_logger_from_filename(__file__)
13+
LOGGER = get_logger_from_filename(__file__)
1514

1615

1716
st.markdown(
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Activity Stats."""
1+
"""Activity Statistics."""
22

33
# ruff: noqa: PLR2004
44
import datetime as dt
@@ -15,8 +15,7 @@
1515
from helper_pandas import reorder_cols
1616
from helper_ui_components import excel_download_buttons, list_sports, select_sport
1717

18-
st.title(__doc__[:-1]) # type: ignore
19-
logger = get_logger_from_filename(__file__)
18+
LOGGER = get_logger_from_filename(__file__)
2019

2120

2221
AGGREGATIONS = {
@@ -254,6 +253,8 @@ def activity_stats_grouping(
254253
time_unit = "yearmonth(date):T"
255254
elif sel_freq == "Week":
256255
time_unit = "date:T"
256+
else:
257+
time_unit = "date:T"
257258

258259
c = (
259260
alt.Chart(
@@ -315,18 +316,15 @@ def get_cell(df: pd.DataFrame, sport: str, period: str, agg: str) -> float | int
315316
for i in range(4):
316317
col = cols[i]
317318
sport = sports[i]
319+
prev1, prev2, prev3 = 0, 0, 0
318320
col.subheader(sport)
319321
cur = get_cell(df=df2c, sport=sport, period=periods[0], agg=sel_agg)
320322
if len(periods) >= 2:
321323
prev1 = get_cell(df=df2c, sport=sport, period=periods[1], agg=sel_agg)
322-
else:
323-
prev1 = 0
324324
if len(periods) >= 3:
325325
prev2 = get_cell(df=df2c, sport=sport, period=periods[2], agg=sel_agg)
326326
if len(periods) >= 4:
327327
prev3 = get_cell(df=df2c, sport=sport, period=periods[3], agg=sel_agg)
328-
else:
329-
prev3 = 0
330328
col.metric(label=periods[0], value=cur)
331329
if len(periods) >= 2:
332330
delta = round(prev1 - prev2, 1) if len(periods) >= 3 else None

0 commit comments

Comments
 (0)