Deleting uploader widget when using st.file_uploader after picture is uploaded

Hi,
we are building an app where the user can upload an image and via API get a caption returned. Now the challenge is: that we would like to delete the uploader widget after the image is uploaded for an improved UI (see what we would like to delete in the
image below. We are currently hosting it locally.

Our code:

File uploader widget

uploaded_file = st.file_uploader(“Choose an image…”, type=[“jpg”, “jpeg”, “png”])

Check if a file is uploaded

if uploaded_file is not None:
# Display the uploaded image
st.image(uploaded_file, use_column_width=True)

It is not neat indeed. One approach without deleting the upload widget and still looks nice is to put it under the expander widget.

1 Like

To not show the uploader widget, you can create session variables to control it, and be sure to save the image as well.

import streamlit as st
from streamlit import session_state as ss


if 'show_uploader' not in ss:
    ss['show_uploader'] = True

if 'image' not in ss:
    ss['image'] = None


if ss['show_uploader']:
    uploaded_file = st.file_uploader('Choose an image…', type=['jpg', 'jpeg', 'png'])
    if uploaded_file is not None:
        ss['image'] = uploaded_file  # backup the file
        ss['show_uploader'] = False
        st.rerun()

if ss['image'] is not None:
    st.image(ss['image'], use_column_width=True)
3 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.