Saksham
Saksham

Reputation: 65

Second File Uploader getting overwritten by the first File Uploader in Streamlit

I am using two file uploaders inside the same function. The first file uploader works well , however, the second file uploader gets overwritten by the first.

def apps():
    text = None
    passport = st.file_uploader("Upload Passport", type=["png", "jpg"], key="passport")
    if passport:
        button = st.button("Print")
        if button:
            text = st.write(passport.name)
            if text:
                st.success(text)
                verify_button = st.sidebar.button("Verify Face")
                if verify_button:
                    photo = st.sidebar.file_uploader("UPLOAD PHOTO", type=["png", "jpg"], key="photo")

The second file uploader does not get activated on clicking verify_button. The first file uploader shows on the screen again.

Upvotes: 0

Views: 622

Answers (1)

ferdy
ferdy

Reputation: 5014

When you put a button and the user presses it, streamlit will rerun the code from top to bottom - meaning it will rerun the app(). So the solution is not to add a button inside the app()

Example:

Code

import streamlit as st 


def apps():
    passport = st.file_uploader("Upload Passport", type=["png", "jpg"], key="passport")
    if passport:
        st.image(passport)

        # button = st.button("Print")
        # if button:
            # st.write(passport.name)

        # verify_button = st.sidebar.button("Verify Face")
        # if verify_button:

        st.sidebar.write('Verify Face')
        photo = st.sidebar.file_uploader("UPLOAD PHOTO", type=["png", "jpg"], key="photo")
        if photo:
            st.sidebar.image(photo)

apps()

Output

enter image description here

Upvotes: 2

Related Questions