이번에는 KOSIS(국가통계포털) 에서 제공하는 데이터를 통해 간단한 조회 화면을 만들어 봤습니다.
회원 가입하고 나면 이 데이터를 활용할수 있도록 신청을 해야하는데요..
활용신청 메뉴에서 신청하면 됩니다.
신청하고 나면 바로 사용자 인증키가 생성됩니다.
다음 개발가이드 메뉴에서 통계목록를 조회하는 화면을 Windsurf 를 통해
Claude 3.5 Sonnet 으로 생성 명령을 주었습니다.
화면은 Streamlit 이라는 파이썬에서 웹 애플리케이션을 쉽고 빠르게 생성해주는 오픈소스 라이브러리입니다.
생성된 화면은 아래와 같습니다.
이것을 생성하기 위한 파이썬 코드는 ...
import requests
import pandas as pd
import streamlit as st
import json
def fetch_statistics_list():
"""
KOSIS API를 통해 통계목록을 조회합니다.
"""
API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # API 키는 실제 발급받은 키로 변경해야 합니다
BASE_URL = 'https://kosis.kr/openapi/statisticsList.do'
params = {
'method': 'getList',
'apiKey': API_KEY,
'vwCd': 'MT_ZTITLE',
'parentListId' : 'I1_6',
'format': 'json',
'jsonVD': 'Y'
}
try:
info_placeholder = st.empty()
info_placeholder.info("KOSIS 통계목록 조회 중...")
response = requests.get(BASE_URL, params=params)
# API 응답 상태 확인
st.write("응답 상태 코드:", response.status_code)
if response.status_code == 200:
# JSON 데이터 파싱
data = response.json()
# 데이터프레임으로 변환
df = pd.DataFrame(data)
# 주요 컬럼 선택 및 이름 변경
if not df.empty:
selected_columns = {
'VW_CD': '서비스뷰',
'LIST_ID': '목록ID',
'TBL_NM': '목록명',
'ORG_ID': '기관코드',
'TBL_ID': '통계표ID',
'STAT_ID': '통계조사ID',
'SEND_DE': '최종갱신일',
'REC_TBL_SE': '추천통계표여부'
}
# 존재하는 컬럼만 선택
existing_columns = [col for col in selected_columns.keys() if col in df.columns]
df_selected = df[existing_columns].copy()
# 컬럼명 한글로 변경
df_selected.columns = [selected_columns[col] for col in existing_columns]
info_placeholder.info("KOSIS 통계목록 조회 완료")
return df_selected
else:
st.error("조회된 데이터가 없습니다.")
return pd.DataFrame()
else:
st.error(f"API 호출 실패: {response.status_code}")
return pd.DataFrame()
except Exception as e:
st.error(f"오류 발생: {str(e)}")
return pd.DataFrame()
def main():
st.set_page_config(layout="wide")
st.title("KOSIS 통계목록 조회")
if st.button("통계목록 조회"):
df = fetch_statistics_list()
if not df.empty:
st.write("### 조회된 통계목록")
st.dataframe(df, use_container_width=True, height=600)
# CSV 다운로드 버튼
csv = df.to_csv(index=False, encoding='utf-8-sig')
st.download_button(
label="CSV 파일 다운로드",
data=csv,
file_name="kosis_statistics_list.csv",
mime="text/csv"
)
else:
st.warning("조회할 통계목록이 없습니다.")
if __name__ == "__main__":
main()
자동으로 생성하다보니 colum 이 맞지 않아서 제대로 표시되지 않았는데..
이 부분만 조금 수정을 하고 실행하면 바로 브라우저가 호출되어 보여집니다.
streamlit 실행 명령은 아래와 같습니다.
더보기
streamlit run statistics_list.py
그외 다양한 개발가이드가 있어서 잘 활용하면 좋은 결과물을 가질수 있을거라 생각됩니다.
'Python' 카테고리의 다른 글
fastAPI + JWT 인증 2 - 토큰 생성 및 검증, 관리 (0) | 2025.04.05 |
---|---|
fastAPI + JWT 인증 1 (0) | 2025.04.05 |
python+fastAPI+postgresql 로 구성된 환경에서 간단한 CRUD 구현2 (0) | 2025.03.28 |
python+fastAPI+postgresql 로 구성된 환경에서 간단한 CRUD 구현1 (0) | 2025.03.28 |
FastAPI + Python 으로 구성한 Back-End (0) | 2025.03.26 |