1 steamlit
- 데이터 과학 및 머신러닝 웹 애플리케이션 빠르게 구축하는 라이브러리. 웹 개발 할 줄 모른 채 간단한 python 스크립트로 대화형 웹 앱 만들 수 있다.
2 과정
- visual studio coide, anaconda 설치
- C/사용자에 Streamlit이라는 폴더 만들기
- visual studio에서 파이썬 앱 설치
- visual studio에서 Streamlit이라는 폴더랑 연결
- ctrl + shift + p 하고 python interpreter global
- view- terminal - command prompt
- base로 이동: conda activate base(혹시 여기서 문제가 생기면 anaconda가 환경 변수로 설정되어야 하는 것)
- 가상 환경 만들기: conda create -n STR python=3.9.13
- 가상 환경으로 이동: conda activate STR
- 가상 환경에 streamlit 설치: pip install streamlit으로 설치
- 필요 라이브러리 일괄 설치: streamlit 폴더에 requirement.txt에서 옮겨놓고 => pip install -r requirements.txt
- VS streamlit 폴더에 myApp.py라는 파일 만들어서 코드 짠다
- 실행: streamlit run myApp.py
- 가상환경 삭제: conda env remove -n STR
3 예시
3-1
import streamlit as st
st.title('Title *Markdown* 인식')
st.header('Title *Markdown* 헤더')
st.title('Title *Markdown* 인식')
st.header('Title *Markdown* 헤더')
st.subheader('Title *Markdown* 서브')
x = 10
y = 20
st.write('x=', x, 'y=', y)
import pandas as pd
df = pd.DataFrame({'col1': [1,2,3]})
df
st.write('데이터 프레임', df)
import matplotlib.pyplot as plt
import numpy as np
arr = np.random.normal(1, 1, size = 100)
fig, ax = plt.subplots()
ax.hist(arr, bins =20)
fig
code = '''def hello():
print('Hello, Streamlist')'''
st.code(code, language = 'python')
'This is :red[red]'
'This is :green[green]'
st.caption('This')
'여름엔 딱 좋아 :선글라스: '
':100: 점~'
':웃음:ㅎㅎ :+1: 최고!!'
':반짝임: 재밌다. :질문: :질문:'
option = st.selectbox('Please select in selectbox!', ('봄', '여름', '가을', '겨을'))
st.write('You selected:', option)
3-2 예시
import streamlit as st
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
_, col, _ = st.columns([2,6,2])
col.header('Streamlit 시각화')
''
dfIris = sns.load_dataset('iris')
colors = {'setosa':'red', 'virginica' : 'green', 'versicolor':'blue'}
st.sidebar.title('Iris Species :벚꽃:')
with st.sidebar:
selectX = st.selectbox("X 변수 선택:", ["sepal_length", "sepal_width", "petal_length", "petal_width"])
''
selectY = st.selectbox("Y 변수 선택:", ["sepal_length", "sepal_width", "petal_length", "petal_width"])
''
selectSpecies = st.multiselect(
'붓꽃 유형 선택 (:blue[다중]):', ['setosa', 'versicolor', 'virginica'])
selectAlpha = st.slider('alpha 설정:', 0.1, 1.0, 0.5)
if selectSpecies:
fig = plt.figure(figsize = (7,5))
for aSpecies in selectSpecies:
df = dfIris[dfIris.species == aSpecies]
plt.scatter(df[selectX], df[selectY],
color = colors[aSpecies], label=aSpecies)
plt.legend(loc='lower right')
plt.xlabel(selectX); plt.ylabel(selectY)
plt.title('Iris Scatter Plot')
st.pyplot(fig)
else:
st.warning('붓꽃의 유형을 선택해 주세요!!!')