Skip to content

Commit a59caa3

Browse files
Merge pull request #70 from CausalInferenceLab/feature/69-lang2sql-cli-code-refactoring
refact: lang2sql CLI 관련 코드 리펙토링
2 parents 3d98da4 + 5965350 commit a59caa3

File tree

1 file changed

+115
-15
lines changed

1 file changed

+115
-15
lines changed

cli/__init__.py

+115-15
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,135 @@
1-
import click
1+
"""
2+
Datahub GMS 서버 URL을 설정하고, 필요 시 Streamlit 인터페이스를 실행하는 CLI 프로그램입니다.
3+
"""
4+
25
import subprocess
6+
7+
import click
8+
39
from llm_utils.tools import set_gms_server
410

511

612
@click.group()
713
@click.version_option(version="0.1.4")
814
@click.pass_context
915
@click.option(
10-
"--datahub_server", default="http://localhost:8080", help="Datahub GMS 서버 URL"
16+
"--datahub_server",
17+
default="http://localhost:8080",
18+
help=(
19+
"Datahub GMS 서버의 URL을 설정합니다. "
20+
"기본값은 'http://localhost:8080'이며, "
21+
"운영 환경 또는 테스트 환경에 맞게 변경할 수 있습니다."
22+
),
23+
)
24+
@click.option(
25+
"--run-streamlit",
26+
is_flag=True,
27+
help=(
28+
"이 옵션을 지정하면 CLI 실행 시 Streamlit 애플리케이션을 바로 실행합니다. "
29+
"별도의 명령어 입력 없이 웹 인터페이스를 띄우고 싶을 때 사용합니다."
30+
),
31+
)
32+
@click.option(
33+
"-p",
34+
"--port",
35+
type=int,
36+
default=8501,
37+
help=(
38+
"Streamlit 서버가 바인딩될 포트 번호를 지정합니다. "
39+
"기본 포트는 8501이며, 포트 충돌을 피하거나 여러 인스턴스를 실행할 때 변경할 수 있습니다."
40+
),
1141
)
12-
@click.option("--run-streamlit", is_flag=True, help="Run the Streamlit app.")
13-
@click.option("-p", "--port", type=int, default=8501, help="Streamlit port")
14-
def cli(ctx, datahub_server, run_streamlit, port):
42+
# pylint: disable=redefined-outer-name
43+
def cli(
44+
ctx: click.Context,
45+
datahub_server: str,
46+
run_streamlit: bool,
47+
port: int,
48+
) -> None:
49+
"""
50+
Datahub GMS 서버 URL을 설정하고, Streamlit 애플리케이션을 실행할 수 있는 CLI 명령 그룹입니다.
51+
52+
이 함수는 다음 역할을 수행합니다:
53+
- 전달받은 'datahub_server' URL을 바탕으로 GMS 서버 연결을 설정합니다.
54+
- 설정 과정 중 오류가 발생하면 오류 메시지를 출력하고 프로그램을 종료합니다.
55+
- '--run-streamlit' 옵션이 활성화된 경우, 지정된 포트에서 Streamlit 웹 앱을 즉시 실행합니다.
56+
57+
매개변수:
58+
ctx (click.Context): 명령어 실행 컨텍스트 객체입니다.
59+
datahub_server (str): 설정할 Datahub GMS 서버의 URL입니다.
60+
run_streamlit (bool): Streamlit 앱을 실행할지 여부를 나타내는 플래그입니다.
61+
port (int): Streamlit 서버가 바인딩될 포트 번호입니다.
62+
63+
주의:
64+
'set_gms_server' 함수에서 ValueError가 발생할 경우, 프로그램은 비정상 종료(exit code 1)합니다.
65+
"""
66+
1567
try:
1668
set_gms_server(datahub_server)
1769
except ValueError as e:
18-
click.echo(str(e))
70+
click.secho(f"GMS 서버 URL 설정 실패: {str(e)}", fg="red")
1971
ctx.exit(1)
2072
if run_streamlit:
2173
run_streamlit_command(port)
2274

2375

24-
def run_streamlit_command(port):
25-
"""Run the Streamlit app."""
26-
subprocess.run(
27-
["streamlit", "run", "interface/streamlit_app.py", "--server.port", str(port)]
28-
)
76+
def run_streamlit_command(port: int) -> None:
77+
"""
78+
지정된 포트에서 Streamlit 애플리케이션을 실행하는 함수입니다.
79+
80+
이 함수는 subprocess를 통해 'streamlit run' 명령어를 실행하여
81+
'interface/streamlit_app.py' 파일을 웹 서버 형태로 구동합니다.
82+
사용자가 지정한 포트 번호를 Streamlit 서버의 포트로 설정합니다.
83+
84+
매개변수:
85+
port (int): Streamlit 서버가 바인딩될 포트 번호입니다.
86+
87+
주의:
88+
- Streamlit이 시스템에 설치되어 있어야 정상 동작합니다.
89+
- subprocess 호출 실패 시 예외가 발생할 수 있습니다.
90+
"""
91+
92+
try:
93+
subprocess.run(
94+
[
95+
"streamlit",
96+
"run",
97+
"interface/streamlit_app.py",
98+
"--server.port",
99+
str(port),
100+
],
101+
check=True,
102+
)
103+
except subprocess.CalledProcessError as e:
104+
click.echo(f"Streamlit 실행 실패: {e}")
105+
raise
106+
107+
108+
@cli.command(name="run-streamlit")
109+
@click.option(
110+
"-p",
111+
"--port",
112+
type=int,
113+
default=8501,
114+
help=(
115+
"Streamlit 애플리케이션이 바인딩될 포트 번호를 지정합니다. "
116+
"기본 포트는 8501이며, 필요 시 포트 충돌을 피하거나 "
117+
"여러 인스턴스를 동시에 실행할 때 다른 포트 번호를 설정할 수 있습니다."
118+
),
119+
)
120+
def run_streamlit_cli_command(port: int) -> None:
121+
"""
122+
CLI 명령어를 통해 Streamlit 애플리케이션을 실행하는 함수입니다.
123+
124+
이 명령은 'interface/streamlit_app.py' 파일을 Streamlit 서버로 구동하며,
125+
사용자가 지정한 포트 번호를 바인딩하여 웹 인터페이스를 제공합니다.
126+
127+
매개변수:
128+
port (int): Streamlit 서버가 사용할 포트 번호입니다. 기본값은 8501입니다.
29129
130+
주의:
131+
- Streamlit이 시스템에 설치되어 있어야 정상적으로 실행됩니다.
132+
- Streamlit 실행에 실패할 경우 subprocess 호출에서 예외가 발생할 수 있습니다.
133+
"""
30134

31-
@cli.command()
32-
@click.option("-p", "--port", type=int, default=8501, help="Streamlit port")
33-
def run_streamlit(port):
34-
"""Run the Streamlit app."""
35135
run_streamlit_command(port)

0 commit comments

Comments
 (0)