How to retain changes across app modes in a streamlit program?

Viewed 194

Here's the code:

def main():
    st.title("Image Restoration")

    #define states of the app
    colorizationPage = "Image Colorization"
    inpaintingPage = "Image Inpainting"
    enhancementPage = "Image Enhacement"
    denoisingPage = "Image Denoising"

    appMode = st.sidebar.selectbox(
            "Choose the app mode",
            [
             colorizationPage,
             inpaintingPage,
             enhancementPage,
             denoisingPage
             ])

    st.header(appMode)
    
    # prompt for uploading an image
    imgFile = st.file_uploader("Choose an image file")
    if imgFile is None:
        st.text("You haven't uploaded an image file")
    else:
        image = Image.open(imgFile)

        # run the python script corresponding to appMode

        # image colorization
        if appMode == colorizationPage:
            original, colorized = st.columns(2)
            # get the colorized image
            result = colorize.colorize(image)
            #display the images side by side
            original.header("Original")
            original.image(image, use_column_width = True)
            colorized.header("Colorized")
            colorized.image(result, use_column_width = True)

        # image enhancement
        if appMode == enhancementPage:
            original, enhanced = st.columns(2)
            # get the enhanced image
            res = night_images.enhance(image)
            # display the images
            original.header("Original Image")
            original.image(image, use_column_width = True)

            enhanced.header("Denoised Image")
            enhanced.image(res, use_column_width = True)

When the app mode is changed, the changes done to the image are reversed even if I reassign the result to the image itself. How would I retain the changes made ?

I tried changing the prompt where the image is uploaded to check if an image has already been uploaded to not re-upload it but that also doesn't seem to work.

1 Answers

You should use the session state to save this type of information - https://docs.streamlit.io/library/api-reference/session-state

You can think of it as a dictionary that is not lost on page reload.

For your case writing something like

 appMode = st.sidebar.selectbox(
            "Choose the app mode",
            [
             colorizationPage,
             inpaintingPage,
             enhancementPage,
             denoisingPage
             ])

# New code
st.session_state['app_mode'] = appMode
# End 

should fix your problem.

To load this every time I would also save the information in a list and get the index.

app_modes = [
    colorizationPage,
    inpaintingPage,
    enhancementPage,
    denoisingPage
]

selected_app_mode = st.session_state.get('app_mode')

app_mode_ix = 0
if selected_app_mode: 
    app_mode_ix = app_modes.index(selected_app_mode)

appMode = st.sidebar.selectbox(app_modes, index=app_mode_ix)

st.session_state['app_mode'] = appMode
Related