- Spec files
pypi-streamlit
Describes: pkg:pypi/streamlit@1.50.x
- Description
- A faster way to build and share data apps
- Author
- tessl
- Last updated
layout-containers.md docs/
1# Layout and Containers23Functions for organizing app layout including columns, containers, sidebars, tabs, and expandable sections. These components provide the structural foundation for organizing content and creating sophisticated user interfaces.45## Capabilities67### Layout Functions89Core layout functions for organizing content in columns, rows, and structured arrangements.1011```python { .api }12def columns(spec, *, gap="small"):13"""14Create column layout for horizontal content arrangement.1516Args:17spec (int or list): Number of columns or list of column widths18gap (str): Gap size between columns ("small", "medium", "large")1920Returns:21list[DeltaGenerator]: List of column containers22"""2324def tabs(tab_list):25"""26Create tab container with multiple named tabs.2728Args:29tab_list (list): List of tab names/labels3031Returns:32list[DeltaGenerator]: List of tab containers33"""34```3536Example usage:37```python38# Create equal-width columns39col1, col2, col3 = st.columns(3)40with col1:41st.write("Column 1")42with col2:43st.write("Column 2")4445# Create tabs46tab1, tab2 = st.tabs(["Data", "Chart"])47with tab1:48st.dataframe(data)49with tab2:50st.line_chart(data)51```5253### Container Functions5455Generic container functions for grouping content with optional styling and behavior.5657```python { .api }58def container(*, height=None, border=None):59"""60Create generic container for grouping content with optional styling.6162Args:63height (int, optional): Container height in pixels64border (bool, optional): Whether to show container border6566Returns:67DeltaGenerator: Container context manager68"""6970def empty():71"""72Create placeholder container that can be filled later.7374Returns:75DeltaGenerator: Empty placeholder container76"""77```7879Example usage:80```python81# Create bordered container82with st.container(border=True):83st.write("Content inside bordered container")8485# Create placeholder for later use86placeholder = st.empty()87# Later fill the placeholder88placeholder.write("Dynamic content")89```9091### Expandable Containers9293Collapsible containers for organizing content that can be shown or hidden.9495```python { .api }96def expander(label, *, expanded=False, icon=None):97"""98Create expandable/collapsible container with toggle functionality.99100Args:101label (str): Expander header text102expanded (bool): Whether to start in expanded state103icon (str, optional): Icon name or emoji for header104105Returns:106DeltaGenerator: Expandable container context manager107"""108109def popover(label, *, help=None, disabled=False, use_container_width=False, icon=None):110"""111Create popover container that opens on button click.112113Args:114label (str): Popover button text115help (str, optional): Tooltip text for button116disabled (bool): Whether popover button is disabled117use_container_width (bool): Whether button uses full container width118icon (str, optional): Icon name or emoji for button119120Returns:121DeltaGenerator: Popover container context manager122"""123```124125Example usage:126```python127# Create expander128with st.expander("Show details", expanded=False):129st.write("Detailed information here")130131# Create popover132with st.popover("Options"):133st.write("Popover content")134option = st.selectbox("Choose:", ["A", "B", "C"])135```136137### Sidebar138139Dedicated sidebar container for navigation and secondary content.140141```python { .api }142sidebar: DeltaGenerator143```144145The sidebar object provides access to the sidebar container where you can place widgets and content that appears in a collapsible side panel.146147Example usage:148```python149# Add content to sidebar150st.sidebar.title("Navigation")151st.sidebar.selectbox("Choose page:", ["Home", "About", "Contact"])152153# Sidebar can contain any widget or display element154with st.sidebar:155st.write("Sidebar content")156user_input = st.text_input("Enter value:")157```158159### Status and Progress Elements160161Containers and widgets for showing status, progress, and temporary states.162163```python { .api }164def status(label, *, expanded=False, state="running"):165"""166Create status container that shows operation progress with state indicator.167168Args:169label (str): Status label/description170expanded (bool): Whether to show details by default171state (str): Status state ("running", "complete", "error")172173Returns:174DeltaGenerator: Status container context manager175"""176177def progress(value, *, text=None, help=None):178"""179Display progress bar with optional text label.180181Args:182value (float): Progress value between 0.0 and 1.0183text (str, optional): Text label for progress bar184help (str, optional): Tooltip text185186Returns:187DeltaGenerator: Progress bar element that can be updated188"""189190def spinner(text=""):191"""192Display loading spinner with optional text during long operations.193194Args:195text (str): Loading message text196197Returns:198ContextManager: Spinner context manager for use with 'with' statement199"""200```201202Example usage:203```python204# Status container205with st.status("Processing data...", expanded=True):206st.write("Loading dataset")207# ... processing code ...208st.write("Analysis complete")209210# Progress bar211progress_bar = st.progress(0, text="Starting...")212for i in range(100):213progress_bar.progress(i + 1, text=f"Processing {i+1}/100")214215# Spinner for long operations216with st.spinner("Loading..."):217time.sleep(5) # Long operation218st.success("Done!")219```220221### Message and Alert Containers222223Containers for displaying status messages, alerts, and notifications.224225```python { .api }226def success(body, *, icon=None, help=None):227"""228Display success message with green styling.229230Args:231body (str): Success message text232icon (str, optional): Custom icon (default checkmark)233help (str, optional): Tooltip text234"""235236def info(body, *, icon=None, help=None):237"""238Display informational message with blue styling.239240Args:241body (str): Info message text242icon (str, optional): Custom icon (default info icon)243help (str, optional): Tooltip text244"""245246def warning(body, *, icon=None, help=None):247"""248Display warning message with yellow styling.249250Args:251body (str): Warning message text252icon (str, optional): Custom icon (default warning icon)253help (str, optional): Tooltip text254"""255256def error(body, *, icon=None, help=None):257"""258Display error message with red styling.259260Args:261body (str): Error message text262icon (str, optional): Custom icon (default error icon)263help (str, optional): Tooltip text264"""265266def exception(exception, *, help=None):267"""268Display formatted exception with traceback information.269270Args:271exception (Exception): Exception object to display272help (str, optional): Tooltip text273"""274275def toast(body, *, icon=None, duration="short"):276"""277Display temporary toast notification that auto-dismisses.278279Args:280body (str): Toast message text281icon (str, optional): Icon name or emoji282duration (str or int, optional): How long to show toast ("short", "long", "infinite", or seconds)283"""284```285286Example usage:287```python288# Message containers289st.success("Operation completed successfully!")290st.info("This is informational content")291st.warning("Please review your input")292st.error("An error occurred")293294# Exception display295try:296risky_operation()297except Exception as e:298st.exception(e)299300# Toast notification301st.toast("File saved!", icon="✅")302```303304### Animation and Visual Effects305306Functions for adding visual flair and celebratory effects.307308```python { .api }309def balloons():310"""Display floating balloons animation for celebration."""311312def snow():313"""Display falling snow animation for celebration."""314```315316Example usage:317```python318# Celebratory animations319if user_completed_task:320st.balloons() # Show balloons321322if holiday_theme:323st.snow() # Show snow effect324```325326### Layout Patterns327328Common layout patterns and best practices:329330#### Multi-Column Layouts331```python332# Equal columns333col1, col2, col3 = st.columns(3)334335# Custom column widths336col1, col2 = st.columns([3, 1]) # 3:1 ratio337338# With gap control339col1, col2 = st.columns(2, gap="large")340```341342#### Nested Containers343```python344# Container within container345with st.container(border=True):346col1, col2 = st.columns(2)347with col1:348with st.expander("Details"):349st.write("Nested content")350```351352#### Sidebar Layout353```python354# Sidebar with main content355with st.sidebar:356page = st.selectbox("Navigate:", ["Home", "Data", "Settings"])357358# Main content area359if page == "Home":360st.title("Welcome")361elif page == "Data":362st.dataframe(data)363```364365#### Form Layout366```python367# Form with organized layout368with st.form("user_form"):369col1, col2 = st.columns(2)370with col1:371name = st.text_input("Name")372email = st.text_input("Email")373with col2:374age = st.number_input("Age", min_value=0)375country = st.selectbox("Country", countries)376377submitted = st.form_submit_button("Submit")378```379380#### Tab Organization381```python382# Tabbed interface383tab1, tab2, tab3 = st.tabs(["Overview", "Details", "Settings"])384385with tab1:386st.metric("Users", 1234)387st.line_chart(overview_data)388389with tab2:390st.dataframe(detailed_data)391392with tab3:393settings = st.form("settings_form")394```