File size: 3,914 Bytes
9a5d6a9
 
 
 
508403c
6919349
605f57d
508403c
 
 
9a5d6a9
605f57d
508403c
 
c91d609
 
605f57d
508403c
605f57d
9a5d6a9
508403c
 
 
9a5d6a9
605f57d
508403c
b931666
 
 
 
 
 
605f57d
508403c
605f57d
9a5d6a9
b931666
 
 
9a5d6a9
605f57d
508403c
9312ce7
 
 
 
 
 
605f57d
508403c
605f57d
9a5d6a9
 
 
 
b931666
 
 
 
 
508403c
b931666
 
508403c
b931666
 
 
c91d609
 
b931666
 
 
 
 
 
 
 
 
9312ce7
b931666
c91d609
 
 
 
 
 
9a5d6a9
b931666
 
 
6919349
 
 
c91d609
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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)