๊ด€๋ฆฌ ๋ฉ”๋‰ด

ruriruriya

[Python] ํŒŒ์ด์ฌ ์ŠคํŠธ๋ฆผ๋ฆฟ(Streamlit) - ๋‚ ์งœ/์‹œ๊ฐ„ ์ž…๋ ฅ๋ฐ›๊ธฐ(date_input, time_input) ๋ณธ๋ฌธ

๐ŸPython/Streamlit

[Python] ํŒŒ์ด์ฌ ์ŠคํŠธ๋ฆผ๋ฆฟ(Streamlit) - ๋‚ ์งœ/์‹œ๊ฐ„ ์ž…๋ ฅ๋ฐ›๊ธฐ(date_input, time_input)

๋ฃจ๋ฆฌ์•ผใ…‘ 2023. 11. 25. 23:53
๋ฐ˜์‘ํ˜•

์ŠคํŠธ๋ฆผ๋ฆฟ(Streamlit)์˜ date_input ๋ฐ time_input ํ•จ์ˆ˜๋Š” ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๊ฐ๊ฐ ๋‚ ์งœ์™€ ์‹œ๊ฐ„์„ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ํŠน์ • ๋‚ ์งœ๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•˜๊ณ , ์„ ํƒํ•œ ๋‚ ์งœ๋ฅผ ๋ณ€์ˆ˜์— ์ €์žฅํ•˜์—ฌ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

  • "๋‚ ์งœ๋ฅผ ์„ ํƒํ•˜์„ธ์š”"๋Š” ๋‚ ์งœ๋ฅผ ์„ ํƒํ•˜๋Š” ๋‹ฌ๋ ฅ ์œ„์— ํ‘œ์‹œ๋˜๋Š” ์„ค๋ช… ํ…์ŠคํŠธ
  • datetime.today()๋Š” ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์˜ค๋Š˜ ๋‚ ์งœ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ’์€ ์„ ํƒ๋œ ๋‚ ์งœ๊ฐ€ ์—†์„ ๋•Œ ์‚ฌ์šฉ

 

1. date_input

* strftime() ๋ฉ”์†Œ๋“œ๋Š” ํŒŒ์ด์ฌ์˜ ๋ฉ”์†Œ๋“œ๋กœ ๋‚ ์งœ ๋ฐ ์‹œ๊ฐ„์„ ์›ํ•˜๋Š” ํ˜•์‹์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.

import streamlit as st
def main():

# ๋‚ ์งœ ์ž…๋ ฅ๋ฐ›๋Š” ๋ฐฉ๋ฒ•

    my_date = st.date_input('์•ฝ์† ๋‚ ์งœ ์ž…๋ ฅ')

    st.write(my_date)

    st.text(type(my_date))
    
    # "2023๋…„ 11์›” 12์ผ ํ™”์š”์ผ์ž…๋‹ˆ๋‹ค. ๋ผ๊ณ  ์›นํ™”๋ฉด์— ํ‘œ์‹œ!
    st.subheader(my_date.strftime('%Y๋…„ %m์›” %d์ผ %A ์ž…๋‹ˆ๋‹ค.')) 
    # ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์‚ฌ๋žŒ์ด ์›ํ•˜๋Š” ํ˜•์‹์œผ๋กœ ์‰ฝ๊ฒŒ ๊ฐ€๊ณต

if __name__ == '__main__':
    main()

 

[๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด]

2. time_input

time_input ํ•จ์ˆ˜๋Š” ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์‹œ๊ฐ„์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ์‹œ๊ณ„๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ํŠน์ •ํ•œ ์‹œ๊ฐ„์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•œ๋‹ค. 
์•„๋ž˜ ์˜ˆ์ œ๋Š” ํŒŒ์ด์ฌ์˜ strftime ๋ฉ”์†Œ๋“œ๋กœ ์›ํ•˜๋Š” ํ˜•์‹์œผ๋กœ ๊ฐ€๊ณตํ•˜์—ฌ ์ถœ๋ ฅํ–ˆ๋‹ค.

import streamlit as st
def main():

    # ์‹œ๊ฐ„ ์ž…๋ ฅ๋ฐ›๋Š” ๋ฐฉ๋ฒ•
    my_time = st.time_input('์•ฝ์† ์‹œ๊ฐ„ ์„ ํƒ')
    print(type(my_time))
    st.text(my_time.strftime('%H:%M์— ์•ฝ์†์‹œ๊ฐ„์„ ์žก์•˜์Šต๋‹ˆ๋‹ค.'))


if __name__ == '__main__':
    main()

 

[๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด]

๋ฐ˜์‘ํ˜•