[MLOps] 프로토타입 만들기 (2)

hyunsooo·2022년 11월 10일
0

streamlit-gallery
streamlit-doc

Streamlit 사용하기

voila는 노트북에서 프로토타입을 쉽게 만들 수 있었고 Streamlit은 조금 더 웹 서비스에 가까운 방법으로 프로토타입을 만드는 방법으로 현업에서는 다른 조직(웹서비스를 구현하기 위한)의 도움없이 간단하고 빠르게 프로토타입을 만드는 방법으로 사용한다.

장점

  • python 코드를 조금만 수정하면 웹을 사용할 수 있다.
  • 백엔드 개발이나 HTTP 요청을 구현하지 않아도 된다.
  • 다양한 Component를 제공해 대시보드 UI 구성할 수 있다.
  • Stramlit Cloud도 존재하여 쉽게 배포가 가능하다.(단, Community Plan은 Public Repo만 가능하다. private는 유료버전을 이용)
  • 화면 녹화 기능이 존재한다.
  • 설치

pip3 install streamlit

  • 실행

>>> streamlit run <실행파일>

Text 작성

  • st.title('text') : 제목

  • st.header('text'): 헤더

  • st.subheader('text'): 서브헤더

  • st.write('text'): 텍스트

Button

  • st.button('button text') : 버튼 생성

  • if st.button(): 버튼이 클릭됬을때 기능을 구현할 수 있다.

  • st.checkbox('checkbox text'): 체크박스 생성

  • st.checkbox(): 체크박스가 표시되었을때 기능을 구현할 수 있다.

  • st.radio("title", ("a", "b", "c")) : 라디오버튼 생성

  • st.selectbox("title", ("a", "b", "c")): 셀럭트 박스 생성

  • st.multiselect("title", ["a", "b", "c"]): 멀티셀렉트 박스 생성

  • st.text_input("text"): 텍스트 박스 생성

  • st.text_input("text", type="password"): 암호박스 생성

  • st.number_input("text"): 숫자 박스 생성

  • st.data_input("text"): 날짜 박스 생성

  • st.time_input("text"): 시간 박스 생성

  • st.caption("text"): 캡션 생성

  • st.code("code"): 코드박스 생성

  • st.latex("latex"): latex 생성

Slider

  • st.slider("title", min, max, default): 슬라이더 설정

Pandas Dataframe, Markdown

  • st.write : 보여줄 수 있는 것이면 어떤 것이든 보여줄 수 있다.

    • st.writed(df)로 dataframe을 보여준다.
  • st.dataframe(df): interative한 dataframe으로 컬럼클릭 및 정렬 가능

  • st.table(df): static한 dataframe

  • st.markdown('text'): markdown으로 작성할 수 있다.

Metric, JSON

  • st.metric('name', 현재값, 전대비 증감량)

  • st.json(df.to_json()) : json구조로 표현

Line Chart

  • st.line_chart(chart_dataframe)

Map Chart

  • st.map(map_dataframe)

그외 charts

st.sidebar.button()를 사용하면 사이드바에 표시할 수 있다. 기존의 method들 앞에 sidebar를 붙이면 사용할 수 있는 것!
사이드바에는 보통 파라미터를 지정하거나 암호같은것을 설정할 수 있다.

Layout

  • col1, col2, col3 = st.columns(3) : 여러 칸으로 나눠서 추가하고 싶은 경우

    • col1.write()와 같은 방식으로 나눠서 적용할 수 있다.
  • st.expander("클릭하면 열린다."): 눌렀을 경우 확장하는 부분이 필요한 경우

  • st.spinner("please wait.."): 연산이 진행되는 도중 메세지를 보여주고 싶은 경우

  • st.balloons(): 웹에 재밌는 효과 부여 풍선이 날라다닌다~

  • st.success("message")

  • st.info("message")

  • st.warning("message")

  • st.error("message")

file uploader

  • st.file_uploader(): 200메가 제한 업로더

그외 component들


Streamlit의 Data Flow

streamlit의 화면에서 무언가 업데이트 되면 전체 streamlit 코드가 다시 실행된다.

  1. code가 수정되는 경우
  2. 사용자가 streamlit의 위젯과 상호작용하는 경우(버튼, 입력상자 등)

session state를 사용해서 global variable처럼 사용할 수 있다.

if 'cnt' not in st.session_state:
	st.session_state.count = 0
    
increment = st.button("Increment")
if increment:
	st.session_state.count += 1
    
decrement = st.button("Decrement")
if decrement:
	st.session_state.count -= 1
    
st.write('Count = ', st.session_state.count)

@st.cache

매번 다시 실행하는 특성 때문에 데이터도 다시 읽을 수 있다.
이런 경우 @st.cache데코레이터를 통해 캐싱할 수 있다.
데이터를 읽는 함수를 만들고 데코레이터를 적용할 수 있다.

profile
CS | ML | DL

0개의 댓글