RandomForest / app.py
dltmdgus's picture
Update app.py
96240e1 verified
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import font_manager
import streamlit as st
from io import BytesIO
# μ‚¬μš©μž μ§€μ • 폰트 파일 경둜
font_path = r"H2GTRM.TTF"
# μ‚¬μš©μž μ§€μ • 폰트λ₯Ό matplotlib에 μΆ”κ°€
font_prop = font_manager.FontProperties(fname=font_path)
font_manager.fontManager.addfont(font_path)
# matplotlib의 κΈ°λ³Έ 폰트λ₯Ό μ‚¬μš©μž μ§€μ • 폰트둜 μ„€μ •
plt.rcParams['font.family'] = font_prop.get_name()
# μ—‘μ…€ νŒŒμΌμ„ μ½μ–΄μ˜΅λ‹ˆλ‹€.
file_path = r"book_analysis_predictions_summary.xlsx"
df = pd.read_excel(file_path)
# λ„μ„œλͺ… 열을 인덱슀둜 μ„€μ •ν•©λ‹ˆλ‹€.
df.set_index('λ„μ„œλͺ…', inplace=True)
# 월별 열을 μΆ”μΆœν•©λ‹ˆλ‹€.
months = ['2024λ…„ 7μ›”', '2024λ…„ 8μ›”', '2024λ…„ 9μ›”', '2024λ…„ 10μ›”', '2024λ…„ 11μ›”', '2024λ…„ 12μ›”']
# Streamlit μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ UI ꡬ성
st.title('랜덀포레슀트 기반 λ„μ„œ λŒ€μΆœκ±΄μˆ˜ 예츑')
# λ„μ„œλͺ…을 검색할 수 μžˆλŠ” μž…λ ₯ ν•„λ“œ 제곡
book_name = st.text_input('λ„μ„œλͺ…을 μž…λ ₯ν•˜μ„Έμš”:')
# λ„μ„œλͺ…을 μž…λ ₯ν–ˆμ„ λ•Œλ§Œ κ·Έλž˜ν”„ 생성
if book_name:
if book_name in df.index:
# λ„μ„œλ³„ κ·Έλž˜ν”„ 생성
fig, ax = plt.subplots()
ax.plot(months, df.loc[book_name, months], marker='o', label=book_name)
ax.set_title(f'{book_name} 월별 λŒ€μΆœκ±΄μˆ˜ 뢄석')
ax.set_xlabel('μ›”')
ax.set_ylabel('λŒ€μΆœκ±΄μˆ˜')
ax.set_xticklabels(months, rotation=45)
ax.legend(title='λ„μ„œλͺ…')
ax.grid(True)
plt.tight_layout() # κ·Έλž˜ν”„μ˜ λ ˆμ΄μ•„μ›ƒμ„ μ‘°μ •
# κ·Έλž˜ν”„λ₯Ό Streamlitμ—μ„œ ν‘œμ‹œν•˜κΈ° μœ„ν•΄ BytesIO둜 μ €μž₯
buf = BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
st.image(buf, use_column_width=True)
# κ·Έλž˜ν”„ νŒŒμΌμ„ μ €μž₯
plt.close(fig)
else:
st.error('ν•΄λ‹Ή λ„μ„œλͺ…을 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.')