"Python streamlit dashboard"의 두 판 사이의 차이
DB CAFE
1번째 줄: | 1번째 줄: | ||
= 파이썬 Streamlit 대시보드 개발 = | = 파이썬 Streamlit 대시보드 개발 = | ||
== 설치 == | == 설치 == | ||
− | https:// | + | - Install command |
+ | <source lang=python> | ||
+ | $ pip install streamlit | ||
+ | $ streamlit hello | ||
+ | </source> | ||
+ | |||
+ | - 사용통계 출력시 아래 위치 옵션 변경 | ||
+ | <source lang=python> | ||
+ | ~/.streamlit/config.toml, creating that file if necessary: | ||
+ | |||
+ | [browser] | ||
+ | gatherUsageStats = false | ||
+ | </source> | ||
+ | |||
+ | |||
+ | * github repository: https://github.com/streamlit/streamlit | ||
+ | * document: https://docs.streamlit.io/ | ||
+ | * tutorial: https://docs.streamlit.io/en/stable/getting_started.html | ||
+ | |||
+ | | ||
== 실행 방법 == | == 실행 방법 == | ||
7번째 줄: | 26번째 줄: | ||
streamlit run test.py | streamlit run test.py | ||
</source> | </source> | ||
+ | |||
+ | * Local URL: http://localhost:8501 | ||
+ | * Network URL: http://192.168.35.186:8501 | ||
+ | |||
== Streamlit 샘플 == | == Streamlit 샘플 == | ||
18번째 줄: | 41번째 줄: | ||
<source lang=python> | <source lang=python> | ||
streamlit run app.py | streamlit run app.py | ||
+ | </source> | ||
+ | |||
+ | == Streamlit 샘플2 == | ||
+ | === first_app.py 생성 === | ||
+ | import streamlit as st | ||
+ | import pandas as pd | ||
+ | |||
+ | === 타이틀 생성 === | ||
+ | <source lang=python> | ||
+ | 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] | ||
+ | })) | ||
+ | |||
+ | </source> | ||
+ | * st.write()는 str 타입뿐만 아니라 pandas의 DataFrame 도 write 가능. | ||
+ | | ||
+ | === 스트림릿 실행 === | ||
+ | <source lang=python> | ||
+ | $ streamlit run first_app.py | ||
</source> | </source> | ||
33번째 줄: | 80번째 줄: | ||
== 레이아웃 만들기 == | == 레이아웃 만들기 == | ||
− | |||
* 레이아웃으로 웹페이지 분할 | * 레이아웃으로 웹페이지 분할 | ||
* columns 함수 | * columns 함수 | ||
59번째 줄: | 105번째 줄: | ||
− | + | == Data Visualization == | |
− | + | === line chart 그리기 === | |
− | + | * st.line_chart() 메서드로 line chart 추가 | |
− | + | <source lang=python> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | | ||
− | |||
− | |||
− | |||
− | |||
chart_data = pd.DataFrame( | chart_data = pd.DataFrame( | ||
np.random.randn(20, 3), | np.random.randn(20, 3), | ||
135번째 줄: | 114번째 줄: | ||
st.line_chart(chart_data) | st.line_chart(chart_data) | ||
− | + | </source> | |
− | + | ||
− | | + | === map 그리기 === |
− | + | * st.map() 메서드 사용. | |
+ | <source lang=python> | ||
map_data = pd.DataFrame( | map_data = pd.DataFrame( | ||
np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4], | np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4], | ||
144번째 줄: | 124번째 줄: | ||
st.map(map_data) | st.map(map_data) | ||
− | + | </source> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | === Checkbox show/hide data === | ||
+ | * st.checkbox() 이용, 체크박스를 이용해서 데이터 show/hide 설정. | ||
+ | * st.checkbox()는 위젯명을 argument로 받아서 처리. | ||
+ | <source lang=python> | ||
if st.checkbox('Show dataframe'): | if st.checkbox('Show dataframe'): | ||
chart_data = pd.DataFrame( | chart_data = pd.DataFrame( | ||
160번째 줄: | 136번째 줄: | ||
chart_data | chart_data | ||
+ | |||
+ | </source> | ||
+ | |||
| | ||
− | + | === Selectbox === | |
− | | + | * st.selectbox()는 pandas.Series를 입력으로 받아서 옵션 선택. |
− | st.selectbox()는 pandas.Series를 입력으로 받아서 | + | |
+ | <source lang=python> | ||
import streamlit as st | import streamlit as st | ||
import pandas as pd | import pandas as pd | ||
174번째 줄: | 154번째 줄: | ||
'You selected: ', option | 'You selected: ', option | ||
− | + | </source> | |
− | + | | |
− | | + | ==== Selectbox를 사이드로 이동 ==== |
− | + | <source lang=python> | |
− | |||
− | |||
− | |||
− | |||
import streamlit as st | import streamlit as st | ||
import pandas as pd | import pandas as pd | ||
191번째 줄: | 167번째 줄: | ||
'You selected: ', option | 'You selected: ', option | ||
+ | |||
+ | </source> | ||
− | + | * streamlit에서 제공하는 대부분의 element는 st.sidebar. [element_name]() 포맷으로 사용 가능. | |
+ | * 위의 위젯 외에도 button, expander 등 여러 위젯 사용 가능. | ||
| | ||
− | + | === progress 진행현황표시 === | |
− | | + | * st.progress()이용, 웹이지에 진행현황을 표시. |
− | + | <source lang=python> | |
import time | import time | ||
212번째 줄: | 191번째 줄: | ||
'...and now we\'re done! | '...and now we\'re done! | ||
+ | </source> | ||
+ | === APP Deploy , Manage , Share === | ||
+ | * streamlit으로 개발한 app은 Streamlit Cloud로 deploy, manage, share 가능. | ||
+ | * 현재 Streamlit Cloud는 초대를 받은 멤버에 한해서 사용이 가능합니다. | ||
+ | * Request an invite에 몇 가지 사항을 제출하고 사용. | ||
| | ||
− | + | ==== 3스텝으로 구현 가능 ==== | |
− | + | 1. Put your app in a public Github repo (and make sure it has a requirements.txt!) | |
− | |||
− | |||
− | |||
− | |||
− | |||
2. Sign into share.streamlit.io | 2. Sign into share.streamlit.io | ||
3. Click ‘Deploy an app’ and then paste in your GitHub URL | 3. Click ‘Deploy an app’ and then paste in your GitHub URL | ||
230번째 줄: | 209번째 줄: | ||
https://analyticsindiamag.com/ai-mysteries/build-and-deploy-your-first-real-time-dashboard-with-streamlit/ | https://analyticsindiamag.com/ai-mysteries/build-and-deploy-your-first-real-time-dashboard-with-streamlit/ | ||
− | |||
https://www.geeksforgeeks.org/create-interactive-dashboard-in-python-using-streamlit/ | https://www.geeksforgeeks.org/create-interactive-dashboard-in-python-using-streamlit/ |
2024년 8월 21일 (수) 12:44 판
thumb_up 추천메뉴 바로가기
- DBA { Oracle DBA 명령어 > DBA 초급 과정 > DBA 고급 과정 }
- 튜닝 { 오라클 튜닝 목록 }
- 모델링 { 데이터 모델링 가이드 }
목차
1 파이썬 Streamlit 대시보드 개발[편집]
1.1 설치[편집]
- Install command
$ pip install streamlit
$ streamlit hello
- 사용통계 출력시 아래 위치 옵션 변경
~/.streamlit/config.toml, creating that file if necessary:
[browser]
gatherUsageStats = false
- github repository: https://github.com/streamlit/streamlit
- document: https://docs.streamlit.io/
- tutorial: https://docs.streamlit.io/en/stable/getting_started.html
1.2 실행 방법[편집]
streamlit run test.py
- Local URL: http://localhost:8501
- Network URL: http://192.168.35.186:8501
1.3 Streamlit 샘플[편집]
- 샘플 소스
import streamlit as st
st.title('Hello Streamlit')
- 실행
streamlit run app.py
1.4 Streamlit 샘플2[편집]
=== first_app.py 생성 === import streamlit as st import pandas as pd
1.4.1 타이틀 생성[편집]
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 가능.
1.4.2 스트림릿 실행[편집]
$ streamlit run first_app.py
1.5 문자열 관련[편집]
import streamlit as st
# 타이틀
st.title('this is title')
# 헤더
st.header('this is header')
# 서브헤더
st.subheader('this is subheader')
1.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: 안의 내용과 같은 기능
1.7 Data Visualization[편집]
1.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)
1.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)
1.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
1.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
1.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 등 여러 위젯 사용 가능.
1.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!
[편집]
- streamlit으로 개발한 app은 Streamlit Cloud로 deploy, manage, share 가능.
- 현재 Streamlit Cloud는 초대를 받은 멤버에 한해서 사용이 가능합니다.
- Request an invite에 몇 가지 사항을 제출하고 사용.
1.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