Streamlit
Streamlit 기본 문법(9) : 사이드바 & selectbox, tab 활용
ISLA!
2023. 7. 28. 19:34
☘ 사이드바 활용 코드 예시
import matplotlib.pyplot as plt
import streamlit as st
import seaborn as sns
def main():
st.title("확인")
with st.sidebar:
st.header("Sidebar")
day = st.selectbox("요일 선택", ["Thur", "Fri", "Sat", "Sun"])
tips = sns.load_dataset("tips")
filtered_tips = tips.loc[tips['day'] == day]
#st.dataframe(filtered_tips)
top_bill = filtered_tips['total_bill'].max()
top_tip = filtered_tips['tip'].max()
tab1, tab2, tab3 = st.sidebar.tabs(['Total Bill', 'Tip', 'Size'])
with tab1:
fig, ax = plt.subplots()
st.header("Total Bill")
sns.histplot(filtered_tips['total_bill'], kde = False, ax = ax)
st.pyplot(fig)
with tab2:
st.metric("최고 금액: ",top_bill)
if __name__ == "__main__":
main()
1. 사이드바에 selectbox 삽입 : 옵션 (목, 금, 토, 일) 선택 가능
2. tips 데이터에서 selectbox의 옵션(요일)에 해당하는 데이터프레임 추출
3. 각 요일별 데이터프레임에서 top_bill 과 top_tip 추출
4. 사이드바에 탭 만들고, 각 탭 내용 정의하기
👉 결과

728x90