-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
103 lines (92 loc) · 3.23 KB
/
app.py
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
import streamlit as st
import pandas as pd
import os
import base64
from functools import lru_cache
from tracyllm import main as tracyllm_main
# Page configuration
st.set_page_config(layout="centered", page_title="Nyack Library", page_icon="🏠")
# Load the Excel file
@st.cache_data
def load_data():
filepath = os.path.join(os.getcwd(), 'APP Layout.xlsx')
return pd.read_excel(filepath)
@lru_cache(maxsize=100)
def format_link(cell_content):
if ';' in cell_content:
parts = cell_content.split(';')
if len(parts) == 2:
link, title = parts
if link.startswith("http://") or link.startswith("https://"):
return f"[{title}]({link})"
return cell_content
@st.cache_data
def load_images():
img = {}
img_data = {}
for col_name in data.columns:
image_path = os.path.join("assets", f"{col_name}.jpg")
if os.path.exists(image_path):
with open(image_path, "rb") as f:
img_data[col_name] = base64.b64encode(f.read()).decode("utf-8")
img[col_name] = f"data:image/jpeg;base64,{img_data[col_name]}"
return img
data = load_data()
img = load_images()
# Custom CSS
st.markdown("""
<style>
.main {
max-width: 800px;
margin: auto;
}
.stButton > button {
width: 100%;
height: auto;
white-space: normal !important;
word-wrap: break-word;
padding: 0.25rem;
font-size: 0.7rem;
min-height: 0;
}
.category-image {
width: 100%;
max-width: 80px;
height: auto;
margin-bottom: 0.25rem;
}
[data-testid="column"] {
width: calc(33.33% - 1rem) !important;
flex: 1 1 calc(33.33% - 1rem) !important;
min-width: calc(33.33% - 1rem) !important;
}
</style>
""", unsafe_allow_html=True)
# App title
st.title('I have an unhoused patron or I need help with...')
# Search functionality
with st.container():
text_search = st.text_input("Search Training Material", placeholder="Enter your search query.")
if text_search:
results = tracyllm_main(text_search)
st.markdown(results, unsafe_allow_html=True)
# Category buttons in 3x3 grid
with st.container():
columns_per_segment = 3
cols = st.columns(3)
for i, col in enumerate(cols):
with col:
for j in range(columns_per_segment):
index = i * columns_per_segment + j
if index < len(data.columns):
col_name = data.columns[index]
if col_name in img:
st.image(img[col_name], use_column_width='auto')
if st.button(col_name, key=f"col{i}_{j}", use_container_width=True):
st.session_state['selected_column'] = col_name
# Display selected category data
if 'selected_column' in st.session_state and st.session_state['selected_column'] is not None:
st.write(f"### {st.session_state['selected_column']} Resources")
for item in data[st.session_state['selected_column']].dropna().tolist():
formatted_item = format_link(str(item))
st.markdown(formatted_item, unsafe_allow_html=True)