streamlit1 / app.py
SeungHyun111's picture
Update app.py
4fcf868 verified
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import font_manager as fm
# ํฐํŠธ ํŒŒ์ผ ๊ฒฝ๋กœ ์„ค์ •
font_path = "NanumGothic.ttf" # ํฐํŠธ ํŒŒ์ผ์ด ์žˆ๋Š” ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
# ํฐํŠธ ๋“ฑ๋ก
font_prop = fm.FontProperties(fname=font_path)
plt.rcParams['font.family'] = font_prop.get_name()
# Excel ํŒŒ์ผ ๊ฒฝ๋กœ
excel_path = "์ƒ์œ„_5_๋„์„œ.xlsx"
# Excel ํŒŒ์ผ์˜ ์‹œํŠธ๋“ค์„ ์ฝ์–ด์˜ค๊ธฐ
@st.cache_data
def load_excel_sheets(file_path):
xls = pd.ExcelFile(file_path)
sheets = {}
for sheet_name in xls.sheet_names:
sheets[sheet_name] = pd.read_excel(xls, sheet_name)
return sheets
sheets = load_excel_sheets(excel_path)
sheet_names = list(sheets.keys())
# Streamlit ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜
st.title('์—ฐ๋ น/์„ฑ๋ณ„์— ๋”ฐ๋ฅธ ์ƒ์œ„ 5๊ฐœ ๋Œ€์ถœ ๋„์„œ')
# ์นดํ…Œ๊ณ ๋ฆฌ ์„ ํƒ
selected_category = st.selectbox('์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ์„ ํƒํ•˜์„ธ์š”:', sheet_names)
# ์„ ํƒํ•œ ์นดํ…Œ๊ณ ๋ฆฌ์˜ Excel ๋ฐ์ดํ„ฐ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
df = sheets[selected_category]
# ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„ ํ™•์ธ
st.write(f'์„ ํƒํ•œ ์นดํ…Œ๊ณ ๋ฆฌ: {selected_category}')
st.dataframe(df)
# ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์—์„œ ์ƒ์œ„ 5๊ฐœ ๋„์„œ์™€ ๋Œ€์ถœ ๊ฑด์ˆ˜ ์ถ”์ถœ
top_5_books = df.head(5)
# ๋ง‰๋Œ€ ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ
fig, ax = plt.subplots(figsize=(10, 6)) # ๊ทธ๋ž˜ํ”„ ํฌ๊ธฐ ์กฐ์ •
ax.bar(top_5_books['๋„์„œ๋ช…'], top_5_books['๋Œ€์ถœ๊ฑด์ˆ˜'])
# ๋ ˆ์ด๋ธ” ํšŒ์ „ ๋ฐ ๋ ˆ์ด๋ธ” ๊ฐ„๊ฒฉ ์กฐ์ •
ax.set_xlabel('๋„์„œ๋ช…')
ax.set_ylabel('๋Œ€์ถœ๊ฑด์ˆ˜')
ax.set_title(f'{selected_category} - ์ƒ์œ„ 5๊ฐœ ๋„์„œ ๋Œ€์ถœ ๊ฑด์ˆ˜')
# X์ถ• ๋ ˆ์ด๋ธ” ํšŒ์ „
plt.xticks(rotation=45, ha='right')
# Streamlit์—์„œ ๊ทธ๋ž˜ํ”„ ํ‘œ์‹œ
st.pyplot(fig)