Python 라이브러리, Streamlit 소개입니다.
목차
- Streamlit 이란?
- Streamlit 설치와 세팅
- 몇가지 Component API 소개
- 배포하기
- 마치며
- 참고 자료
Streamlit 이란?
제목에서 알 수 있듯, 굉장히 빠르고 쉽게 웹앱을 만들어 배포할 수 있는 라이브러리 입니다. 단순한 웹 앱이 아닌 대시보드, 챗봇 같은 앱들을 만들고 배포할 수 있습니다.
공식 문서에서 말하는 장점은 아래와 같습니다.
- Simple : 파이썬 답게 읽고 쓰기 쉬운 코드
- 빠르고 interactive 한 프로토타이핑 : 데이터와 바로 바로 피드백을 주고 받음
- Live Edit : 코드 수정 후, 재 build 하지 않아도 자동으로 업데이트
- 무료 오픈 소스
이러한 장점 때문에 프로토 타이핑에 매우 적절한데요,
모든 서비스를 코드 레벨에서 테스트 하는 것보다, 간단한 대시보드 등이 있으면 더 빠르게 시각화하고, 서빙할 수 있기 때문입니다. 특히 Front End에 약한 ML/AI 개발자나 데이터 엔지니어들에게 매우 유용한 라이브러리 입니다.
streamlit 예시 서비스는 아래 링크에서 확인 가능합니다.
Chatbot
Starter examples for building LLM apps with Streamlit.
llm-examples.streamlit.app
- map 시각화 : https://prettymapp.streamlit.app/
prettymapp
Prettymapp is based on a rewrite of the fantastic prettymaps project by@marceloprates. All credit...
prettymapp.streamlit.app
Streamlit 설치, 실행
공식 문서에 나온 예시입니다.
pip install streamlit # pip 사용시
poetry add streamlit # poetry 사용시
간단한 예시는 아래와 같습니다.
# streamlit_app.py
import streamlit as st
# 슬라이더 생성
x = st.slider("Select a value")
# 생성된 값 출력
st.write(x, "squared is", x * x)
작성된 app 을 바로 터미널에서 웹앱으로 실행할 수 있습니다.
$ streamlit run streamlit_app.py
위처럼 코드를 실행하면, local URL과 Network URL 두 가지 모두로 접속할 수 있습니다.
몇가지 Component API 소개
# 기본적으로 import 되어 있다고 가정합니다.
import streamlit as st
import pandas as pd
import numpy as np
1. 버튼
if st.button('Say hello'): # Say hello 버튼 생성
st.write('Why hello there')
else:
st.write('Goodbye')
2. 슬라이더
st.subheader('Slider') # 슬라이더 생성
age = st.slider('당신의 나이는?', 0, 130, 25) # 슬라이더 설명, 범위, 초기값
st.write("나는 ", age, '살입니다')
3. Line 차트
st.line_chart(data)
4. 선택 박스 select box, 멀티 선택 multiselect
option = st.selectbox(
"How would you like to be contacted?",
("Email", "Home phone", "Mobile phone"))
options = st.multiselect(
"What are your favorite colors",
["Green", "Yellow", "Red", "Blue"],
["Yellow", "Red"])
5. 사이드 바 생성
# 일반 실행
st.sidebar.[element_name]...
# "with" notation
with st.sidebar:
st.[element_name]...
배포하기
그냥 웹앱이기 때문에 배포하는 방법은 다양하지만,
가장 쉽게 배포하는 방법은 Streamlit의 community cloud 를 사용하는 것입니다.
무료이기 때문에, 쉽게 사용 가능합니다.
https://share.streamlit.io/
share.streamlit.io
1. Github 생성
우선, github에 repo를 생성합니다.
그리고 그 repo 에 위에서 작성한 것과 같은 streamlit_app.py 를 push 합니다.
2. streamlit community cloud 계정 생성 후 연동
계정을 생성하고, create app을 선택합니다.
github와 연동되고, 해당 repo 에 있는, 선택된 main file 로 웹앱이 생성됩니다.
생성이 완료되면, github repo에 push 될때 마다 자동으로 업데이트 됩니다.
무료 서비스이기 때문에, 계정 별로 제한이 존재합니다.
마치며
저도 아직 적극적으로 사용해보지 않았지만,
이미 생성된 웹앱들을 보면 굉장히 다양하게 활용 가능합니다.
요즘 유행하는 LLM ChatBot을 api를 활용하여 만들수도 있고,
DB와 연결하여 원하는 정보를 조회하는 웹앱으로 만들 수도 있습니다.
사내 내부 사용룡, 외부 공유용 어느쪽으로든 사용하기 좋은 library입니다.
참고 자료
https://github.com/streamlit/streamlit
GitHub - streamlit/streamlit: Streamlit — A faster way to build and share data apps.
Streamlit — A faster way to build and share data apps. - streamlit/streamlit
github.com
https://docs.streamlit.io/get-started
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
https://wikidocs.net/book/14530
스트림릿(Streamlit) 30일 챌린지
Streamlit 앱을 배우고, 만들고, 배포하는 30일간의 소셜 챌린지 `#30DaysOfStreamlit` 한국어판 (제가 번역한 과 같은 내용입니다.) --- …
wikidocs.net
'Python 관련 정보 > Library 소개' 카테고리의 다른 글
poetry 사용법 - 파이썬 패키지 의존성 관리 (0) | 2023.07.20 |
---|---|
googletrans - 무료 구글 번역기 라이브러리 (0) | 2023.07.08 |