Language&Framework/Python
[Python] Streamlit 사용법
JUNG씨
2023. 2. 18. 02:04
Streamlit이란?
- 공식 문서 : The fastest way to build and share data apps
- 가장 빠르게 데이터 어플리케이션을 만들 수 있는 방법
- 앱을 만드는 미니멀한 프레임워크
- 21년 2월 기준 Github Star 13K
- 장점
- 간단하게 파이썬 코드로 앱을 빌드할 수 있음
- 인터랙티브한 기능 제공(백엔드 개발이나 HTTP 요청 구현할 필요 없음)
- 다양한 예시 제공
- 커뮤니티에서 개발한 Component도 존재
- Streamlit에서 배포할 수 있는 시스템 제공(단, 신청 필요)
- 화면을 녹화할 수 있는 Record 기능도 제공
- app을 빌드한 후, 오른쪽 ☰ 버튼을 클릭하면 Record a screencast를 확인할 수 있음
- Awesome Streamlit Github에 참고할 수 있는 자료가 매우 많음
Streamlit 설치 및 실행
- 가상환경 실행
python3 -m venv "가상환경이름"
source "가상환경이름"/bin/activate
- streamlit 설치
pip install streamlit
- 설치 확인
streamlit hello
- 공식문서에 있는 샘플코드 실행 https://docs.streamlit.io/library/get-started/create-an-app
# app.py
import streamlit as st
import pandas as pd
import numpy as np
st.title('Uber pickups in NYC')
DATE_COLUMN = 'date/time'
DATA_URL = ('https://s3-us-west-2.amazonaws.com/'
'streamlit-demo-data/uber-raw-data-sep14.csv.gz')
@st.cache_data
def load_data(nrows):
data = pd.read_csv(DATA_URL, nrows=nrows)
lowercase = lambda x: str(x).lower()
data.rename(lowercase, axis='columns', inplace=True)
data[DATE_COLUMN] = pd.to_datetime(data[DATE_COLUMN])
return data
data_load_state = st.text('Loading data...')
data = load_data(10000)
data_load_state.text("Done! (using st.cache_data)")
if st.checkbox('Show raw data'):
st.subheader('Raw data')
st.write(data)
st.subheader('Number of pickups by hour')
hist_values = np.histogram(data[DATE_COLUMN].dt.hour, bins=24, range=(0,24))[0]
st.bar_chart(hist_values)
# Some number in the range 0-23
hour_to_filter = st.slider('hour', 0, 23, 17)
filtered_data = data[data[DATE_COLUMN].dt.hour == hour_to_filter]
st.subheader('Map of all pickups at %s:00' % hour_to_filter)
st.map(filtered_data)
streamlit run app.py
- http://localhost:8501/로 접속 가능
다양한 Streamlit API
- 제목 설정하기
- 캐싱하기
- 위젯 만들기
- 버튼 만들기
- 체크 박스 만들기
- 라디오 버튼 만들기
- 선택 박스 만들기
- 다중 선택 박스 만들기
- 슬라이더 만들기
- 데이터 입력
- 데이터 출력
- 차트 출력
- Progress, Status 메세지
- 각종 컨텐츠 출력
- 사이드바, 레이아웃
👇🏻 아래 공식문서에 들어가면 다양한 API의 사용법이 나와있으니 필요할 때 참고하면 좋을것 같다!
https://docs.streamlit.io/library/api-reference
Streamlit Docs
Join the community Streamlit is more than just a way to make data apps, it's also a community of creators that share their apps and ideas and help each other make their work better. Please come join us on the community forum. We love to hear your questions
docs.streamlit.io
Streamlit 배포하기
- streamlit cloud를 이용해 간단하게 배포가 가능하다.
- github의 repository에 코드를 올리고 해당 repository 링크만 등록하면 배포가 가능하다.