다른 명령
설치
- Install command
$ pip install streamlit $ streamlit hello
- 사용통계 출력시 아래 위치 옵션 변경
~/.streamlit/config.toml, creating that file if necessary: [browser] gatherUsageStats = false
- github repository: https://github.com/streamlit/streamlit
- document: https://docs.streamlit.io/
- tutorial: https://docs.streamlit.io/en/stable/getting_started.html
실행 방법
streamlit run test.py
- Local URL: http://localhost:8501
- Network URL: http://192.168.35.186:8501
Streamlit 샘플
- 샘플 소스
import streamlit as st st.title('Hello Streamlit')
- 실행
streamlit run app.py
Streamlit 샘플2
import 선언
import streamlit as st import pandas as pd
타이틀 생성
st.title('My first app') st.write("Here's our first attempt at using data to create a table:") st.write(pd.DataFrame({ 'first column': [1, 2, 3, 4], 'second column': [10, 20, 30, 40] }))
- st.write()는 str 타입뿐만 아니라 pandas의 DataFrame 도 write 가능.
스트림릿 실행
$ streamlit run first_app.py
문자열 관련
import streamlit as st # 타이틀 st.title('this is title') # 헤더 st.header('this is header') # 서브헤더 st.subheader('this is subheader')
레이아웃 만들기
- 레이아웃으로 웹페이지 분할
- columns 함수
import streamlit as st col1,col2 = st.columns([2,3]) # 공간을 2:3 으로 분할하여 col1과 col2 컬럼 생성. with col1 : # column 1 에 담을 내용 st.title('here is column1') with col2 : # column 2 에 담을 내용 st.title('here is column2') st.checkbox('this is checkbox1 in col2 ') # with 구문 말고 다르게 사용 가능 col1.subheader(' i am column1 subheader !! ') col2.checkbox('this is checkbox2 in col2 ') # => 위에 with col2: 안의 내용과 같은 기능
Data Visualization
line chart 그리기
* st.line_chart() 메서드로 line chart 추가
chart_data = pd.DataFrame( np.random.randn(20, 3), columns=['a', 'b', 'c']) st.line_chart(chart_data)
map 그리기
* st.map() 메서드 사용.
map_data = pd.DataFrame( np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4], columns=['lat', 'lon']) st.map(map_data)
Checkbox show/hide data
* st.checkbox() 이용, 체크박스를 이용해서 데이터 show/hide 설정.
- st.checkbox()는 위젯명을 argument로 받아서 처리.
if st.checkbox('Show dataframe'): chart_data = pd.DataFrame( np.random.randn(20, 3), columns=['a', 'b', 'c']) chart_data
Selectbox
* st.selectbox()는 pandas.Series를 입력으로 받아서 옵션 선택.
import streamlit as st import pandas as pd st.title('TUNiBerse') option = st.selectbox( '당신의 직책을 선택해주세요.', pd.Series(['CEO', 'AI Engineer', 'Intern', 'Product Manager'])) 'You selected: ', option
Selectbox를 사이드로 이동
import streamlit as st import pandas as pd st.title('TUNiBerse') option = st.sidebar.selectbox( '당신의 직책을 선택해주세요.', pd.Series(['CEO', 'AI Engineer', 'Intern', 'Product Manager'])) 'You selected: ', option
- streamlit에서 제공하는 대부분의 element는 st.sidebar. [element_name]() 포맷으로 사용 가능.
- 위의 위젯 외에도 button, expander 등 여러 위젯 사용 가능.
progress 진행현황표시
* st.progress()이용, 웹이지에 진행현황을 표시.
import time 'Starting a long computation...' # Add a placeholder latest_iteration = st.empty() bar = st.progress(0) for i in range(100): # Update the progress bar with each iteration. latest_iteration.text(f'Iteration {i+1}') bar.progress(i + 1) time.sleep(0.1) '...and now we\'re done!
- streamlit으로 개발한 app은 Streamlit Cloud로 deploy, manage, share 가능.
- 현재 Streamlit Cloud는 초대를 받은 멤버에 한해서 사용이 가능합니다.
- Request an invite에 몇 가지 사항을 제출하고 사용.
3스텝으로 구현 가능
1. Put your app in a public Github repo (and make sure it has a requirements.txt!) 2. Sign into share.streamlit.io 3. Click ‘Deploy an app’ and then paste in your GitHub URL