###### 파일을 업로드 하는 방법 #########
###### 이미지 파일, CSV 파일 업로드
import streamlit as st
from PIL import Image
import pandas as pd
import os
from datetime import datetime
# 디렉토리 정보와 파일을 알려주면, 해당 디렉토리에
# 파일을 저장하는 함수
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())
return st.success("Saved file : {} in {}".format(file.name, directory))
def main():
# 사이드바 만들기
st.title('파일 업로드 프로젝트')
menu = ['Image', 'CSV', 'About']
choice = st.sidebar.selectbox('메뉴', menu)
if choice == menu[0] :
st.subheader('이미지 파일 업로드')
upload_file = st.file_uploader('이미지 파일 선택', type=['jpg','png','jpeg'])
if upload_file is not None :
print(upload_file.name)
print(upload_file.size)
print(upload_file.type)
# 파일명을 유니크하게 만들어서 저장해야 한다.
# 현재시간을 활용해서, 파일명을 만든다.
current_time = datetime.now()
print(current_time.isoformat().replace(':', '_'))
new_filename = current_time.isoformat().replace(':', '_') + '.jpg'
upload_file.name = new_filename
save_uploaded_file('temp', upload_file)
elif choice == menu[1] :
st.subheader('CSV 파일 업로드')
upload_file = st.file_uploader('CSV 파일 선택', type = ['csv'])
if upload_file is not None:
# 파일명을 유니크하게 만들어서 저장해야 한다.
# 현재시간을 활용해서, 파일명을 만든다.
current_time = datetime.now()
print(current_time.isoformat().replace(':', '_'))
new_filename = current_time.isoformat().replace(':', '_') + '.csv'
upload_file.name = new_filename
save_uploaded_file('temp', upload_file)
else :
st.subheader('파일 업로드 프로젝트 입니다.')
if __name__=='__main__' :
main()
from email import message
import streamlit as st
def main():
# 유저한테 입력을 받는 방법
# 1. 이름 입력 받기
name = st.text_input('이름을 입력하세요!')
if name != '' :
st.subheader(name + '님 안녕하세요??')
# 2. 입력 글자 갯수 제한
address = st.text_input('주소를 입력하세요', max_chars=10)
st.subheader(address)
# 3. 여러 행을 입력가능토록
message = st.text_area('메세지를 입력하세요', height=3)
st.subheader(message)
# 4. 숫자 입력 , 정수
st.number_input('숫자 입력', 1, 100)
# 5. 숫자 입력 , 실수
st.number_input('실수 입력', 1.0, 100.0)
# 6. 날짜 입력
my_date = st.date_input('약속날짜')
st.write(my_date)
# 요일 찍기
st.write( my_date.weekday() )
st.write( my_date.strftime('%A'))
# 7. 시간 입력
my_time = st.time_input('시간 선택')
st.write(my_time)
# 8. 색깔 입력
color = st.color_picker('색을 선택하세요')
st.write(color)
# 9. 비밀번호 입력
password = st.text_input('비밀번호 입력', type='password')
st.write(password)
if __name__ == '__main__':
print(__name__)
main()
import streamlit as st
import pandas as pd
# 이미지 처리를 위한 라이브러리
from PIL import Image
def main():
# 1. 저장되어있는, 이미지 파일을, 화면에 표시하기
img = Image.open('data2/image_03.jpg')
st.image(img)
st.image(img, use_column_width=True)
# 2. 인터넷상에 있는 이미지를 화면에 표시하기.
# URL이 있는 이미지를 말한다.
url = 'https://dimg.donga.com/wps/NEWS/IMAGE/2019/07/01/96260737.1.jpg'
st.image(url)
video_file = open('data2/secret_of_success.mp4', 'rb')
st.video(video_file)
audio_file = open('data2/song.mp3', 'rb')
st.audio( audio_file.read() , format='audio/mp3' )
if __name__ == '__main__' :
main()
import streamlit as st
import pandas as pd
def main():
df = pd.read_csv('data2/iris.csv')
st.dataframe(df)
species = df['species'].unique()
st.text('아이리스 꽃은 ' + species + '으로 되어있다.')
st.dataframe(df.head())
st.write(df.head())
if __name__ == '__main__':
main()
import streamlit as st
def main():
st.title('웹 대시보드')
st.text('웹 대시보드 개발하기')
name = '홍길동'
# 제 이름은 홍길동 입니다.
print('제 이름은 {}입니다.'.format(name))
st.text('제 이름은 {}입니다.'.format(name))
st.header('이 영역은 헤더 영역')
st.subheader('이 영역은 subheader영역')
st.success('작업이 성공했을때 사용하자')
st.warning('경고 문구를 보여주고 싶을 때 사용하자.')
st.info('정보를 보여주고 싶을 때')
st.error('문제가 발생했을 때 사용')
# 파이썬의 함수 사용법을 보여주고 싶을 때
st.help(sum)
st.help(len)
if __name__ == '__main__':
main()