LazyHuman10 commited on
Commit ·
dcfde3c
1
Parent(s): e34605e
Clean up material hub file labels
Browse files- pages/Study_Material_Hub.py +32 -5
pages/Study_Material_Hub.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
|
|
| 1 |
from html import escape
|
|
|
|
| 2 |
from urllib.parse import quote
|
| 3 |
|
| 4 |
import streamlit as st
|
|
@@ -30,6 +32,24 @@ OFFICE_PREVIEW_MIMES = {
|
|
| 30 |
}
|
| 31 |
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
def display_pdf(file_content):
|
| 34 |
"""Display PDF using streamlit-pdf-viewer."""
|
| 35 |
pdf_viewer(file_content, width="100%", height=700)
|
|
@@ -41,7 +61,7 @@ def display_office_document(download_url, filename):
|
|
| 41 |
preview_url = f"https://view.officeapps.live.com/op/embed.aspx?src={encoded_url}"
|
| 42 |
components.iframe(preview_url, width=None, height=700, scrolling=True)
|
| 43 |
st.caption(
|
| 44 |
-
f"Inline preview for `{filename}` is powered by Office Web Viewer. "
|
| 45 |
"If it does not load, use the download button."
|
| 46 |
)
|
| 47 |
|
|
@@ -115,7 +135,14 @@ files_list = subject_data[selected_type]
|
|
| 115 |
file_names = [file_entry["name"] for file_entry in files_list]
|
| 116 |
with filter_cols[3]:
|
| 117 |
selected_file_name = (
|
| 118 |
-
st.selectbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
)
|
| 120 |
|
| 121 |
selected_file_obj = (
|
|
@@ -178,7 +205,7 @@ with preview_col:
|
|
| 178 |
f"""
|
| 179 |
<section class="plexi-panel">
|
| 180 |
<div class="plexi-kicker">{selected_semester}</div>
|
| 181 |
-
<div class="plexi-sidecard-title">{selected_file_obj["name"]}</div>
|
| 182 |
<div class="plexi-muted">
|
| 183 |
{selected_subject} / {selected_type}
|
| 184 |
</div>
|
|
@@ -244,7 +271,7 @@ with info_col:
|
|
| 244 |
</div>
|
| 245 |
<div class="plexi-meta-row">
|
| 246 |
<div class="plexi-meta-key">Format</div>
|
| 247 |
-
<div class="plexi-meta-value">{escape(file_mime)}</div>
|
| 248 |
</div>
|
| 249 |
</section>
|
| 250 |
""",
|
|
@@ -264,7 +291,7 @@ with info_col:
|
|
| 264 |
item_class = "current" if file_name == selected_file_obj["name"] else ""
|
| 265 |
label = "Current" if file_name == selected_file_obj["name"] else "Available"
|
| 266 |
bucket_items.append(
|
| 267 |
-
f'<li class="{item_class}">{escape(label)}: {escape(file_name)}</li>'
|
| 268 |
)
|
| 269 |
st.markdown(
|
| 270 |
f'<section class="plexi-meta"><ul class="plexi-filelist">{"".join(bucket_items)}</ul></section>',
|
|
|
|
| 1 |
+
import re
|
| 2 |
from html import escape
|
| 3 |
+
from pathlib import Path
|
| 4 |
from urllib.parse import quote
|
| 5 |
|
| 6 |
import streamlit as st
|
|
|
|
| 32 |
}
|
| 33 |
|
| 34 |
|
| 35 |
+
def format_file_label(filename):
|
| 36 |
+
"""Return a cleaner display label for a stored filename."""
|
| 37 |
+
stem = Path(filename).stem
|
| 38 |
+
return re.sub(r"[._-]+", " ", stem).strip() or filename
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def get_file_type_label(filename, file_mime):
|
| 42 |
+
"""Return a short human-readable file type label."""
|
| 43 |
+
suffix = Path(filename).suffix.lower().lstrip(".")
|
| 44 |
+
if suffix:
|
| 45 |
+
return suffix
|
| 46 |
+
if file_mime.startswith("text/"):
|
| 47 |
+
return "text"
|
| 48 |
+
if file_mime == "application/pdf":
|
| 49 |
+
return "pdf"
|
| 50 |
+
return file_mime.rsplit("/", 1)[-1]
|
| 51 |
+
|
| 52 |
+
|
| 53 |
def display_pdf(file_content):
|
| 54 |
"""Display PDF using streamlit-pdf-viewer."""
|
| 55 |
pdf_viewer(file_content, width="100%", height=700)
|
|
|
|
| 61 |
preview_url = f"https://view.officeapps.live.com/op/embed.aspx?src={encoded_url}"
|
| 62 |
components.iframe(preview_url, width=None, height=700, scrolling=True)
|
| 63 |
st.caption(
|
| 64 |
+
f"Inline preview for `{format_file_label(filename)}` is powered by Office Web Viewer. "
|
| 65 |
"If it does not load, use the download button."
|
| 66 |
)
|
| 67 |
|
|
|
|
| 135 |
file_names = [file_entry["name"] for file_entry in files_list]
|
| 136 |
with filter_cols[3]:
|
| 137 |
selected_file_name = (
|
| 138 |
+
st.selectbox(
|
| 139 |
+
"File",
|
| 140 |
+
file_names,
|
| 141 |
+
key="hub_file",
|
| 142 |
+
format_func=format_file_label,
|
| 143 |
+
)
|
| 144 |
+
if file_names
|
| 145 |
+
else None
|
| 146 |
)
|
| 147 |
|
| 148 |
selected_file_obj = (
|
|
|
|
| 205 |
f"""
|
| 206 |
<section class="plexi-panel">
|
| 207 |
<div class="plexi-kicker">{selected_semester}</div>
|
| 208 |
+
<div class="plexi-sidecard-title">{format_file_label(selected_file_obj["name"])}</div>
|
| 209 |
<div class="plexi-muted">
|
| 210 |
{selected_subject} / {selected_type}
|
| 211 |
</div>
|
|
|
|
| 271 |
</div>
|
| 272 |
<div class="plexi-meta-row">
|
| 273 |
<div class="plexi-meta-key">Format</div>
|
| 274 |
+
<div class="plexi-meta-value">{escape(get_file_type_label(selected_file_obj["name"], file_mime).upper())}</div>
|
| 275 |
</div>
|
| 276 |
</section>
|
| 277 |
""",
|
|
|
|
| 291 |
item_class = "current" if file_name == selected_file_obj["name"] else ""
|
| 292 |
label = "Current" if file_name == selected_file_obj["name"] else "Available"
|
| 293 |
bucket_items.append(
|
| 294 |
+
f'<li class="{item_class}">{escape(label)}: {escape(format_file_label(file_name))}</li>'
|
| 295 |
)
|
| 296 |
st.markdown(
|
| 297 |
f'<section class="plexi-meta"><ul class="plexi-filelist">{"".join(bucket_items)}</ul></section>',
|