? Dapatkan Kod: GitHub - jamesbmour/blog_tutorials
? Tutorial Streamlit Berkaitan:JustCodeIt
Streamlit telah merevolusikan cara kami mencipta aplikasi web dengan Python. Kesederhanaan dan kuasanya menjadikannya pilihan yang sangat baik untuk saintis data dan pembangun. Dalam siaran ini, kami akan menyelami salah satu ciri Streamlit yang paling berkuasa: widget input. Kami akan meneroka 16 jenis input yang berbeza, menunjukkan cara menggunakannya dengan berkesan dalam apl Streamlit anda.
Sebelum kita menyelami widget, mari sediakan apl Streamlit kami:
import streamlit as st st.set_page_config(layout="wide") st.title("Streamlit Part 4: Inputs in Streamlit") col1, col2 = st.columns(2)
Kami telah mengimport Streamlit, menetapkan halaman kepada reka letak yang luas, menambah tajuk dan mencipta dua lajur untuk penyusunan widget kami yang lebih baik.
Bentuk input yang paling mudah ialah butang. Begini cara untuk menciptanya:
with col1: st.subheader("1. Button") btn1 = st.button("Click Me", key="button", help="Click me to see the magic", type='secondary', disabled=False) if btn1: st.write("Button Clicked")
Penjelasan Terperinci:
Kes Penggunaan:
Petua: Gunakan keadaan butang untuk mengawal aliran apl anda, seperti menunjukkan/menyembunyikan bahagian atau mencetuskan pengiraan.
Untuk mengubah hala pengguna ke pautan luaran, gunakan butang pautan:
st.subheader("2. Link Button") if st.link_button("Click Me", "<https://www.streamlit.io/>"): st.write("Link Button Clicked")
Penjelasan Terperinci:
Kes Penggunaan:
Petua: Gunakan butang pautan dengan berhati-hati untuk mengelakkan pengguna menjauhi apl anda tanpa perlu.
Benarkan pengguna memuat turun fail terus daripada apl anda:
st.subheader("3. Download Button") if st.download_button("Download Me", "hello world", "hello.txt", mime='text/plain'): st.write("Download Button Clicked")
Penjelasan Terperinci:
Kes Penggunaan:
Petua: Anda boleh menjana kandungan fail secara dinamik berdasarkan interaksi pengguna atau hasil pemprosesan data.
Kotak pilihan bagus untuk menogol pilihan:
st.subheader("4. Checkbox") checkbox_val = st.checkbox("Check Me", value=False) if checkbox_val: st.write("Checkbox Checked")
Penjelasan Terperinci:
Kes Penggunaan:
Petua: Gunakan kotak pilihan untuk mengawal keterlihatan elemen lain dalam apl anda untuk pengalaman pengguna yang lebih dinamik.
Apabila pengguna perlu memilih satu pilihan daripada senarai:
st.subheader("5. Radio") radio_val = st.radio("Select Color", ["Red", "Green", "Blue"], index=0) if radio_val: st.write(f"You selected {radio_val}")
Penjelasan Terperinci:
Kes Penggunaan:
Petua: Gunakan butang radio apabila anda mempunyai sebilangan kecil pilihan (biasanya 2-5) yang saling eksklusif.
Untuk pilihan lungsur turun:
st.subheader("6. Selectbox") select_val = st.selectbox("Select Color", ["Red", "Green", "Blue", "Black"], index=1) if select_val: st.write(f"You selected {select_val}")
Penjelasan Terperinci:
Kes Penggunaan:
Tip: You can populate the options dynamically based on data or user inputs.
Allow users to select multiple options:
st.subheader("7. Multiselect") multiselect_val = st.multiselect("Select Colors", ["Red", "Green", "Blue", "Black"], default=["Red"]) if multiselect_val: st.write(f"You selected {multiselect_val}")
Detailed Explanation:
Use Cases:
Tip: Use st.multiselect() when you want users to be able to select any number of options, including none or all.
For selecting from a range of discrete values:
st.subheader("8. Select Slider") select_slider_val = st.select_slider("Select Value", options=range(1, 101), value=50) if select_slider_val: st.write(f"You selected {select_slider_val}")
Detailed Explanation:
Use Cases:
Tip: You can use custom labels for the slider by passing a list of tuples (label, value) as options.
For single-line text input:
with col2: st.subheader("9. Text Input") text_input_val = st.text_input("Enter some text", value="", max_chars=50) if text_input_val: st.write(f"You entered {text_input_val}")
Detailed Explanation:
Use Cases:
Tip: Use the type parameter to create password fields or other specialized inputs.
For multi-line text input:
st.subheader("10. Text Area") text_area_val = st.text_area("Enter some text", value="", height=150, max_chars=200) if text_area_val: st.write(f"You entered {text_area_val}")
Detailed Explanation:
Use Cases:
Tip: You can use st.text_area() with natural language processing models for text analysis or generation tasks.
For numerical inputs:
st.subheader("11. Number Input") number_input_val = st.number_input("Enter a number", value=0, min_value=0, max_value=100, step=1) if number_input_val: st.write(f"You entered {number_input_val}")
Detailed Explanation:
Use Cases:
Tip: You can use format parameter to control the display of decimal places.
For selecting dates:
st.subheader("12. Date Input") date_input_val = st.date_input("Enter a date") if date_input_val: st.write(f"You selected {date_input_val}")
Detailed Explanation:
Use Cases:
Tip: Use datetime.date.today() as the default value to start with the current date.
For selecting times:
st.subheader("13. Time Input") time_input_val = st.time_input("Enter a time") if time_input_val: st.write(f"You selected {time_input_val}")
Detailed Explanation:
Use Cases:
Tip: Combine with st.date_input() to create full datetime inputs.
For uploading files:
st.subheader("14. File Uploader") file_uploader_val = st.file_uploader("Upload a file", type=["png", "jpg", "txt"]) if file_uploader_val: st.write(f"You uploaded {file_uploader_val.name}")
Detailed Explanation:
Use Cases:
Tip: Use st.file_uploader() in combination with libraries like Pillow or pandas to process uploaded files directly in your app.
For selecting colors:
st.subheader("15. Color Picker") color_picker_val = st.color_picker("Pick a color", value="#00f900") if color_picker_val: st.write(f"You picked {color_picker_val}")
Detailed Explanation:
Use Cases:
Tip: You can use the selected color to dynamically update the appearance of other elements in your app.
For capturing images using the device's camera:
st.subheader("16. Camera Input") camera_input_val = st.camera_input("Take a picture", help="Capture an image using your camera") if camera_input_val: st.write("Picture captured successfully")
Detailed Explanation:
Use Cases:
Tip: Combine with image processing libraries like OpenCV to perform real-time analysis on captured images.
Streamlit's input widgets provide a powerful and flexible way to create interactive web applications. From simple buttons to complex file uploaders and camera inputs, these widgets cover a wide range of use cases. By mastering these input types, you can create rich, interactive Streamlit apps that engage users and provide meaningful interactions with your data and models.
Happy Streamlit coding!
? Get the Code: GitHub - jamesbmour/blog_tutorials
? Related Streamlit Tutorials:JustCodeIt
? Support my work: Buy Me a Coffee
Atas ialah kandungan terperinci Perkemas Bahagian Menguasai Widget Input. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!