PL Template for NLP(5)

City_Duck·2023년 5월 2일
0

PL Template

목록 보기
6/6

Template을 GUI로 사용하기 위해 Streamlit을 사용하고자 합니다.

그렇기에 Streamlit에 대해 알아보고자 합니다.

Streamlit : 빠르고 간편하게 어필리케이션을 만들 수 있는 툴

python 3.7 ~ 3.11까지 사용가능하며, Mac OS 기준으로 다음과 같이 설치 가능합니다.

>>> python -m ensurepip --upgrade
>>> pip install streamlit

Get started

  • Main Concept
    streamlit run 해당 커맨드를 통해 streamlit을 시작할 수 있습니다.
    streamlit run code.py [-- script args]
    python -m streamlit run code.py
  • Development Flow
    "Always rerun"을 통해 코드를 변경하고 저장하면 streamlit은 이를 자동으로 반영할 수 있습니다.
  • Display and style data
    Streamlit에는 몇가지의 display 방식이 존재합니다. 그 중 magic 방법과 st.write() 방식이 존재합니다.
    • 먼저 magic의 경우 다음과 같이 코드를 작성하면 됩니다.
      import streamlit as st
      import pandas as pd
      df = pd.DataFrame({
      'first column': [1, 2, 3, 4],
      'second column': [10, 20, 30, 40]
      })
      df
    • 다음으로는 st.write()를 사용하는 방법입니다.
      import streamlit as st
      import pandas as pd
      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]
      }))

      하지만 모든 경우에 magic과 st.write()가 유용한 것은 아닙니다.
      해당 방식들은 display 형식을 자동으로 지정하기에 원하는 형태의 display가 나오지 않을 수 있습니다.
      이러한 경우엔 st.table, st.dataframe, Styler 등을 활용하여 원하는 방식으로 꾸밀 수 있습니다.
      import streamlit as st
      import pandas as pd
      import numpy as np
      dataframe = np.random.randn(10, 20)
      st.dataframe(dataframe)
      df = pd.DataFrame(
        np.random.randn(10, 20),
        columns=('col %d' % i for i in range(20)))
      st.dataframe(df.style.highlight_max(axis=0))

      이외에도 Widgets, Layout 등을 설정할 수 있습니다.
  • Caching
    Streamlit에서는 caching 기능을 지원합니다.
    1. @st.cache_data : DF, csv, API calls 등 반환을 해야하는 데이터에 권장됩니다.
    2. @st.cache_resource : ML models 혹은 database connections와 같이 여러번 로드할 필요가 없는 것에 사용합니다.
profile
AI 새싹

0개의 댓글