Streamit session state example

PHOTO EMBED

Thu Jan 19 2023 20:14:15 GMT+0000 (Coordinated Universal Time)

Saved by @tofufu #python #streamlit

import streamlit as st

# Function to create a slider with an edit bar. The name of the slider must be unique on the page.
def createSliderWithEdit(container, initialValue, sliderName):
    val = initialValue

    # session_state is a global object, associated with streamlit, not with the container
    if sliderName in st.session_state:
        val = st.session_state[sliderName]
    nb = container.number_input(label=sliderName, value=val)

    # When you add a Key to the slider it will automatically create a session state associated with it.
    v = container.number_input(label='', value=nb, key=sliderName)
    # v = container.slider(label='', value=nb, key=sliderName)

createSliderWithEdit(st, 0, 'MyFavoriteInput')
createSliderWithEdit(st, 0, 'MySecondFavoriteInput')

col1, col2 = st.columns(2)

createSliderWithEdit(col1, 0, 'FirstInputInAColumn')
createSliderWithEdit(col2, 0, 'SecondInputInAColumn')
content_copyCOPY