Spaces:
Sleeping
Sleeping
Anna Zhou commited on
Commit ·
d7faa86
1
Parent(s): c9da65d
Prevent nav labels from breaking words
Browse files- app/app.py +111 -44
app/app.py
CHANGED
|
@@ -5,8 +5,11 @@ import ast
|
|
| 5 |
import uuid
|
| 6 |
import os
|
| 7 |
from datetime import datetime, timezone
|
|
|
|
| 8 |
from pathlib import Path
|
|
|
|
| 9 |
import streamlit as st
|
|
|
|
| 10 |
|
| 11 |
sys.path.insert(0, str(Path(__file__).parent.parent))
|
| 12 |
from rag.pipeline import RAGProviderError, generate_answer, retrieve_chunks
|
|
@@ -145,62 +148,126 @@ def _render_apa_source_links(raw_links, limit: int | None = None) -> None:
|
|
| 145 |
st.markdown(f" - [PDF]({link['pdf_url']})")
|
| 146 |
|
| 147 |
def _render_navigation(current_page: str, conversation_id: str) -> None:
|
| 148 |
-
|
| 149 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
<style>
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
}
|
| 155 |
-
|
| 156 |
-
min-height: 2.25rem;
|
| 157 |
-
width: 100%;
|
| 158 |
-
border-radius: 6px;
|
| 159 |
-
white-space: normal;
|
| 160 |
-
line-height: 1.15;
|
| 161 |
-
padding: 0.45rem 0.65rem;
|
| 162 |
-
}
|
| 163 |
-
.rfr-nav-brand {
|
| 164 |
-
min-height: 2.25rem;
|
| 165 |
display: flex;
|
| 166 |
align-items: center;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
color: #17324d;
|
| 168 |
font-weight: 700;
|
| 169 |
font-size: 1.05rem;
|
| 170 |
white-space: nowrap;
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
border-bottom: 1px solid #dfe4ea;
|
| 174 |
-
margin: 0.
|
| 175 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
</style>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
""",
|
| 178 |
-
|
| 179 |
)
|
| 180 |
|
| 181 |
-
def nav_button(col, page: str, label: str) -> None:
|
| 182 |
-
if col.button(
|
| 183 |
-
label,
|
| 184 |
-
key=f"nav_{page}",
|
| 185 |
-
type="primary" if page == current_page else "secondary",
|
| 186 |
-
disabled=page == current_page,
|
| 187 |
-
):
|
| 188 |
-
_set_query_param("disclaimer_accepted", "true")
|
| 189 |
-
_set_query_param("conversation_id", conversation_id)
|
| 190 |
-
_set_query_param("page", page)
|
| 191 |
-
st.rerun()
|
| 192 |
-
|
| 193 |
-
cols = st.columns([3.4, 0.75, 1.55, 1.05, 1.95, 1.25, 0.8], gap="small")
|
| 194 |
-
cols[0].markdown('<div class="rfr-nav-brand">RFR Knowledge Platform</div>', unsafe_allow_html=True)
|
| 195 |
-
nav_button(cols[1], "ask", "Ask")
|
| 196 |
-
nav_button(cols[2], "previous_responses", "Previous Responses")
|
| 197 |
-
nav_button(cols[3], "how_to_use", "How to Use")
|
| 198 |
-
nav_button(cols[4], "new_case_requests", "New Case Study Requests")
|
| 199 |
-
nav_button(cols[5], "case_studies", "Case Studies")
|
| 200 |
-
nav_button(cols[6], "about", "About")
|
| 201 |
-
|
| 202 |
-
st.markdown('<div class="rfr-nav-rule"></div>', unsafe_allow_html=True)
|
| 203 |
-
|
| 204 |
|
| 205 |
def _case_sort_key(case_id: str) -> int:
|
| 206 |
m = re.search(r"\d+", case_id or "")
|
|
|
|
| 5 |
import uuid
|
| 6 |
import os
|
| 7 |
from datetime import datetime, timezone
|
| 8 |
+
from html import escape
|
| 9 |
from pathlib import Path
|
| 10 |
+
from urllib.parse import urlencode
|
| 11 |
import streamlit as st
|
| 12 |
+
import streamlit.components.v1 as components
|
| 13 |
|
| 14 |
sys.path.insert(0, str(Path(__file__).parent.parent))
|
| 15 |
from rag.pipeline import RAGProviderError, generate_answer, retrieve_chunks
|
|
|
|
| 148 |
st.markdown(f" - [PDF]({link['pdf_url']})")
|
| 149 |
|
| 150 |
def _render_navigation(current_page: str, conversation_id: str) -> None:
|
| 151 |
+
nav_items = [
|
| 152 |
+
("ask", "Ask"),
|
| 153 |
+
("previous_responses", "Previous Responses"),
|
| 154 |
+
("how_to_use", "How to Use"),
|
| 155 |
+
("new_case_requests", "New Case Study Requests"),
|
| 156 |
+
("case_studies", "Case Studies"),
|
| 157 |
+
("about", "About"),
|
| 158 |
+
]
|
| 159 |
+
buttons = []
|
| 160 |
+
for page, label in nav_items:
|
| 161 |
+
classes = "rfr-nav-button is-active" if page == current_page else "rfr-nav-button"
|
| 162 |
+
query = urlencode({
|
| 163 |
+
"disclaimer_accepted": "true",
|
| 164 |
+
"conversation_id": conversation_id,
|
| 165 |
+
"page": page,
|
| 166 |
+
})
|
| 167 |
+
buttons.append(
|
| 168 |
+
f'<button class="{classes}" type="button" data-query="{escape(query)}">'
|
| 169 |
+
f'{escape(label)}</button>'
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
components.html(
|
| 173 |
+
f"""
|
| 174 |
<style>
|
| 175 |
+
body {{
|
| 176 |
+
margin: 0;
|
| 177 |
+
font-family: sans-serif;
|
| 178 |
+
}}
|
| 179 |
+
.rfr-nav {{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
display: flex;
|
| 181 |
align-items: center;
|
| 182 |
+
gap: 0.8rem 1.25rem;
|
| 183 |
+
width: 100%;
|
| 184 |
+
box-sizing: border-box;
|
| 185 |
+
}}
|
| 186 |
+
.rfr-nav-brand {{
|
| 187 |
color: #17324d;
|
| 188 |
font-weight: 700;
|
| 189 |
font-size: 1.05rem;
|
| 190 |
white-space: nowrap;
|
| 191 |
+
margin-right: auto;
|
| 192 |
+
}}
|
| 193 |
+
.rfr-nav-buttons {{
|
| 194 |
+
display: flex;
|
| 195 |
+
flex-wrap: wrap;
|
| 196 |
+
justify-content: flex-end;
|
| 197 |
+
align-items: center;
|
| 198 |
+
gap: 0.5rem 0.65rem;
|
| 199 |
+
min-width: 0;
|
| 200 |
+
}}
|
| 201 |
+
.rfr-nav-button {{
|
| 202 |
+
min-height: 2.25rem;
|
| 203 |
+
padding: 0.45rem 0.8rem;
|
| 204 |
+
border: 1px solid #d1d5db;
|
| 205 |
+
border-radius: 6px;
|
| 206 |
+
background: #ffffff;
|
| 207 |
+
color: #2f3340;
|
| 208 |
+
font: inherit;
|
| 209 |
+
font-size: 1rem;
|
| 210 |
+
line-height: 1.15;
|
| 211 |
+
white-space: normal;
|
| 212 |
+
overflow-wrap: normal;
|
| 213 |
+
word-break: keep-all;
|
| 214 |
+
cursor: pointer;
|
| 215 |
+
}}
|
| 216 |
+
.rfr-nav-button:hover:not(:disabled) {{
|
| 217 |
+
border-color: #aeb6c2;
|
| 218 |
+
background: #f8fafc;
|
| 219 |
+
color: #17324d;
|
| 220 |
+
}}
|
| 221 |
+
.rfr-nav-button.is-active {{
|
| 222 |
+
background: #edf4f8;
|
| 223 |
+
border-color: #d6e3ea;
|
| 224 |
+
color: #17324d;
|
| 225 |
+
font-weight: 600;
|
| 226 |
+
cursor: default;
|
| 227 |
+
}}
|
| 228 |
+
.rfr-nav-rule {{
|
| 229 |
border-bottom: 1px solid #dfe4ea;
|
| 230 |
+
margin-top: 0.75rem;
|
| 231 |
+
}}
|
| 232 |
+
@media (max-width: 980px) {{
|
| 233 |
+
.rfr-nav {{
|
| 234 |
+
align-items: flex-start;
|
| 235 |
+
flex-direction: column;
|
| 236 |
+
gap: 0.65rem;
|
| 237 |
+
}}
|
| 238 |
+
.rfr-nav-brand {{
|
| 239 |
+
margin-right: 0;
|
| 240 |
+
}}
|
| 241 |
+
.rfr-nav-buttons {{
|
| 242 |
+
justify-content: flex-start;
|
| 243 |
+
gap: 0.45rem 0.55rem;
|
| 244 |
+
}}
|
| 245 |
+
.rfr-nav-button {{
|
| 246 |
+
padding: 0.42rem 0.7rem;
|
| 247 |
+
}}
|
| 248 |
+
}}
|
| 249 |
</style>
|
| 250 |
+
<nav class="rfr-nav" aria-label="Primary navigation">
|
| 251 |
+
<div class="rfr-nav-brand">RFR Knowledge Platform</div>
|
| 252 |
+
<div class="rfr-nav-buttons">
|
| 253 |
+
{"".join(buttons)}
|
| 254 |
+
</div>
|
| 255 |
+
</nav>
|
| 256 |
+
<div class="rfr-nav-rule"></div>
|
| 257 |
+
<script>
|
| 258 |
+
const buttons = document.querySelectorAll(".rfr-nav-button");
|
| 259 |
+
buttons.forEach((button) => {{
|
| 260 |
+
button.addEventListener("click", () => {{
|
| 261 |
+
if (button.classList.contains("is-active")) return;
|
| 262 |
+
const query = button.dataset.query;
|
| 263 |
+
window.parent.location.href = window.parent.location.pathname + "?" + query;
|
| 264 |
+
}});
|
| 265 |
+
}});
|
| 266 |
+
</script>
|
| 267 |
""",
|
| 268 |
+
height=116,
|
| 269 |
)
|
| 270 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
|
| 272 |
def _case_sort_key(case_id: str) -> int:
|
| 273 |
m = re.search(r"\d+", case_id or "")
|