Update app.py
Browse files
app.py
CHANGED
|
@@ -1,97 +1,97 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import requests
|
| 3 |
-
import pandas as pd
|
| 4 |
-
|
| 5 |
-
# API ํค์ ๊ธฐ๋ณธ URL ์ค์
|
| 6 |
-
api_key = "
|
| 7 |
-
base_url = "https://ecos.bok.or.kr/api/"
|
| 8 |
-
|
| 9 |
-
# API ํธ์ถ ํจ์๋ค
|
| 10 |
-
def get_service_statistics():
|
| 11 |
-
url = f"{base_url}StatisticTableList/{api_key}/json/kr/1/1000/"
|
| 12 |
-
response = requests.get(url)
|
| 13 |
-
return response.json()
|
| 14 |
-
|
| 15 |
-
def get_statistical_glossary():
|
| 16 |
-
url = f"{base_url}StatisticWordList/{api_key}/json/kr/1/1000/"
|
| 17 |
-
response = requests.get(url)
|
| 18 |
-
return response.json()
|
| 19 |
-
|
| 20 |
-
def get_statistic_detail_items(stat_code):
|
| 21 |
-
url = f"{base_url}StatisticItemList/{api_key}/json/kr/1/1000/{stat_code}"
|
| 22 |
-
response = requests.get(url)
|
| 23 |
-
return response.json()
|
| 24 |
-
|
| 25 |
-
def get_statistic_search_conditions(stat_code):
|
| 26 |
-
url = f"{base_url}StatisticSearch/{api_key}/json/kr/1/100/{stat_code}"
|
| 27 |
-
response = requests.get(url)
|
| 28 |
-
return response.json()
|
| 29 |
-
|
| 30 |
-
def get_top_100_statistics():
|
| 31 |
-
url = f"{base_url}KeyStatisticList/{api_key}/json/kr/1/100/"
|
| 32 |
-
response = requests.get(url)
|
| 33 |
-
return response.json()
|
| 34 |
-
|
| 35 |
-
def get_statistical_metadata():
|
| 36 |
-
url = f"{base_url}StatisticMeta/{api_key}/json/kr/1/100/"
|
| 37 |
-
response = requests.get(url)
|
| 38 |
-
return response.json()
|
| 39 |
-
|
| 40 |
-
# JSON ๋ฐ์ดํฐ๋ฅผ DataFrame์ผ๋ก ๋ณํํ์ฌ ํ์
|
| 41 |
-
def display_data_as_table(data):
|
| 42 |
-
if isinstance(data, dict):
|
| 43 |
-
if "StatisticItemList" in data:
|
| 44 |
-
df = pd.DataFrame(data["StatisticItemList"]["row"])
|
| 45 |
-
elif "StatisticTableList" in data:
|
| 46 |
-
df = pd.DataFrame(data["StatisticTableList"]["row"])
|
| 47 |
-
elif "StatisticWordList" in data:
|
| 48 |
-
df = pd.DataFrame(data["StatisticWordList"]["row"])
|
| 49 |
-
elif "KeyStatisticList" in data:
|
| 50 |
-
df = pd.DataFrame(data["KeyStatisticList"]["row"])
|
| 51 |
-
elif "StatisticMeta" in data:
|
| 52 |
-
df = pd.DataFrame(data["StatisticMeta"]["row"])
|
| 53 |
-
else:
|
| 54 |
-
st.write("๋ฐ์ดํฐ๋ฅผ ํ์ํ ์ ์์ต๋๋ค.")
|
| 55 |
-
return
|
| 56 |
-
st.dataframe(df)
|
| 57 |
-
else:
|
| 58 |
-
st.write("๋ฐ์ดํฐ ํ์์ด ์๋ชป๋์์ต๋๋ค.")
|
| 59 |
-
|
| 60 |
-
# Streamlit ์ฑ ๊ตฌ์ฑ
|
| 61 |
-
st.title("ํ๊ตญ์ํ ECOS API ๋ฐ์ดํฐ ์กฐํ")
|
| 62 |
-
st.sidebar.header("API ํญ๋ชฉ ์ ํ")
|
| 63 |
-
|
| 64 |
-
# ์ฌ์ด๋๋ฐ์์ ์ ํํ ์ ์๋ ํญ๋ชฉ
|
| 65 |
-
option = st.sidebar.selectbox(
|
| 66 |
-
"์กฐํํ ํญ๋ชฉ์ ์ ํํ์ธ์:",
|
| 67 |
-
("์๋น์ค ํต๊ณ ๋ชฉ๋ก", "ํต๊ณ์ฉ์ด์ฌ์ ", "ํต๊ณ ์ธ๋ถํญ๋ชฉ ๋ชฉ๋ก", "ํต๊ณ ์กฐํ ์กฐ๊ฑด ์ค์ ", "100๋ ํต๊ณ์งํ", "ํต๊ณ ๋ฉํDB")
|
| 68 |
-
)
|
| 69 |
-
|
| 70 |
-
# ๊ฐ ํญ๋ชฉ์ ๋ฐ๋ผ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๊ณ ํ๋ฉด์ ํ๋ก ์ถ๋ ฅ
|
| 71 |
-
if option == "์๋น์ค ํต๊ณ ๋ชฉ๋ก":
|
| 72 |
-
data = get_service_statistics()
|
| 73 |
-
display_data_as_table(data)
|
| 74 |
-
|
| 75 |
-
elif option == "ํต๊ณ์ฉ์ด์ฌ์ ":
|
| 76 |
-
data = get_statistical_glossary()
|
| 77 |
-
display_data_as_table(data)
|
| 78 |
-
|
| 79 |
-
elif option == "ํต๊ณ ์ธ๋ถํญ๋ชฉ ๋ชฉ๋ก":
|
| 80 |
-
stat_code = st.text_input("ํต๊ณ ์ฝ๋ ์
๋ ฅ:", "064Y001")
|
| 81 |
-
if stat_code:
|
| 82 |
-
data = get_statistic_detail_items(stat_code)
|
| 83 |
-
display_data_as_table(data)
|
| 84 |
-
|
| 85 |
-
elif option == "ํต๊ณ ์กฐํ ์กฐ๊ฑด ์ค์ ":
|
| 86 |
-
stat_code = st.text_input("ํต๊ณ ์ฝ๋ ์
๋ ฅ:", "064Y001")
|
| 87 |
-
if stat_code:
|
| 88 |
-
data = get_statistic_search_conditions(stat_code)
|
| 89 |
-
display_data_as_table(data)
|
| 90 |
-
|
| 91 |
-
elif option == "100๋ ํต๊ณ์งํ":
|
| 92 |
-
data = get_top_100_statistics()
|
| 93 |
-
display_data_as_table(data)
|
| 94 |
-
|
| 95 |
-
elif option == "ํต๊ณ ๋ฉํDB":
|
| 96 |
-
data = get_statistical_metadata()
|
| 97 |
-
display_data_as_table(data)
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
# API ํค์ ๊ธฐ๋ณธ URL ์ค์
|
| 6 |
+
api_key = "Z2FNKPFTMDSTHHR60KS4"
|
| 7 |
+
base_url = "https://ecos.bok.or.kr/api/"
|
| 8 |
+
|
| 9 |
+
# API ํธ์ถ ํจ์๋ค
|
| 10 |
+
def get_service_statistics():
|
| 11 |
+
url = f"{base_url}StatisticTableList/{api_key}/json/kr/1/1000/"
|
| 12 |
+
response = requests.get(url)
|
| 13 |
+
return response.json()
|
| 14 |
+
|
| 15 |
+
def get_statistical_glossary():
|
| 16 |
+
url = f"{base_url}StatisticWordList/{api_key}/json/kr/1/1000/"
|
| 17 |
+
response = requests.get(url)
|
| 18 |
+
return response.json()
|
| 19 |
+
|
| 20 |
+
def get_statistic_detail_items(stat_code):
|
| 21 |
+
url = f"{base_url}StatisticItemList/{api_key}/json/kr/1/1000/{stat_code}"
|
| 22 |
+
response = requests.get(url)
|
| 23 |
+
return response.json()
|
| 24 |
+
|
| 25 |
+
def get_statistic_search_conditions(stat_code):
|
| 26 |
+
url = f"{base_url}StatisticSearch/{api_key}/json/kr/1/100/{stat_code}"
|
| 27 |
+
response = requests.get(url)
|
| 28 |
+
return response.json()
|
| 29 |
+
|
| 30 |
+
def get_top_100_statistics():
|
| 31 |
+
url = f"{base_url}KeyStatisticList/{api_key}/json/kr/1/100/"
|
| 32 |
+
response = requests.get(url)
|
| 33 |
+
return response.json()
|
| 34 |
+
|
| 35 |
+
def get_statistical_metadata():
|
| 36 |
+
url = f"{base_url}StatisticMeta/{api_key}/json/kr/1/100/"
|
| 37 |
+
response = requests.get(url)
|
| 38 |
+
return response.json()
|
| 39 |
+
|
| 40 |
+
# JSON ๋ฐ์ดํฐ๋ฅผ DataFrame์ผ๋ก ๋ณํํ์ฌ ํ์
|
| 41 |
+
def display_data_as_table(data):
|
| 42 |
+
if isinstance(data, dict):
|
| 43 |
+
if "StatisticItemList" in data:
|
| 44 |
+
df = pd.DataFrame(data["StatisticItemList"]["row"])
|
| 45 |
+
elif "StatisticTableList" in data:
|
| 46 |
+
df = pd.DataFrame(data["StatisticTableList"]["row"])
|
| 47 |
+
elif "StatisticWordList" in data:
|
| 48 |
+
df = pd.DataFrame(data["StatisticWordList"]["row"])
|
| 49 |
+
elif "KeyStatisticList" in data:
|
| 50 |
+
df = pd.DataFrame(data["KeyStatisticList"]["row"])
|
| 51 |
+
elif "StatisticMeta" in data:
|
| 52 |
+
df = pd.DataFrame(data["StatisticMeta"]["row"])
|
| 53 |
+
else:
|
| 54 |
+
st.write("๋ฐ์ดํฐ๋ฅผ ํ์ํ ์ ์์ต๋๋ค.")
|
| 55 |
+
return
|
| 56 |
+
st.dataframe(df)
|
| 57 |
+
else:
|
| 58 |
+
st.write("๋ฐ์ดํฐ ํ์์ด ์๋ชป๋์์ต๋๋ค.")
|
| 59 |
+
|
| 60 |
+
# Streamlit ์ฑ ๊ตฌ์ฑ
|
| 61 |
+
st.title("ํ๊ตญ์ํ ECOS API ๋ฐ์ดํฐ ์กฐํ")
|
| 62 |
+
st.sidebar.header("API ํญ๋ชฉ ์ ํ")
|
| 63 |
+
|
| 64 |
+
# ์ฌ์ด๋๋ฐ์์ ์ ํํ ์ ์๋ ํญ๋ชฉ
|
| 65 |
+
option = st.sidebar.selectbox(
|
| 66 |
+
"์กฐํํ ํญ๋ชฉ์ ์ ํํ์ธ์:",
|
| 67 |
+
("์๋น์ค ํต๊ณ ๋ชฉ๋ก", "ํต๊ณ์ฉ์ด์ฌ์ ", "ํต๊ณ ์ธ๋ถํญ๋ชฉ ๋ชฉ๋ก", "ํต๊ณ ์กฐํ ์กฐ๊ฑด ์ค์ ", "100๋ ํต๊ณ์งํ", "ํต๊ณ ๋ฉํDB")
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# ๊ฐ ํญ๋ชฉ์ ๋ฐ๋ผ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๊ณ ํ๋ฉด์ ํ๋ก ์ถ๋ ฅ
|
| 71 |
+
if option == "์๋น์ค ํต๊ณ ๋ชฉ๋ก":
|
| 72 |
+
data = get_service_statistics()
|
| 73 |
+
display_data_as_table(data)
|
| 74 |
+
|
| 75 |
+
elif option == "ํต๊ณ์ฉ์ด์ฌ์ ":
|
| 76 |
+
data = get_statistical_glossary()
|
| 77 |
+
display_data_as_table(data)
|
| 78 |
+
|
| 79 |
+
elif option == "ํต๊ณ ์ธ๋ถํญ๋ชฉ ๋ชฉ๋ก":
|
| 80 |
+
stat_code = st.text_input("ํต๊ณ ์ฝ๋ ์
๋ ฅ:", "064Y001")
|
| 81 |
+
if stat_code:
|
| 82 |
+
data = get_statistic_detail_items(stat_code)
|
| 83 |
+
display_data_as_table(data)
|
| 84 |
+
|
| 85 |
+
elif option == "ํต๊ณ ์กฐํ ์กฐ๊ฑด ์ค์ ":
|
| 86 |
+
stat_code = st.text_input("ํต๊ณ ์ฝ๋ ์
๋ ฅ:", "064Y001")
|
| 87 |
+
if stat_code:
|
| 88 |
+
data = get_statistic_search_conditions(stat_code)
|
| 89 |
+
display_data_as_table(data)
|
| 90 |
+
|
| 91 |
+
elif option == "100๋ ํต๊ณ์งํ":
|
| 92 |
+
data = get_top_100_statistics()
|
| 93 |
+
display_data_as_table(data)
|
| 94 |
+
|
| 95 |
+
elif option == "ํต๊ณ ๋ฉํDB":
|
| 96 |
+
data = get_statistical_metadata()
|
| 97 |
+
display_data_as_table(data)
|