On the journey of
[AWS 사전공인교육] Streamlit 활용하기 (1) 본문
Streamlit은 Google Colab, Jupyter notebook 등 파이썬 언어를 지원하는 곳 어디에서나 활용이 가능한 시각화 툴이다. 분석 툴이 아니기 때문에, 프론트 지식이 어느 정도는 뒷받침되어야 한다고 생각함 (디자인 센스라도 ,,)
설치부터 실행까지 기본적인 코드는 아래와 같다. 만약 아래 기본 코드가 먹히지 않는다면 !pip install (magic command) 나 pip3 로 변경하면 된다.
#1. Streamlit 모듈 설치하기
pip install streamlit
#2. 앱 폴더 및 파일 만들기
mkdir apps
cd apps
touch app.py
#3. Streamlit 모듈 import 하고 실행하기
import streamlit as st
#4. Streamlit 실행하기(터미널에 입력)
streamlit run app.py
앞서 기본적인 프론트 지식이 조금 필요할 수 있다고 했는데, 그 첫 번째 이유는 Title, Header, Subheader에서 시작한다. 말하자면 큰 제목, 중간 제목, 작은 제목이라고 할 수 있고 작은 제목에 깨알같은 내용들(Text)이 출력되는 것.
import streamlit as st
## Title
st.title("Streamlit Tutorial")
## Header/Subheader
st.header("This is header")
st.subheader("This is subheader")
## Text
st.text("Hello Streamlit!")
Hello World 대신 Hello Streamlit 으로 변경해보았다 :)
다음은 Markdown으로, Streamlit 역시 Dash 와 마찬가지로 Markdown을 지원한다.
import streamlit as st
## Markdown syntax
st.markdown("# This is a Markdown title")
st.markdown("## This is a Markdown header")
st.markdown("### This is a Markdown subheader")
st.markdown("- item 1\n"
" - item 1.1\n"
" - item 1.2\n"
"- item 2\n"
"- item 3")
st.markdown("1. item 1\n"
" 1. item 1.1\n"
" 2. item 1.2\n"
"2. item 2\n"
"3. item 3"
5.3 Latex : 백슬래시(\)을 자주 사용하는, 논문이나 노션 등의 작성 툴에서 수식작성을 위해 쓰이는 그 라텍스이다.
import streamlit as st
## Latex
st.latex(r"Y = \alpha + \beta X_i")
## Latex-inline
st.markdown(r"회귀분석에서 잔차식은 다음과 같습니다 $e_i = y_i —
\hat{y}_i$")
6. 메시지
: html, css 등 웹 관련 지식은 여기서도 필요하다. 알림창을 디자인하는 것도 프론트의 몫인데, 여기서는 포맷된 형태로 (알림)메시지와 에러메시지, 예외처리 메시지 기능을 제공한다.
import streamlit as st
## Error/message text
st.success("Successful")
st.info("Information!")
st.warning("This is a warning")
st.error("This is an error!")
st.exception("NameError('Error name is not defined')"
7. 시각화 툴이라고 했으니까, 데이터를 분석한 결과를 시각화해보자. 클래식 데이터 중 하나인 iris data를 github 상에서 불러왔다.
## Load data
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
# GitHub 에서 아이리스 데이터 다운로드
url = "https://raw.githubusercontent.com/uiuc-cse/data-fa14/ghpages/data/iris.csv"
iris_df = pd.read_csv(url)
이런 결과물로 데이터프레임과 테이블, 데이터를 출력하는 방법은 크게 3가지이다.
1) st.table : 입력한 테이블 전체 return
2) st.dataframe : 10개 행을 기준으로 스크롤 가능, 그 후의 열마다 정렬하는 것도 가능!
3) st.write : st.dataframe과 같은 결과를 return
## Load data
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
# GitHub 에서 아이리스 데이터 다운로드
url = "https://raw.githubusercontent.com/uiuc-cse/data-fa14/ghpages/data/iris.csv"
iris_df = pd.read_csv(url)
## Return table/dataframe
# table
st.table(iris_df.head())
# dataframe
st.dataframe(iris_df)
st.write(iris_df)
st.markdown("* * *")
마크다운까지 깔끔하게 가능하다 :)
본격적인 라디오박스(select 가능한 그런)와 input은 다음 포스트에 작성해 보겠다.
'Experiences & Study > AWS' 카테고리의 다른 글
[AWS 사전공인교육] 4. AWS의 서비스 종류 (0) | 2023.10.11 |
---|---|
[AWS 사전공인교육] Streamlit 활용하기 (2) (1) | 2023.10.07 |
[AWS 사전공인교육] 10-1.Crawling 기본코드 정리 (0) | 2023.09.28 |
[AWS 사전공인교육] 3. Lambda ~ 3.1 Hello World API (0) | 2023.09.19 |
[AWS 사전공인교육] 2. AWS Cloud9 - Hello World API 및 실행 (0) | 2023.09.16 |