Skip to content

refact: streamlit 멀티 페이징 모듈 리펙토링 및 검증함수 추가 #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 72 additions & 7 deletions interface/streamlit_app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,75 @@
"""
Streamlit 멀티 페이지 애플리케이션 설정 파일.

- PAGES 딕셔너리로 페이지 정보(page 경로 및 제목)를 관리합니다.
- PAGES를 기반으로 Streamlit 네비게이션 메뉴를 생성하고 실행합니다.
"""

import streamlit as st

pg = st.navigation(
[
st.Page("lang2sql.py", title="Lang2SQL"),
st.Page("viz_eval.py", title="Lang2SQL Evaluation 시각화"),
]
)
PAGES = {
"lang2sql": {
"page": "lang2sql.py",
"title": "Lang2SQL",
},
"lang2sql_eval_viz": {
"page": "viz_eval.py",
"title": "Lang2SQL Evaluation 시각화",
},
}


def validate_pages(
*,
pages_dict: dict,
) -> None:
"""
PAGES 딕셔너리의 구조와 값을 검증합니다.

검증 항목:
- 최상위 객체는 딕셔너리(dict)여야 합니다.
- 각 항목은 'page'와 'title' 키를 가진 딕셔너리여야 합니다.
- 'page'와 'title' 값은 비어 있지 않은 문자열이어야 합니다.

오류 발생:
- 조건을 만족하지 않으면 ValueError를 발생시킵니다.

Args:
pages_dict (dict): 페이지 정보가 담긴 딕셔너리

Raises:
ValueError: 유효하지 않은 구조나 값을 가진 경우
"""

if not isinstance(pages_dict, dict):
raise ValueError("PAGES must be a dictionary.")

for key, page_info in pages_dict.items():
if not isinstance(page_info, dict):
raise ValueError(
f"Each page info must be a dictionary. Error at key: {key}"
)

if "page" not in page_info or "title" not in page_info:
raise ValueError(
f"Each page must have 'page' and 'title' fields. Error at key: {key}"
)

if not isinstance(page_info["page"], str) or not page_info["page"]:
raise ValueError(f"'page' must be a non-empty string. Error at key: {key}")

if not isinstance(page_info["title"], str) or not page_info["title"]:
raise ValueError(f"'title' must be a non-empty string. Error at key: {key}")


validate_pages(pages_dict=PAGES)

pages = [
st.Page(
page=page_info["page"],
title=page_info["title"],
)
for page_info in PAGES.values()
]

pg.run()
st.navigation(pages).run()