-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
257 lines (226 loc) · 8.14 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
from os import path
from typing import Literal
import streamlit as st
import polars as pl
import streamlit_antd_components as sac
from streamlit_extras.stylable_container import stylable_container
from importlib.metadata import version
from data import get_all_sheets
from events import on_select_sheet, on_search_snippet, on_click_home, on_click_struct, \
on_select_language, on_quicksearch, on_search_sheet
from sidebar import sheet_toc
from views.home_view import generate_landing_view
from views.sheet_view import generate_sheet_view, generate_filtered_sheet_view
from views.sheets_view import generate_sheets_results
from views.structure_view import generate_structure_view
st.set_page_config(
page_title='Actionsheets',
page_icon='📄',
initial_sidebar_state='expanded',
layout='wide'
)
print('\n== INIT APP ==')
active_lang: str = 'Python'
active_sheet_id: str = ''
active_view: Literal[
'home',
'sheets', 'sheets_result',
'sheet', 'sheet_result',
] = 'home'
static = st.session_state['static'] if 'static' in st.session_state else False
print('STATIC: ', static)
if 'lang' not in st.session_state:
# new session; set default language
st.session_state['lang'] = active_lang
st.session_state['view'] = active_view
static = True
else:
active_lang = st.session_state['lang']
active_view = st.session_state['view']
if 'sheet' not in st.session_state:
st.session_state['sheet'] = ''
else:
active_sheet_id = st.session_state['sheet']
print(f'\tView: {active_view}')
print(f'\tLang: {active_lang}')
print(f'\tSheet: {active_sheet_id}')
sheets = get_all_sheets()
# Import CSS file
with open(path.join('.streamlit', 'style.css')) as f:
st.sidebar.html(f'<style>{f.read()}</style><div id="top"/>')
# Sidebar
with st.sidebar:
with stylable_container(
key='home',
css_styles=[
'div {float: left; width: 100%; text-align: left;}',
'button {border-style: none; background-color: transparent; float:left;}',
'button:hover {color: inherit}',
'button:focus {color: inherit}',
'p {font-size: 24pt; font-weight: bold;}'
]
):
st.button(
label='Actionsheets',
use_container_width=True,
on_click=on_click_home
)
with stylable_container(
key='nav',
css_styles=[
'div {float: left; width: 100%; text-align: left; gap: 0;}',
'button {border-style: none; background-color: transparent; float:left;}',
'button:hover {background-color: gray; color: inherit;}',
'button:focus {background-color: gray; color: inherit}'
]
):
with st.container():
st.button(
key='home_nav',
label='🏠 Home',
on_click=on_click_home,
use_container_width=True
)
st.button(
key='struc_nav',
label='📄 Sheet structure',
on_click=on_click_struct,
use_container_width=True
)
sac.divider('Programming language', color='blue', size='xl')
langs = sorted(sheets.sheets_data.filter(pl.col('sheet_parent') == '')['title'])
st.selectbox(
key='lang_select',
label='Programming language',
placeholder='Select proglang',
index=0,
options=langs,
label_visibility='collapsed',
on_change=on_select_language
)
with st.sidebar:
st.text_input(
key='quick_search',
label='Search all',
placeholder='Quick snippet search',
label_visibility='collapsed',
on_change=on_quicksearch
)
sac.divider(label='Actionsheets', color='green', size='lg')
st.text_input(
key='search_sheet',
label='Search sheet',
placeholder='Search sheet',
label_visibility='collapsed',
on_change=on_search_sheet
)
def get_child_sheets(id: str) -> list[str]:
return sheets.sheets_data.filter(
pl.col('sheet_parent') == id
).sort('rank')['sheet'].to_list()
def get_nested_child_sheets(id: str) -> list[str]:
# get children in sorted order
child_ids = get_child_sheets(id)
ids = list()
ids.append(id)
for child_id in child_ids:
ids += get_nested_child_sheets(child_id)
return ids
sheet_ids = get_nested_child_sheets(active_lang.lower())
df_sheet = pl.DataFrame(pl.Series('sheet', sheet_ids))
sheets_data = df_sheet.join(sheets.sheets_data, on='sheet', how='left')
with stylable_container(
key='sheets_menu',
css_styles=[
'div {float: left; width: 100%; text-align: left; gap: 0;}',
'''button {
border-style: none;
background-color: transparent;
color: var(--sheet-color);
float:left;
min-height: 0;
padding: 0;
margin: -2px;
padding-left: 5px;
}''',
'button:hover {background-color: gray; color: inherit;}',
'button:focus {background-color: gray; color: inherit}'
]
):
with st.container():
for sheet, title, depth in zip(
sheets_data['sheet'], sheets_data['title'], sheets_data['depth']
):
st.button(
key=f'sheet_{sheet}',
label=' ' * (depth - 1) + ('▫️' if depth > 0 else '') + title,
use_container_width=True,
on_click=on_select_sheet,
args=(sheet,)
)
has_sheet = sheets.has_sheet(active_sheet_id)
with st.sidebar:
sac.divider(label='Actionsheet sections', color='yellow', size='md')
st.text_input(
key='search_snippet',
label='Search snippets',
placeholder='Search snippets',
label_visibility='collapsed',
disabled=not has_sheet,
on_change=on_search_snippet
)
if sheets.has_sheet(active_sheet_id):
sheet_toc(
sheet=sheets.sheet_view(active_sheet_id),
parent_section=''
)
sac.menu(
items=[
sac.MenuItem(type='divider'),
sac.MenuItem(
label='Actionsheets package',
disabled=True,
tag=sac.Tag(
label=f'v{version("actionsheets")}',
link=f'https://github.com/niekdt/actionsheets/releases/tag/v{version("actionsheets")}'
),
),
sac.MenuItem('Github', icon='github', href='https://github.com/niekdt/actionsheets'),
sac.MenuItem(
label='Submit an issue / snippet',
icon='question-circle',
href='https://github.com/niekdt/actionsheets/issues'
),
sac.MenuItem('Streamlit actionsheets app', disabled=True),
sac.MenuItem('Github', icon='github',
href='https://github.com/niekdt/actionsheets-streamlit'),
sac.MenuItem(
label='Submit an issue',
icon='question-circle',
href='https://github.com/niekdt/actionsheets/issues'
),
sac.MenuItem('Copyright © 2024 Niek Den Teuling', disabled=True)
],
size='xs',
indent=10
)
match active_view:
case 'home':
generate_landing_view()
case 'struct':
generate_structure_view()
case 'sheet':
if has_sheet:
generate_sheet_view()
else:
sac.result(label=f'Sheet "{active_sheet_id}" is undefined', status='error')
case 'sheet_result':
if has_sheet:
generate_filtered_sheet_view()
else:
sac.result(label=f'Sheet "{active_sheet_id}" is undefined', status='error')
case 'sheets_result':
generate_sheets_results()
case _:
sac.result(label=f'undefined view: {active_view}', status='error')
st.session_state['static'] = False