Library / streamlit1.py
SeungHyun111's picture
Upload 2 files
8721276 verified
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import font_manager as fm
# ํฐํŠธ ํŒŒ์ผ ๊ฒฝ๋กœ ์„ค์ •
font_path = r"C:\Users\user\Desktop\๋„์„œ๊ด€_๊ณต๋ชจ์ „\์ตœ์ข…\H2GTRM.TTF"
# ํฐํŠธ ๋“ฑ๋ก
font_prop = fm.FontProperties(fname=font_path)
plt.rcParams['font.family'] = font_prop.get_name()
# Excel ํŒŒ์ผ ๊ฒฝ๋กœ
excel_path = "C:/Users/user/Desktop/๋„์„œ๊ด€_๊ณต๋ชจ์ „/์ตœ์ข…/12_๋‹ค๋Œ€์ถœ๊ทธ๋ฃน/๋„์„œ ๋Œ€์ถœ ํŒจํ„ด ๋ถ„์„/์ƒ์œ„_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)