Repository navigation
Expand file tree
/
Copy pathshared_sidebar.py
More file actions
142 lines (129 loc) · 6.49 KB
/
Copy pathshared_sidebar.py
File metadata and controls
142 lines (129 loc) · 6.49 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
import streamlit as st
import html
from utils_auth import check_session
def create_sidebar(page_name=""):
"""
Create a consistent sidebar for all pages
Args:
page_name: Optional identifier for the current page to create unique button keys
"""
# Enforce session expiry on every page render. ``check_session`` clears the
# authentication keys when the stored expiry has passed.
if st.session_state.get("authenticated", False) and check_session() is None:
st.warning("Your session has expired. Please log in again.")
# Fixed width CSS to prevent sidebar trembling
fixed_width_css = """
<style>
section[data-testid="stSidebar"] {
width: 18rem !important;
min-width: 18rem !important;
max-width: 18rem !important;
transition: none !important;
}
</style>
"""
st.markdown(fixed_width_css, unsafe_allow_html=True)
# Create a container for the sidebar with fixed width styling
with st.sidebar:
# Only show logo in sidebar for authenticated users to avoid duplication
if st.session_state.get("authenticated", False):
# Apply button styles
button_style = """
<style>
div[data-testid="stVerticalBlock"] div.stButton > button {
width: 100%;
border: none;
padding: 15px 15px;
text-align: left;
font-size: 16px;
font-weight: 500;
border-radius: 10px;
margin-bottom: 8px;
display: flex;
align-items: center;
background-color: #f0f8ff;
transition: none !important;
}
div[data-testid="stVerticalBlock"] div.stButton > button:hover {
background-color: #e1f5fe;
border-left: 4px solid #1e88e5;
}
.sidebar-content {
padding-top: 1rem;
padding-bottom: 1rem;
}
</style>
"""
st.markdown(button_style, unsafe_allow_html=True)
# Center the logo horizontally and make it bigger
col1, col2, col3 = st.columns([1, 3, 1])
with col2:
st.image("assets/logo.png", width=160)
# Title already included in the logo
safe_username = html.escape(str(st.session_state.get("username", "")))
safe_role = html.escape(str(st.session_state.get("role", "")))
st.markdown(f"<div style='text-align:center; margin-bottom:10px;'>Welcome, <b>{safe_username}</b> ({safe_role})</div>", unsafe_allow_html=True)
st.markdown("---")
# Create a container with a colored background and rounded corners for the navigation menu
# Use a specific height to prevent content shifting
menu_container = st.container()
with menu_container:
# Add menu buttons with prominent icons and colored backgrounds
menu_options = {
"chat": {"icon": "💬", "label": "Chat", "path": "pages/chat.py", "color": "#e3f2fd"},
"history": {"icon": "📜", "label": "History", "path": "pages/history.py", "color": "#e8f5e9"},
"settings": {"icon": "⚙️", "label": "Settings", "path": "pages/settings.py", "color": "#fafafa"},
"models": {"icon": "🤖", "label": "Model Manager", "path": "pages/model_manager.py", "color": "#ede7f6"},
"home": {"icon": "🏠", "label": "Home", "path": "app.py", "color": "#e0f7fa"}
}
# Admin-only options
if st.session_state.get("role", "") == "admin":
menu_options["admin"] = {"icon": "👑", "label": "Admin Panel", "path": "pages/admin.py", "color": "#f3e5f5"}
else:
# Remove Model Manager option for non-admin users
if "models" in menu_options:
del menu_options["models"]
# Create buttons for each menu option - with consistent widths
for key, option in menu_options.items():
# Use unique keys for each page to ensure they don't conflict
button_key = f"btn_{key}_{page_name}"
col1, col2 = st.columns([1, 5])
with col1:
st.markdown(f'<div style="font-size:24px; text-align:center">{option["icon"]}</div>', unsafe_allow_html=True)
with col2:
if st.button(option["label"], key=button_key, use_container_width=True):
st.switch_page(option["path"])
st.markdown("---")
theme_col, logout_col = st.columns(2)
with theme_col:
theme_key = f"theme_toggle_{page_name}"
# Choose icon based on current theme
icon = "☀️" if st.session_state.get("dark_mode", False) else "🌙"
button_text = f"{icon} Theme"
tooltip = "Switch to Light Mode" if st.session_state.get("dark_mode", False) else "Switch to Dark Mode"
if st.button(button_text, key=theme_key, help=tooltip):
# Toggle directly; importing app.py from a page would re-execute the
# whole entry script (including st.set_page_config).
st.session_state.dark_mode = not st.session_state.get("dark_mode", False)
st.rerun()
with logout_col:
logout_key = f"logout_button_{page_name}"
if st.button("🚪 Logout", key=logout_key, help="Log out"):
keys_to_clear = [
"authenticated",
"username",
"user_id",
"role",
"user_info",
"current_conversation_id",
"conversations",
"must_change_password",
"azure_auth_state",
"azure_token_cache",
"azure_user",
"sidebar_created",
]
for key in keys_to_clear:
if key in st.session_state:
del st.session_state[key]
st.rerun()