Skip to content

Commit 2433c3f

Browse files
Merge pull request #72 from CausalInferenceLab/71-multi-page-application-setting-file-refactoring
refact: streamlit 멀티 페이징 모듈 리펙토링 및 검증함수 추가
2 parents a59caa3 + 62b0e3b commit 2433c3f

File tree

1 file changed

+72
-7
lines changed

1 file changed

+72
-7
lines changed

interface/streamlit_app.py

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,75 @@
1+
"""
2+
Streamlit 멀티 페이지 애플리케이션 설정 파일.
3+
4+
- PAGES 딕셔너리로 페이지 정보(page 경로 및 제목)를 관리합니다.
5+
- PAGES를 기반으로 Streamlit 네비게이션 메뉴를 생성하고 실행합니다.
6+
"""
7+
18
import streamlit as st
29

3-
pg = st.navigation(
4-
[
5-
st.Page("lang2sql.py", title="Lang2SQL"),
6-
st.Page("viz_eval.py", title="Lang2SQL Evaluation 시각화"),
7-
]
8-
)
10+
PAGES = {
11+
"lang2sql": {
12+
"page": "lang2sql.py",
13+
"title": "Lang2SQL",
14+
},
15+
"lang2sql_eval_viz": {
16+
"page": "viz_eval.py",
17+
"title": "Lang2SQL Evaluation 시각화",
18+
},
19+
}
20+
21+
22+
def validate_pages(
23+
*,
24+
pages_dict: dict,
25+
) -> None:
26+
"""
27+
PAGES 딕셔너리의 구조와 값을 검증합니다.
28+
29+
검증 항목:
30+
- 최상위 객체는 딕셔너리(dict)여야 합니다.
31+
- 각 항목은 'page'와 'title' 키를 가진 딕셔너리여야 합니다.
32+
- 'page'와 'title' 값은 비어 있지 않은 문자열이어야 합니다.
33+
34+
오류 발생:
35+
- 조건을 만족하지 않으면 ValueError를 발생시킵니다.
36+
37+
Args:
38+
pages_dict (dict): 페이지 정보가 담긴 딕셔너리
39+
40+
Raises:
41+
ValueError: 유효하지 않은 구조나 값을 가진 경우
42+
"""
43+
44+
if not isinstance(pages_dict, dict):
45+
raise ValueError("PAGES must be a dictionary.")
46+
47+
for key, page_info in pages_dict.items():
48+
if not isinstance(page_info, dict):
49+
raise ValueError(
50+
f"Each page info must be a dictionary. Error at key: {key}"
51+
)
52+
53+
if "page" not in page_info or "title" not in page_info:
54+
raise ValueError(
55+
f"Each page must have 'page' and 'title' fields. Error at key: {key}"
56+
)
57+
58+
if not isinstance(page_info["page"], str) or not page_info["page"]:
59+
raise ValueError(f"'page' must be a non-empty string. Error at key: {key}")
60+
61+
if not isinstance(page_info["title"], str) or not page_info["title"]:
62+
raise ValueError(f"'title' must be a non-empty string. Error at key: {key}")
63+
64+
65+
validate_pages(pages_dict=PAGES)
66+
67+
pages = [
68+
st.Page(
69+
page=page_info["page"],
70+
title=page_info["title"],
71+
)
72+
for page_info in PAGES.values()
73+
]
974

10-
pg.run()
75+
st.navigation(pages).run()

0 commit comments

Comments
 (0)