Skip to content

Commit 60248fd

Browse files
refactor: move to use st.Page
Previously the code in `pages/` directory was very redundant. It was essentially a wrapper around the `pipe(some_tiles, map(render))` code which rendered said `some_tiles`. This was in place because prior to release of st.Page in 1.38 the pages in Streamlit application were tied to the presence of files. Now the pages are constructed programmatically in a single python file. I changed the ordering of the functions in the pipeline a bit so that the sidebar completely rendered first before the tiles start rendering. Prior to this change, the use of `st.connection` lead to the session either not being created or `get_active_session` malfunctioning. I worked around it using the try/except mechanism which is not ideal and will need some further investigations. Closes #17
1 parent 1b5a102 commit 60248fd

8 files changed

Lines changed: 59 additions & 80 deletions

src/Authentication.py

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,67 @@
11
"""Main entrypoint. Contains tiles reporting on Authentication."""
22

33
import streamlit as st
4+
import toolz as t
5+
import toolz.curried as tc
6+
from snowflake.snowpark.context import get_active_session
47
from toolz import pipe
5-
from toolz.curried import map
8+
from toolz.curried import map as cmap
69

7-
from common.tiles import AuthTiles, render
10+
from common.tiles import (
11+
AuthTiles,
12+
ConfigurationManagementTiles,
13+
IdentityManagementTiles,
14+
LeastPrivilegedAccessTiles,
15+
May30TTPsGuidanceTiles,
16+
PrivilegedAccessTiles,
17+
SharingTiles,
18+
render,
19+
)
820
from common.utils import sidebar_footer
921

10-
# Before the tiles so it renders first
11-
sidebar_footer()
1222

13-
# Initiate the connection to ensure that get_active_session() calls will succeed
14-
st.connection("default", type="snowflake")
23+
def _mk_connection():
24+
"""Wrap around get_active_session to gracefully handle multiple windows open."""
25+
try:
26+
get_active_session()
27+
except Exception as e:
28+
# no default session is not a typed exception, need to parse the message
29+
if "No default Session is found" in e.message:
30+
st.connection("default", type="snowflake").session()
31+
else:
32+
raise
1533

16-
pipe(AuthTiles, map(render), list)
34+
35+
def _mk_page(tiles):
36+
"""Return the function that will actually render the page."""
37+
38+
def _inner():
39+
pipe(tiles, cmap(render), list)
40+
41+
return _inner
42+
43+
44+
pipe(
45+
(
46+
(AuthTiles, "Authentication"),
47+
(ConfigurationManagementTiles, "Configuration Management"),
48+
(SharingTiles, "Data Sharing"),
49+
(IdentityManagementTiles, "Identity Management"),
50+
(LeastPrivilegedAccessTiles, "Least Privileged Access"),
51+
(May30TTPsGuidanceTiles, "May 30 TTPs Guidance"),
52+
(PrivilegedAccessTiles, "Privileged Access"),
53+
),
54+
# Wrap in st.Page call
55+
# `url_path` is necessary otherwise streamlit will error out when trying to construct URLs
56+
cmap(lambda it: st.Page(_mk_page(it[0]), title=it[1], url_path=it[1])),
57+
# Materialize
58+
list,
59+
# Assemble navigation
60+
st.navigation,
61+
# Force render footer
62+
tc.do(lambda _: sidebar_footer()),
63+
# Connection is created here so that the sidebar is fully rendered by this point
64+
tc.do(lambda _: _mk_connection()),
65+
# Finally run the current page. If ordered earlier, the sidebar pages will not be rendered
66+
lambda it: it.run(),
67+
)

src/pages/Configuration_Management.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/pages/Data_Sharing.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/pages/Identity_Management.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/pages/Least_Privileged_Access.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/pages/May30_TTPs_Guidance.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/pages/Privileged_Access.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/snowflake.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ streamlit:
1212
title: Sentry
1313
main_file: Authentication.py
1414
env_file: environment.yml
15-
pages_dir: pages/
15+
# pages_dir: pages/
1616

1717
additional_source_files:
1818
- common/queries.py

0 commit comments

Comments
 (0)