1 1 Columns Explained
Key Concepts
- Columns: A layout element in Streamlit that allows you to organize content into separate vertical sections.
- st.columns: A Streamlit function that creates a set of columns.
- Column Width: The relative width of each column within the layout.
- Column Content: The elements or widgets placed inside each column.
Explanation
1. Columns
Columns in Streamlit are a way to organize content into separate vertical sections. This layout element is useful for creating complex interfaces where different types of content need to be displayed side by side.
2. st.columns
st.columns
is a Streamlit function that creates a set of columns. You can specify the number of columns and their relative widths. This function returns a list of column objects that you can use to place content inside each column.
3. Column Width
The width of each column can be specified as a fraction of the total width of the container. For example, if you create three columns with widths of 1, 2, and 1, the second column will be twice as wide as the first and third columns.
4. Column Content
Content inside each column can include text, widgets, charts, and other Streamlit elements. You can place any Streamlit element inside a column by using the column object returned by st.columns
.
Examples
Example 1: Basic Columns
import streamlit as st col1, col2, col3 = st.columns(3) with col1: st.write("Column 1") with col2: st.write("Column 2") with col3: st.write("Column 3")
Example 2: Columns with Different Widths
import streamlit as st col1, col2, col3 = st.columns([1, 2, 1]) with col1: st.write("Column 1") with col2: st.write("Column 2") with col3: st.write("Column 3")
Analogies
Think of columns in Streamlit as lanes on a highway. Each lane (column) can carry different types of vehicles (content). The width of each lane can vary, allowing for more space for certain types of vehicles. By organizing content into columns, you can create a more structured and visually appealing interface.
By mastering columns in Streamlit, you can create complex and dynamic interfaces that make your applications more user-friendly and efficient.