What is Streamlit?
Streamlit is an open-source Python framework that simplifies the process of creating interactive web applications for data science, machine learning, and AI projects. It allows developers to quickly build web-based front-end applications using just Python code—without needing HTML, CSS, or JavaScript.
Streamlit is particularly popular among data scientists and machine learning engineers because it makes creating interactive dashboards, visualizations, and models extremely easy.
Main Uses of Streamlit:
- Data Visualization: Quickly create interactive charts using libraries like Matplotlib, Plotly, or Altair.
- Machine Learning Apps: Build web apps to showcase ML models with sliders, dropdowns, and inputs for user interaction.
- Prototyping: Create quick prototypes for data exploration and model inference.
- Dashboard Creation: Build interactive dashboards for data analysis.
- Custom Tools: Create internal tools for automating tasks or sharing model outputs.
How to Install Streamlit
Install Streamlit using pip:
pip install streamlit
How to Create Dropdowns in Streamlit
In Streamlit, dropdown menus can be created using the st.selectbox() method.
Example Code:
import streamlit as st
# Title
st.title("Dropdown Example with Streamlit")
# Dropdown Menu
options = ["Option 1", "Option 2", "Option 3"]
selected_option = st.selectbox("Choose an option:", options)
# Display the selected option
st.write(f"You selected: {selected_option}")
Explanation:
- st.selectbox(): This creates a dropdown with selectable options.
- options: List of options to display in the dropdown.
- selected_option: The variable stores the value selected by the user.
- st.write(): Displays the selected option on the screen.
Advanced Dropdown with Default Value
You can set a default value by specifying the index parameter:
selected_option = st.selectbox("Choose an option:", options, index=1)
In this case, "Option 2" will be pre-selected.
Multi-Select Dropdown
For allowing multiple selections, use:
selected_options = st.multiselect("Choose multiple options:", options)
st.write("You selected:", selected_options)
Running the Streamlit App
Save your file (e.g., app.py) and run the following command in the terminal:
streamlit run app.py
Conclusion
Streamlit is a powerful and easy-to-use tool for creating interactive web applications with minimal effort. With built-in widgets like dropdowns, sliders, and charts, it is perfect for data science projects, machine learning demos, and quick app prototyping.
Comments
Post a Comment