Python/Streamlit 9

Streamlit) ๋Œ€์‹œ๋ณด๋“œ์˜ ๊ทธ๋ž˜ํ”„์— ํ•œ๊ธ€ ๋‚˜์˜ค๊ฒŒ ํ•˜๋Š” ์ฝ”๋“œ

https://luvris2.tistory.com/119 AWS EC2 - Streamlit ๋Œ€์‹œ๋ณด๋“œ์— ํ•œ๊ธ€ ์ถœ๋ ฅํ•˜๊ธฐ (ํ•œ๊ธ€ ๊นจ์ง)์ด ๊ธ€์€ ๋ฆฌ๋ˆ…์Šค ํ•œ๊ธ€ ํฐํŠธ๊ฐ€ ์„ค์น˜๋˜์–ด์žˆ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์˜ ํฌ์ŠคํŒ…์œผ๋กœ ์ด๋™ํ•˜๋ฉด ํ•œ๊ธ€ ํฐํŠธ๋ฅผ ์„ค์น˜ํ•˜๋Š” ์ ˆ์ฐจ๊ฐ€ ๊ธฐ์žฌ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. https://luvris2.tistory.com/118 ๋ฆฌ๋ˆ…์Šค - ํ•œ๊ธ€ ํฐํŠธ ์„คluvris2.tistory.com์„ค์น˜๋Š” ์œ„ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ์กฐํ•œ๋‹ค # ๊ทธ๋ž˜ํ”„์— ํ•œ๊ธ€ ๋‚˜์˜ค๊ฒŒ ํ•˜๋Š” ์ฝ”๋“œimport platformimport matplotlib.pyplot as pltfrom matplotlib import font_manager, rcplt.rcParams['axes.unicode_minus'] = Falseif platform.system() =..

Python/Streamlit 2024.05.03

Streamlit) ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํŒŒ์ผ๋กœ ์›นํŽ˜์ด์ง€ ์‹คํ–‰ํ•˜๊ธฐ (๋‹ค๋ฅธ ํŒŒ์ผ์— ์žˆ๋Š” def ํ•จ์ˆ˜ ๊ฐ€์ ธ์˜ค๊ธฐ)

ํŒŒ์ผ์„ ์—ฌ๋Ÿฌ๊ฐœ๋กœ ๋งŒ๋“ค์–ด์„œ, app8.py ๋ฅผ ์‹คํ–‰์‹œํ‚ค๊ณ  ๊ฐ๊ฐ์˜ defํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.  app8.py์˜ ๋‚ด์šฉ์ด๋‹ค# ํŒŒ์ผ์„ ๋ถ„๋ฆฌํ•ด์„œ ๊ฐœ๋ฐœํ•˜๋Š” ๋ฐฉ๋ฒ•import streamlit as stfrom app8_home import run_home #๋‹ค๋ฅธ ํŒŒ์ผ์˜ defํ•จ์ˆ˜๋ฅผ ๊ฐ€์ ธ์™€์„œ ์“ฐ๋Š”๋ฐฉ๋ฒ•from app8_eda import run_edafrom app8_ml import run_mlfrom app8_about import run_aboutdef main(): st.title('ํŒŒ์ผ ๋ถ„๋ฆฌ ์•ฑ') menu = ['Home','EDA','ML','About'] choice = st.sidebar.selectbox('๋ฉ”๋‰ด', menu) if choice == men..

Python/Streamlit 2024.04.26

Streamlit) ์›นํŽ˜์ด์ง€์— ํŒŒ์ผ ์—…๋กœ๋“œ ์žฅ์น˜๋ฅผ ๋งŒ๋“ค๊ณ  ํŒŒ์ผ ์ €์žฅ์‹œํ‚ค๊ธฐ

# ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•˜๋Š” ๋ฐฉ๋ฒ•# ์ด๋ฏธ์ง€ ํŒŒ์ผ ์—…๋กœ๋“œ, csv ํŒŒ์ผ ์—…๋กœ๋“œimport streamlit as stfrom datetime import datetime # ํ˜„์žฌ์‹œ๊ฐ„์„ ๊ฐ€์ ธ์™€์„œ ์œ ๋‹ˆํฌํ•œ ํŒŒ์ผ๋ช…์„ ๋งŒ๋“œ๋Š”๋ฐ ์‚ฌ์šฉํ•  ๋ชฉ์ ์œผ๋กœ ์ž„ํฌํŠธimport pandas as pdfrom PIL import Image# ๋””๋ ‰ํ† ๋ฆฌ ์ •๋ณด์™€ ํŒŒ์ผ์„ ์•Œ๋ ค์ฃผ๋ฉด# ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ์— ํŒŒ์ผ์„ ์ €์žฅํ•˜๋Š” ํ•จ์ˆ˜def save_uploaded_file(directory, file): # 1. ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ์—†์œผ๋ฉด ๋””๋ ‰ํ† ๋ฆฌ๋ถ€ํ„ฐ ๋งŒ๋“ ๋‹ค. import os if not os.path.exists(directory): os.makedirs(directory) # 2. ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์กด์žฌํ•˜๋ฉด, ..

Python/Streamlit 2024.04.26

Streamlit) ์›นํŽ˜์ด์ง€์—์„œ ์œ ์ €์—๊ฒŒ ์ˆซ์ž, ๋ฌธ์ž, ์‹œ๊ฐ„, ์ƒ‰ ์ž…๋ ฅ ๋ฐ›๊ธฐ

#์œ ์ €ํ•œํ…Œ ์ˆซ์ž, ๋ฌธ์ž, ์‹œ๊ฐ„, ์ƒ‰์„ ์ž…๋ ฅ๋ฐ›๋Š” ๋ฐฉ๋ฒ•import streamlit as stdef main() : #1. ์ด๋ฆ„ ์ž…๋ ฅ๋ฐ›๊ธฐ name = st.text_input('์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”!') if name : st.text( name + "๋‹˜ ์•ˆ๋…•ํ•˜์„ธ์š”") else : st.text('') #2. ์ž…๋ ฅ ๊ธ€์ž ๊ฐฏ์ˆ˜ ์ œํ•œ address = st.text_input('์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”', max_chars=10) st.text(address) #3. ์—ฌ๋Ÿฌ ํ–‰์„ ์ž…๋ ฅ๊ฐ€๋Šฅํ† ๋ก message = st.text_area('๋ฉ”์„ธ์ง€๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”', height=3) st.text(message) #4. ..

Python/Streamlit 2024.04.26

Streamlit) ์ด๋ฏธ์ง€/๋™์˜์ƒ/์Œ์•…ํŒŒ์ผ์„ ์›น ํŽ˜์ด์ง€ ํ™”๋ฉด์— ๋„์šฐ๋Š” ๋ฐฉ๋ฒ•

#์ด๋ฏธ์ง€/๋™์˜์ƒ/์Œ์•…ํŒŒ์ผ์„ ํ™”๋ฉด์— ๋ณด์—ฌ์ฃผ๋Š” ๋ฐฉ๋ฒ•import streamlit as st#์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿดfrom PIL import Image #ํŒŒ์ด์ฌ ์ด๋ฏธ์ง€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ PILdef main(): # 1. ์ €์žฅ๋˜์–ด์žˆ๋Š” ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ํ™”๋ฉด์— ํ‘œ์‹œํ•˜๋Š” ๋ฐฉ๋ฒ• img = Image.open('./data/image_03.jpg') st.image(img) st.image(img, width=500) st.image(img, use_column_width=True) #width=Trueํ•ด์ฃผ๋ฉด ์ž๋™์œผ๋กœ ํญ์„ ๋งž์ถฐ์ค€๋‹ค #2. ์ธํ„ฐ๋„ท์ƒ์— ์žˆ๋Š” ์ด๋ฏธ์ง€๋ฅผ ํ™”๋ฉด์— ํ‘œ์‹œํ•˜๋Š” ๋ฐฉ๋ฒ• ์ธํ„ฐ๋„ท์ƒ์˜ ์ด๋ฏธ์ง€:URL์ด ์žˆ๋‹ค #์‚ฌ์ง„ ํŒŒ์ผ (์›€์ง์ด๋Š” gif์‚ฌ์ง„๋„ ๊ฐ€๋Šฅํ•˜๋‹ค) ..

Python/Streamlit 2024.04.26

Streamlit) UIํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฒ„ํŠผ ๋งŒ๋“ค๊ธฐ

# UI ํ•จ์ˆ˜๋“ค (๋ฒ„ํŠผ ๋“ฑ๋“ฑ)import streamlit as stimport pandas as pddef main() : df = pd.read_csv('./data/iris.csv') #๋ฒ„ํŠผ ๋งŒ๋“ค๊ธฐ #์œ ์ €๊ฐ€ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์„ ๋ณด์—ฌ์ค€๋‹ค if st.button(label='๋ฐ์ดํ„ฐ๋ณด๊ธฐ') : st.dataframe(df) #'๋Œ€๋ฌธ์ž' ๋ฒ„ํŠผ์„ ๋งŒ๋“ค๊ณ , ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด species ์ปฌ๋Ÿผ์˜ ๊ฐ’๋“ค์„ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•œ ๋ฐ์ดํ„ฐ ํ”„๋ ˆ์ž„์„ ํ™”๋ฉด์— ๋ณด์—ฌ์ฃผ์„ธ์š” if st.button(label='๋Œ€๋ฌธ์ž'): df['species'] = df['species'].str.upper() st.dataframe(df)..

Python/Streamlit 2024.04.26

Streamlit) ์›น ํŽ˜์ด์ง€์— ๋‹ค์–‘ํ•œ ํ…์ŠคํŠธ ํ‘œ์‹œํ•˜๊ธฐ

import streamlit as stdef main() : #ํ…์ŠคํŠธ๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋ฐฉ๋ฒ• st.title('์›น ๋Œ€์‹œ๋ณด๋“œ') st.text('์›น ๋Œ€์‹œ๋ณด๋“œ ๊ฐœ๋ฐœํ•˜๊ธฐ') name = 'ํ™๊ธธ๋™' #ํ”„๋ฆฐํŠธ ํ•จ์ˆ˜๋Š” ๋””๋ฒ„๊น…์šฉ์ž…๋‹ˆ๋‹ค!!! print(f'์ œ ์ด๋ฆ„์€ {name}์ž…๋‹ˆ๋‹ค' ) st.text(f'์ œ ์ด๋ฆ„์€ {name}์ž…๋‹ˆ๋‹ค') st.header('์ด ์˜์—ญ์€ ํ—ค๋”') st.subheader('์„œ๋ธŒ ํ—ค๋”') st.success('์ž‘์—…์ด ์„ฑ๊ณตํ–ˆ์„ ๋•Œ ์‚ฌ์šฉํ•˜์ž.') st.warning('๊ฒฝ๊ณ  ๋ฌธ๊ตฌ๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜์ž') st.info('์ •๋ณด๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜์ž') st.error('๋ฌธ์ œ๊ฐ€ ์žˆ..

Python/Streamlit 2024.04.26

Streamlit) ์ž„ํฌํŠธ(import)ํ•˜์—ฌ ์›นํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ

๋งŒ์•ฝ ๊ฐ€์ƒํ™˜๊ฒฝ์„ ๋งŒ๋“ค์—ˆ๋‹ค๋ฉด, VScode์—์„œ ์˜ค๋ฅธ์ชฝ ํ•˜๋‹จ์— Python์ด๋ผ๊ณ  ์ ํ˜€์žˆ๋Š” ๊ณณ ์˜ค๋ฅธ์ชฝ์— ์œ„์น˜ํ•œ ์ˆซ์ž๋ฅผ ๋ˆŒ๋Ÿฌ์ฃผ๊ณ ,    ์ž์‹ ์ด ๋งŒ๋“  ๊ฐ€์ƒํ™˜๊ฒฝ ์ด๋ฆ„์ด ์จ์ ธ์žˆ๋Š” ๊ณณ์„ ์„ ํƒํ•˜์—ฌ ์ง„ํ–‰ํ•˜๋ฉด ๋œ๋‹ค.      ์™ผ์ชฝ ์œ„์—์„œ ํŒŒ์ผ์„ ์„ ํƒํ•˜์—ฌ ์ƒˆ ํŒŒ์ผ์„ ๋งŒ๋“ค๊ฑฐ๋‚˜, ํŒŒ์ผ๋ชจ์–‘์„ ์„ ํƒํ•˜์—ฌ ์ƒˆ๋กœ์šด ํŒŒ์ผ์„ ํ•˜๋‚˜ ๋งŒ๋“ ๋‹ค.    ํŒŒ์ผ ์ œ๋ชฉ์€ app.py ๋ผ๊ณ  ํ•˜์˜€๋‹ค.     ์ƒˆ ํŒŒ์ผ์— ์ ์„ ์ฝ”๋“œ์ด๋‹ค. ์ด๋Š” ์›น ๋Œ€์‹œ๋ณด๋“œ๋ฅผ ์—ฌ๋Š” ์ฝ”๋“œ์ด๋‹ค.# ์ŠคํŠธ๋ฆผ๋ฆฟ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์ž„ํฌํŠธimport streamlit as st# ์›น ๋Œ€์‹œ๋ณด๋“œ ๊ฐœ๋ฐœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ ์ŠคํŠธ๋ฆผ๋ฆฟ์€ mainํ•จ์ˆ˜๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹คdef main() : passif __name__ == '__main__' : main()   Terminal  > New Ter..

Python/Streamlit 2024.04.26

Streamlit) ๊ฐ€์ƒํ™˜๊ฒฝ ๋งŒ๋“ค๊ณ , ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋‹ค์šด ๋ฐ›๊ณ , ์‹คํ–‰์‹œํ‚ค๊ธฐ

๋จผ์ €, ์•„๋‚˜์ฝ˜๋‹ค๋ฅผ ๋‹ค์šด๋ฐ›๋Š”๋‹ค.  ๊ทธ๋Ÿฌ๋ฉด ์œˆ๋„์šฐ์— ์•„๋‚˜์ฝ˜๋‹ค ํ”„๋กฌํ”„ํŠธ๋„ ๋‹ค์šด๋ฐ›์•„์ ธ ์žˆ์„๊ฒƒ์ด๋‹ค. ์•„๋‚˜์ฝ˜๋‹ค ํ”„๋กฌํ”„ํŠธ๋กœ ๋“ค์–ด๊ฐ€์„œ  conda create -n (๊ฐ€์ƒํ™˜๊ฒฝ์ด๋ฆ„ ๋งˆ์Œ๋Œ€๋กœ ์ง€์ •ํ•˜์—ฌ ์—ฌ๊ธฐ์— ๋„ฃ๋Š”๋‹ค!) python=3.10 openssl numpy scipy matplotlib ipython scikit-learn pandas pillow jupyter seaborn  ๊ฐ€์ƒํ™˜๊ฒฝ์ด๋ฆ„์„ ์ง‘์–ด๋„ฃ์–ด์„œ ๊ฐ€์ƒํ™˜๊ฒฝ์„ ๋งŒ๋“ค๊ณ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋‹ค์šด๋ฐ›๋Š”๋‹ค.๋‹ค ๋‹ค์šด๋ฐ›์œผ๋ฉด  conda activate ๊ฐ€์ƒํ™˜๊ฒฝ ์ด๋ฆ„ ์œผ๋กœ ๊ฐ€์ƒํ™˜๊ฒฝ ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•˜๋‹ค     ์ฃผํ”ผํ„ฐ๋…ธํŠธ๋ถ์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด, ์•„๋‚˜์ฝ˜๋‹ค ํ”„๋กฌํ”„ํŠธ์— ์•„๋ž˜๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ ๋ถ™์—ฌ๋„ฃ์œผ๋ฉด ๋œ๋‹ค.  jupyter notebook .    (st_310), (str_310)์€ ๋‚ด๊ฐ€ ์ง€์ •ํ•œ ๊ฐ€์ƒ..

Python/Streamlit 2024.04.26