Python/Streamlit

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

567Rabbit 2024. 4. 26. 17:44
# ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•˜๋Š” ๋ฐฉ๋ฒ•
# ์ด๋ฏธ์ง€ ํŒŒ์ผ ์—…๋กœ๋“œ, csv ํŒŒ์ผ ์—…๋กœ๋“œ

import streamlit as st
from datetime import datetime       # ํ˜„์žฌ์‹œ๊ฐ„์„ ๊ฐ€์ ธ์™€์„œ ์œ ๋‹ˆํฌํ•œ ํŒŒ์ผ๋ช…์„ ๋งŒ๋“œ๋Š”๋ฐ ์‚ฌ์šฉํ•  ๋ชฉ์ ์œผ๋กœ ์ž„ํฌํŠธ
import pandas as pd
from PIL import Image


# ๋””๋ ‰ํ† ๋ฆฌ ์ •๋ณด์™€ ํŒŒ์ผ์„ ์•Œ๋ ค์ฃผ๋ฉด
# ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ์— ํŒŒ์ผ์„ ์ €์žฅํ•˜๋Š” ํ•จ์ˆ˜
def save_uploaded_file(directory, file):
    # 1. ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ์—†์œผ๋ฉด ๋””๋ ‰ํ† ๋ฆฌ๋ถ€ํ„ฐ ๋งŒ๋“ ๋‹ค.
    import os
    if not os.path.exists(directory):
        os.makedirs(directory)
    # 2. ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์กด์žฌํ•˜๋ฉด, ํŒŒ์ผ์„ ์ €์žฅํ•œ๋‹ค.
    with open(os.path.join(directory, file.name), 'wb') as f: # with expression as ๋ณ€์ˆ˜๋ช… => ์‚ฌ์šฉ์ž๊ฐ€ close()๋ฅผ ํ•˜์ง€ ์•Š์•„๋„ ์ž๋™์œผ๋กœ ํŒŒ์ผ์„ ๋‹ซ์•„์คŒ
        f.write(file.getbuffer())
    # 3. ์ €์žฅ์ด ์™„๋ฃŒ๋˜๋ฉด ์œ ์ €ํ•œํ…Œ ์•Œ๋ฆฐ๋‹ค.
    return st.success(f"{file.name} ์ด {directory} ์— ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.")

def main():
    # ์‚ฌ์ด๋“œ๋ฐ” ๋งŒ๋“ค๊ธฐ
    st.title('ํŒŒ์ผ ์—…๋กœ๋“œ ํ”„๋กœ์ ํŠธ')

    sidebar_menu = ['์ด๋ฏธ์ง€ํŒŒ์ผ ์—…๋กœ๋“œ', 'csv ์—…๋กœ๋“œ', 'About']
    choice = st.sidebar.selectbox('๋ฉ”๋‰ด', sidebar_menu) 

    if choice == sidebar_menu[0]: # ์ด๋ฏธ์ง€ํŒŒ์ผ ์—…๋กœ๋“œ ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ–ˆ์„์‹œ
        st.subheader('์ด๋ฏธ์ง€ ํŒŒ์ผ ์—…๋กœ๋“œ!')
        file = st.file_uploader('์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ์„ ํƒํ•˜์„ธ์š”.', type=['jpg', 'png', 'jpeg']) # ์ด๋ฏธ์ง€ํŒŒ์ผ ํ˜•์‹์„ ํ•˜๋‚˜๋งŒ ํ—ˆ์šฉํ• ๊ฑฐ๋ฉด ๊ทธ๋ƒฅ ์“ฐ๋ฉด ๋˜๊ณ  ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ํ—ˆ์šฉํ• ์‹œ์—๋Š” [] ๋ฆฌ์ŠคํŠธ๋กœ ์ž‘์„ฑ
        
        if file is not None: # ์œ ์ €๊ฐ€ ์˜ฌ๋ฆฐ ํŒŒ์ผ์ด ์กด์žฌํ• ๋•Œ๋งŒ ์„œ๋ฒ„์— ์ €์žฅํ•œ๋‹ค.
            print(file.name) # ํŒŒ์ผ ์ด๋ฆ„
            print(file.size) # ํŒŒ์ผ ํฌ๊ธฐ
            print(file.type) # ํŒŒ์ผ ํ˜•์‹
            
            # ํŒŒ์ผ์„ ์„œ๋ฒ„์— ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จผ์ €!
            # ํŒŒ์ผ ์ด๋ฆ„์„ ์œ ๋‹ˆํฌํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์„œ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•œ๋‹ค.
            # "ํ˜„์žฌ์‹œ๊ฐ„ + ์œ ์ € ์•„์ด๋””" ๋ฅผ ์กฐํ•ฉํ•ด์„œ ๋งŒ๋“œ๋Š”๊ฒŒ ์‹ค๋ฌด์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ
            current_time = datetime.now()
            new_filename = current_time.isoformat().replace(':', '_') + '.jpg'
            # .isoformat() ์„ ํ•ด์ฃผ๋ฉด datetimeํ˜•์‹์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•ด์คŒ
            # ํŒŒ์ผ๋ช…์—๋Š” :(์ฝœ๋ก ) ์ด ํฌํ•จ๋  ์ˆ˜ ์—†์Œ. => replace()๋ฅผ ์ด์šฉํ•˜์—ฌ : ์„ _ ๋กœ ๋ฐ”๊ฟ”์คŒ
            # .jpg๋ฅผ ๋ถ™์—ฌ์„œ jpgํ˜•์‹์œผ๋กœ ํ†ต์ผํ•ด์„œ ์ €์žฅ

            file.name = new_filename
            save_uploaded_file('image', file) # ํŒŒ์ผ์„ image๋””๋ ‰ํ† ๋ฆฌ์— ์ €์žฅ

            # ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ
            # 1. ์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•
            st.image(file, use_column_width=True) # use_column_width=True๋ฅผ ํ•ด์ฃผ๋ฉด ์ž๋™์œผ๋กœ ํญ์„ ๋งž์ถฐ์คŒ
    
            # 2. ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•
            img = Image.open(file)
            st.image(img)


    elif choice == sidebar_menu[1]: # csv ์—…๋กœ๋“œ ๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ–ˆ์„์‹œ
        st.subheader('CSVํŒŒ์ผ ์—…๋กœ๋“œ!')
        csv_file = st.file_uploader('CSV ํŒŒ์ผ์„ ์„ ํƒํ•˜์„ธ์š”', type='csv') # ์ด๋ฏธ์ง€ ์—…๋กœ๋“œํ• ๋•Œ์™€ ๋™์ผํ•œ ๋ฐฉ์‹

        if csv_file is not None :

            # ํŒŒ์ผ๋ช…์„ ์œ ๋‹ˆํฌ ํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์„œ ์ €์žฅํ•œ๋‹ค
            current_time = datetime.now()
            new_filename = current_time.isoformat().replace(':', '_') + '.csv' # ์œ ๋‹ˆํฌํ•œ ํŒŒ์ผ๋ช…(ํ˜„์žฌ์‹œ๊ฐ„)์œผ๋กœ ์ €์žฅ
            csv_file.name = new_filename  # csv_file ์ด๋ฆ„์„ new_filename์œผ๋กœ ๋ณ€๊ฒฝ

            save_uploaded_file('data', csv_file)  # data๋””๋ ‰ํ† ๋ฆฌ๋กœ csv_file ์ €์žฅ

            st.dataframe(pd.read_csv(csv_file))


    elif choice == sidebar_menu[2]: # About ๋ฉ”๋‰ด ์„ ํƒ์‹œ
        st.subheader('ํŒŒ์ผ ์—…๋กœ๋“œ ํ”„๋กœ์ ํŠธ์ž…๋‹ˆ๋‹ค.')

    else:
        pass

if __name__ == '__main__':
    main()

 

 

 

 

 

 

 

 

 

VScode์— ๊ฐ€๋ณด๋ฉด,

 

์ดˆ๋ก์ƒ‰์ด ์ƒˆ๋กœ ์ €์žฅ๋œ ์ด๋ฏธ์ง€, csv ํŒŒ์ผ์ด๋‹ค.