ECOS_BOK / app.py
jonghhhh's picture
Update app.py
c91d609 verified
import streamlit as st
import requests
import pandas as pd
# API ν‚€ μ„€μ •
api_key = "Z2FNKPFTMDSTHHR60KS4"
# 톡계 μ½”λ“œ 쑰회 ν•¨μˆ˜
def get_statistic_codes():
url = f"https://ecos.bok.or.kr/api/StatisticTableList/{api_key}/json/kr/1/1000"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
df = pd.DataFrame(data['StatisticTableList']['row'])
df['μ½”λ“œμ™€ 이름'] = df['STAT_CODE'] + ": " + df['STAT_NAME']
return df[['STAT_CODE', 'μ½”λ“œμ™€ 이름']]
else:
st.error(f"Failed to retrieve data: {response.status_code}")
return None
# 톡계 데이터 쑰회 ν•¨μˆ˜
def get_statistic_data(stat_code, period, start_date, end_date):
url = f"https://ecos.bok.or.kr/api/StatisticSearch/{api_key}/json/kr/1/1000/{stat_code}/{period}/{start_date}/{end_date}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
if 'StatisticSearch' in data:
df = pd.DataFrame(data['StatisticSearch']['row'])
return df
else:
st.error("데이터가 μ—†μŠ΅λ‹ˆλ‹€. μž…λ ₯ν•œ 쑰건을 ν™•μΈν•˜μ„Έμš”.")
return None
else:
st.error(f"Failed to retrieve data: {response.status_code}")
return None
# 100λŒ€ ν†΅κ³„μ§€ν‘œ 전체 쑰회 ν•¨μˆ˜
def get_top_100_statistics():
url = f"https://ecos.bok.or.kr/api/KeyStatisticList/{api_key}/json/kr/1/1000"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
if 'KeyStatisticList' in data:
df = pd.DataFrame(data['KeyStatisticList']['row'])
return df
else:
st.error("100λŒ€ ν†΅κ³„μ§€ν‘œ 데이터λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.")
return None
else:
st.error(f"Failed to retrieve data: {response.status_code}")
return None
# Streamlit μ•± ꡬ성
st.title("ν•œκ΅­μ€ν–‰ ECOS API 데이터 쑰회")
# μ™Όμͺ½ μ‚¬μ΄λ“œλ°”μ—μ„œ 선택 메뉴 ꡬ성
menu = st.sidebar.selectbox(
"메뉴λ₯Ό μ„ νƒν•˜μ„Έμš”",
("톡계 μ½”λ“œ 쑰회 및 데이터 뢄석", "100λŒ€ ν†΅κ³„μ§€ν‘œ μ‹€μ‹œκ°„ 확인")
)
if menu == "톡계 μ½”λ“œ 쑰회 및 데이터 뢄석":
st.header("톡계 μ½”λ“œ 쑰회 및 데이터 뢄석")
# 톡계 μ½”λ“œ 쑰회 및 선택
stat_codes_df = get_statistic_codes()
if stat_codes_df is not None:
selected_code_name = st.selectbox("μ‘°νšŒν•  톡계 μ½”λ“œλ₯Ό μ„ νƒν•˜μ„Έμš”:", stat_codes_df['μ½”λ“œμ™€ 이름'])
selected_code = selected_code_name.split(": ")[0] # 톡계 μ½”λ“œλ§Œ μΆ”μΆœ
# 톡계 데이터 쑰회 및 좜λ ₯
period = st.text_input("쑰회 μ£ΌκΈ° μž…λ ₯ (예: A, Q, M):", "A")
start_date = st.text_input("검색 μ‹œμž‘μΌμž μž…λ ₯ (예: 2015, 2015Q1, 201501, 20150101):", "2015")
end_date = st.text_input("검색 μ’…λ£ŒμΌμž μž…λ ₯ (예: 2021, 2021Q1, 202101, 20210101):", "2021")
if st.button("톡계 데이터 쑰회"):
stat_data_df = get_statistic_data(selected_code, period, start_date, end_date)
if stat_data_df is not None:
st.write(f"μ„ νƒν•œ 톡계 μ½”λ“œ: {selected_code}")
st.dataframe(stat_data_df)
# Excel 파일둜 μ €μž₯
st.write("데이터λ₯Ό Excel 파일둜 μ €μž₯ν•˜λ €λ©΄ μ•„λž˜ λ²„νŠΌμ„ ν΄λ¦­ν•˜μ„Έμš”.")
if st.button("Excel둜 μ €μž₯"):
stat_data_df.to_excel(f"{selected_code}_data.xlsx", index=False)
st.success(f"데이터가 {selected_code}_data.xlsx 파일둜 μ €μž₯λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
elif menu == "100λŒ€ ν†΅κ³„μ§€ν‘œ μ‹€μ‹œκ°„ 확인":
st.header("100λŒ€ ν†΅κ³„μ§€ν‘œ μ‹€μ‹œκ°„ 확인")
top_100_df = get_top_100_statistics()
if top_100_df is not None:
st.dataframe(top_100_df)