π Β Β Grid Layout
Submitted by Lukas Masuch
Summary
A multi-element container that places elements on a specified grid layout.
Functions
grid
Insert a multi-element, grid container into your app.
This function inserts a container into your app that arranges multiple elements in a grid layout as defined by the provided spec. Elements can be added to the returned container by calling methods directly on the returned object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*spec |
int | Iterable[int]
|
One or many row specs controlling the number and width of cells in each row.
Each spec can be one of:
* An integer specifying the number of cells. In this case, all cells have equal
width.
* An iterable of numbers (int or float) specifying the relative width of
each cell. E.g., |
()
|
gap |
Optional[str]
|
The size of the gap between cells, specified as "small", "medium", or "large". This parameter defines the visual space between grid cells. Defaults to "small". |
'small'
|
vertical_align |
Literal['top', 'center', 'bottom']
|
The vertical alignment of the cells in the row. Defaults to "top". |
'top'
|
Source code in src/streamlit_extras/grid/__init__.py
Import:
- You should add this to the top of your .py file
Examples
example
def example():
random_df = pd.DataFrame(np.random.randn(20, 3), columns=["a", "b", "c"])
my_grid = grid(2, [2, 4, 1], 1, 4, vertical_align="bottom")
# Row 1:
my_grid.dataframe(random_df, use_container_width=True)
my_grid.line_chart(random_df, use_container_width=True)
# Row 2:
my_grid.selectbox("Select Country", ["Germany", "Italy", "Japan", "USA"])
my_grid.text_input("Your name")
my_grid.button("Send", use_container_width=True)
# Row 3:
my_grid.text_area("Your message", height=40)
# Row 4:
my_grid.button("Example 1", use_container_width=True)
my_grid.button("Example 2", use_container_width=True)
my_grid.button("Example 3", use_container_width=True)
my_grid.button("Example 4", use_container_width=True)
# Row 5 (uses the spec from row 1):
with my_grid.expander("Show Filters", expanded=True):
st.slider("Filter by Age", 0, 100, 50)
st.slider("Filter by Height", 0.0, 2.0, 1.0)
st.slider("Filter by Weight", 0.0, 100.0, 50.0)
my_grid.dataframe(random_df, use_container_width=True)