행위

Python Streamlit 챠트

DB CAFE

thumb_up 추천메뉴 바로가기


1 설치[편집]

- Install command

$ pip install streamlit
$ streamlit hello

- 사용통계 출력시 아래 위치 옵션 변경

~/.streamlit/config.toml, creating that file if necessary:

[browser]
gatherUsageStats = false


2 실행 방법[편집]

streamlit run test.py


3 Streamlit 샘플[편집]

  • 샘플 소스
import streamlit as st
st.title('Hello Streamlit')
  • 실행
streamlit run app.py

4 Streamlit 샘플2[편집]

4.1 import 선언[편집]

import streamlit as st
import pandas as pd

4.2 타이틀 생성[편집]

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 가능.

4.3 스트림릿 실행[편집]

$ streamlit run first_app.py

5 문자열 관련[편집]

import streamlit as st

# 타이틀
st.title('this is title')
# 헤더 
st.header('this is header')
# 서브헤더
st.subheader('this is subheader')

6 레이아웃 만들기[편집]

  • 레이아웃으로 웹페이지 분할
  • 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: 안의 내용과 같은 기능


7 Data Visualization[편집]

7.1 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)

7.2 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)

7.3 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


7.4 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

7.4.1 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 등 여러 위젯 사용 가능.

7.5 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!

7.6 APP Deploy , Manage , Share[편집]

  • streamlit으로 개발한 app은 Streamlit Cloud로 deploy, manage, share 가능.
  • 현재 Streamlit Cloud는 초대를 받은 멤버에 한해서 사용이 가능합니다.
  • Request an invite에 몇 가지 사항을 제출하고 사용.

7.6.1 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