본문 바로가기
Python/Streamlit

[Python] Streamlit 파일 업로드

by coding_su 2022. 12. 13.

📝Python Streamlit 파일 업로드 하기

import와 함수 정의

import streamlit as st
import pandas as pd
import os
from datetime import date, datetime
from PIL import Image

# 함수 정의. 디렉토리(폴더)명과 파일을 알려주면해당 디렉토리에 파일을 저장해 주는 함수

def save_uploaded_file(directory, file) :
# 1. 디렉토리가 있는지 확인하여 없으면 먼저 디렉토리부터 만든다
    if not os.path.exists(directory) :
        os.makedirs(directory)
# 2. 디렉토리가 있으니 파일을 저장한다
    with open(os.path.join(directory, file.name), 'wb') as f :
        f.write(file.getbuffer())
# 3. 파일 저장이 성공했으니 화면에 성공했다고 보여주면서 리턴
    return st.success('{} 에 {} 파일이 저장되었습니다'.format(directory, file.name))
    
    ↓ ↓ ↓ ↓ ↓ ↓
    
def save_uploaded_file(directory, file) :
    if not os.path.exists(directory) :
    with open(os.path.join(directory, file.name), 'wb') as f :
        f.write(file.getbuffer())
    return st.success('{} 에 {} 파일이 저장되었습니다'.format(directory, file.name))

 

사이드바 생성, 각 메뉴에 조건문 입력

def main() :
    st.title('파일 업로드 프로젝트')
	
    # 메뉴바 생성
    menu = ['Image', 'CSV', 'About']
    choice = st.sidebar.selectbox('메뉴', menu)
    
    # 메뉴바가 Image 일때
    if choice == 'Image' :
        st.subheader('이미지 파일 업로드')
        file = st.file_uploader('이미지를 업로드 하세요', type=['jpg', 'jpeg', 'png'])

        if file is not None :
            # 현재시간을 조합하여 파일명을 만들면 유니크하게 파일명을 만들 수 있다
            current_time = datetime.now()
            current_time = current_time.isoformat().replace(':', '_')
            file.name = current_time + '.jpg'
            
            # 바꾼 파일명으로 파일을 서버에 저장한다
            save_uploaded_file('tmp', file)

            # 파일을 웹 화면에 나오게
            img = Image.open(file)
            st.image(img)
            
	# 메뉴바가 CSV 일때
    elif choice == 'CSV' :
        st.subheader('CSV 파일 업로드')
        file = st.file_uploader('CSV 파일 업로드', type=['csv'])
        
        if file is not None :
            # 파일명을 유니크하게 만든다
            current_time = datetime.now()
            current_time = current_time.isoformat().replace(':', '_')
            file.name = current_time + '.csv'

            #파일을 서버에 저장한다
            save_uploaded_file('csv', file)

            # csv파일은 판다스로 읽어서 화면에 보여준다
            df = pd.read_csv(file)
            st.dataframe(df)
    
    # 메뉴바가 About 일때
    elif choice == 'About' :
        st.subheader('파일 업로드 프로젝트 입니다')

if __name__ == '__main__' :
    main()

 

실행 결과 ↓

댓글