s t u d y . . ๐Ÿง/์ด๊ฒƒ์ €๊ฒƒ

[streamlit] ์ŠคํŠธ๋ฆผ๋ฆฟ ์‚ฌ์šฉ๋ฒ• (์ž…๋ ฅ ๋ฐ ์ถœ๋ ฅ, ํŒŒ์ผ ์—…๋กœ๋“œ ๋ฐ ์ถœ๋ ฅ)

H J 2023. 8. 14. 23:47
 

[streamlit] python์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์›น์•ฑ ๋งŒ๋“ค๊ธฐ

streamlit์ด๋ž€ ! ํŒŒ์ด์ฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์›น์•ฑ์„ ์‰ฝ๊ฒŒ ์ œ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค Streamlit Docs Join the community Streamlit is more than just a way to make data apps, it's also a community of creators that share their apps and ideas

hjkim5004.tistory.com

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜๋ถ€ํ„ฐ ๊ฐ„๋‹จํ•œ ์›น์•ฑ ๋„์šฐ๊ธฐ๋Š” ์œ„ ๋งํฌ์— !


์›น์•ฑ์— ๋ฌธ์žฅ ์ถœ๋ ฅ

title

st.title('hello’)

header

st.header('hello’)

subheader

st.subheader('hello’)

text

st.text('hello’)

 

์œ„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•œ ๊ฒฐ๊ณผ


์‚ฌ์ด๋“œ๋ฐ”

menu = ['์ฒซ๋ฒˆ์งธ', '๋‘๋ฒˆ์งธ', '์„ธ๋ฒˆ์งธ']
choice = st.sidebar.selectbox('๋ฉ”๋‰ด', menu)


์ž…๋ ฅ

๋ฌธ์ž ์ž…๋ ฅ

name = st.text_input('๋ฌธ์ž ์ž…๋ ฅ')
st.text(f"๋ฌธ์ž : {name}")

 

๋ฌธ์žฅ ์ž…๋ ฅ

message = st.text_area('๋ฌธ์žฅ ์ž…๋ ฅ')
st.text(f"๋ฌธ์žฅ : {message}")

 

์ˆซ์ž ์ž…๋ ฅ

number = st.number_input('์ˆซ์ž ์ž…๋ ฅ')
st.text(number)

 

๋‚ ์งœ ์ž…๋ ฅ

my_date = st.date_input("๋‚ ์งœ ์ž…๋ ฅ")
st.text(my_date)

 

์‹œ๊ฐ„ ์„ ํƒ

my_time = st.time_input('์‹œ๊ฐ„ ์„ ํƒ')
st.text(my_time)

 

์ƒ‰ ์„ ํƒ

color = st.color_picker('์ƒ‰ ์„ ํƒ')
st.text(color)

 

๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ

password = st.text_input('๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ', type='password')
st.text(password)


์ด๋ฏธ์ง€ & ์˜์ƒ ์ถœ๋ ฅ

from PIL import Image

# ํด๋” ๋‚ด์˜ ์ด๋ฏธ์ง€ ์ถœ๋ ฅ
img = Image.open('image.JPG')
st.image(img)

# ๊ฒฝ๋กœ๋กœ ์ด๋ฏธ์ง€ ์ถœ๋ ฅ
st.image('streamlit/image.JPG')

# ์˜์ƒ ์ถœ๋ ฅ
video_file = open('video.mp4', 'rb')
st.video(video_file)


ํŒŒ์ผ ์—…๋กœ๋“œ

์ด๋ฏธ์ง€ ํŒŒ์ผ ์—…๋กœ๋“œ

img_file = st.file_uploader('์ด๋ฏธ์ง€ ์—…๋กœ๋“œ', type=['png', 'jpg', 'jpeg'])

์ด๋ฏธ์ง€๋ช…์ด ๊ณ ์œ ํ•˜๋„๋ก ์ด๋ฏธ์ง€๋ช… ๋ณ€๊ฒฝ

from datetime import datetime

if img_file is not None:
	current_time = datetime.now()
	filename = current_time.isoformat().replace(":", "_")
	img_file.name = filename + '.jpg'

์ด๋ฏธ์ง€๋ช…์ด ๊ฒน์น  ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์—…๋กœ๋“œํ•œ ์‹œ๊ฐ„์„ ํ™œ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€๋ช…์„ ๋ณ€๊ฒฝํ•œ๋‹ค

 

์‹ค์ œ๋กœ ์ €์žฅ

import os

if not os.path.exists('image'):
    os.makedirs('image')
	
with open(os.path.join('image', img_file.name), 'wb') as f:
    f.write(img_file.getbuffer())

st.success('ํŒŒ์ผ ์—…๋กœ๋“œ ์„ฑ๊ณต')

์‹ค์ œ๋กœ ์ €์žฅ๋œ ๋ชจ์Šต

 

CSV ํŒŒ์ผ ์—…๋กœ๋“œ

csv_file = st.file_uploader('CSV ์—…๋กœ๋“œ', type=['csv'])

ํ•ด๋‹น ๋ถ€๋ถ„๋งŒ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋‚˜๋จธ์ง€๋Š” ๋™์ผํ•˜๊ฒŒ ์ง„ํ–‰๋œ๋‹ค


์•ž์˜ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ๋ฐ ์ €์žฅ ํ›„ ์ถœ๋ ฅ

import streamlit as st
import pandas as pd
import numpy as np
from PIL import Image
from datetime import datetime
import os

# ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ
st.subheader('์ด๋ฏธ์ง€ ์—…๋กœ๋“œ')
img_file = st.file_uploader('',type=['png', 'jpg', 'jpeg'])

if img_file is not None:

	# ์ด๋ฏธ์ง€๋ช…์ด ๊ณ ์œ ํ•˜๋„๋ก ์‹œ๊ฐ„์„ ํ™œ์šฉํ•˜์—ฌ ๋ณ€๊ฒฝ
	current_time = datetime.now()
	filename = current_time.isoformat().replace(":", "_")
	img_file.name = filename + '.jpg'

	# ์‹ค์ œ๋กœ ์ €์žฅ
	if not os.path.exists('image'):
		os.makedirs('image')
		
	with open(os.path.join('image', img_file.name), 'wb') as f:
		f.write(img_file.getbuffer())

	st.success('ํŒŒ์ผ ์—…๋กœ๋“œ ์„ฑ๊ณต! ๋ณ€๊ฒฝ๋œ ๊ณ ์œ ํ•œ ํŒŒ์ผ๋ช… : ' + img_file.name)


	# ๊ฒฝ๋กœ๋กœ ์ด๋ฏธ์ง€ ์ถœ๋ ฅ
	st.subheader('์—…๋กœ๋“œํ•œ ์ด๋ฏธ์ง€')
	img = Image.open('image/'+img_file.name)
	st.image(img)

 

streamlit์„ ์‹คํ–‰์‹œํ‚ค๊ณ  ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜์—ฌ ํŒŒ์ผ์ด ์—…๋กœ๋“œ๋œ ๊ฒƒ๊ณผ ๋ณ€๊ฒฝ๋œ ์ด๋ฏธ์ง€๋ช…์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ , ์—…๋กœ๋“œํ•œ ์ด๋ฏธ์ง€๋ฅผ ์ถœ๋ ฅํ•œ ๊ฒฐ๊ณผ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค