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

ruriruriya

[Python] ํŒŒ์ด์ฌ ์ŠคํŠธ๋ฆผ๋ฆฟ(Streamlit) - ํ…์ŠคํŠธ ์ž…๋ ฅ๋ฐ›๊ธฐ(text_input, text_area) ๋ณธ๋ฌธ

๐ŸPython/Streamlit

[Python] ํŒŒ์ด์ฌ ์ŠคํŠธ๋ฆผ๋ฆฟ(Streamlit) - ํ…์ŠคํŠธ ์ž…๋ ฅ๋ฐ›๊ธฐ(text_input, text_area)

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

์ŠคํŠธ๋ฆผ๋ฆฟ(Streamlit)์—์„œ text_input๊ณผ text_area๋Š” ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋‘ ๊ฐ€์ง€ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด๋‹ค.

  • text_input์€ ํ•œ ์ค„์˜ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅ ๋ฐ›์„ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉฐ, ์ฃผ๋กœ ์งง์€ ํ…์ŠคํŠธ์— ์ ํ•ฉํ•˜๋‹ค.
  • text_area๋Š” ์—ฌ๋Ÿฌ ์ค„์˜ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅ ๋ฐ›์„ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉฐ, ๊ธด ์„ค๋ช…์ด๋‚˜ ์—ฌ๋Ÿฌ ์ค„์˜ ์ •๋ณด๋ฅผ ๋ฐ›์„ ๋•Œ ์ ํ•ฉํ•˜๋‹ค.

text_input๊ณผ text_area๋Š” ๊ฐ๊ฐ์˜ ํ…์ŠคํŠธ ์ž…๋ ฅ ์ƒํ™ฉ์— ๋งž๊ฒŒ ์‚ฌ์šฉ๋˜๋ฏ€๋กœ, ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•  ํ…์ŠคํŠธ์˜ ์–‘๊ณผ ํ˜•ํƒœ์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ๊ธฐ๋Šฅ์„ ์„ ํƒํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

1. text.input()

text.input() ํ•จ์ˆ˜๋Š” ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ƒ์ž๋ฅผ ์ œ๊ณตํ•˜๊ณ ,
์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ž…๋ ฅ ์ƒ์ž ์œ„์— ํ‘œ์‹œ๋˜๋Š” ํ…์ŠคํŠธ์ด๋ฉฐ,
๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

์•„๋ž˜์˜ ๋‘ ๋ฒˆ์งธ ์ดํ›„ ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์„ ์‚ดํŽด ๋ณด๋ฉด

max_chars=5 ๋Š” ์ตœ๋Œ€ ์ž…๋ ฅ๊ธ€์ž ์ˆ˜๋ฅผ 5์ž๋กœ ์ œํ•œํ•˜๋Š” ๊ฒƒ์ด๊ณ 
placeholder='ํ™๊ธธ๋™' ๋Š” ์˜ˆ์‹œ ๋‹ต๋ณ€์„ ํ‘œ์‹œํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์“ฐ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์ด๋‹ค.

import streamlit as st
def main():
    #ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š” ๋ฐฉ๋ฒ•
    name = st.text_input('์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”.') 

    st.subheader('์ œ ์ด๋ฆ„์€' + name + '์ž…๋‹ˆ๋‹ค.')

    st.text_input('์ด๋ฆ„ ์ž…๋ ฅ', max_chars=5, placeholder ='ํ™๊ธธ๋™')


if __name__ == '__main__':
    main()

 

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

2.text_area()

text_area() ๋Š” ์—ฌ๋Ÿฌ ์ค„์˜ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ์˜์—ญ์„ ์ƒ์„ฑํ•œ๋‹ค.
์ด๋Š” ์ฃผ๋กœ ๊ธด ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜๊ฑฐ๋‚˜ ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์—ฌ๋Ÿฌ ์ค„์˜ ์ •๋ณด๋ฅผ ๋ฐ›์„ ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด, ๊ธด ์„ค๋ช…์ด๋‚˜ ์—ฌ๋Ÿฌ ์ค„์˜ ํ…์ŠคํŠธ ์ž…๋ ฅ์— ์ ํ•ฉํ•˜๋‹ค.
์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

import streamlit as st
def main():
    #ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š” ๋ฐฉ๋ฒ•

    text = st.text_area('๋ฉ”์„ธ์ง€๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”')

    st.text(text)   


if __name__ == '__main__':
    main()

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

 

๋ฐ˜์‘ํ˜•