File_uploader vanishing

Hello All,

I am trying to use file_uploader method of streamlit inside an if condition. Basically, if login details are verified, a file_uploader option needs to arise which is working well, the main problem is after I upload a file, the file_uploader widget is vanishing. Surprisingly, this happens only when I am inside an if condition. When using file:_uploader, it works normally.

Please let me know if u have a solution for this.

Hi @Harish_Kodarapu,
How are you handling the login ? I think due to script re-run the login might be resetting to False resulting in vanishing file_uploader,
Is your implementation something similar to this ?

import streamlit as st

def check_login(username):
    return username == "ash2shukla"

def main():
    file = st.file_uploader("test")
    if file:
        st.write(file.read())

username = st.text_input("enter username")
if check_login(username):
    main()

It works fine on my end with the above snipped.

1 Like

Hi, My code is the following

option = st.selectbox(‘Select One’, [‘None’, ‘Test label originality’, ‘Train with new label’], index = 0)

ph1 = st.empty()

ph2 = st.empty()

ph3 = st.empty()

if option != ‘None’:

username = ph1.text_input('username')

password = ph2.text_input('password', type= 'password')

if ph3.button('Click'):

    if username == 'u' and password == 'u':

        ph3.success('success')

        time.sleep(0.5)

        ph1.empty()

        ph2.empty()

        ph3.empty()

        time.sleep(0.5)

        fil = ph1.file_uploader('Upload')

When the file uploader pops up, it vanishes automtically.

Basically, I want to remove username and password text_input widgets when the credentials are correct and then display file_uploader.

They are working correctly but after the file has been uploaded, the username and password widgets are reappearing over the file uploader.

@Harish_Kodarapu You dont really need the button there. Its “clicked state” gets reset on re-run.
Try running the script without it and it will run just fine :slight_smile:

Ps. you can see my above snippet works, but if I changed it to this,

import streamlit as st

def check_login(username):
    return username == "ash2shukla"

def main():
    file = st.file_uploader("test")
    if file:
        st.write(file.read())

username = st.text_input("enter username")
if st.button("Click me"):
    if check_login(username):
        main()

It will start showing the behavior you just mentioned,

@ash2shukla Thankyou for the answer.
One more request is, I want my username text_input also to go away after the credentials being matched.

This should take care of it :slight_smile:

import streamlit as st

def check_login(username):
    return username == "ash2shukla"

def main():
    file = st.file_uploader("test")
    if file:
        st.write(file.read())

username_placeholder = st.empty()

username = username_placeholder.text_input("Username goes here")
if check_login(username):
    username_placeholder.empty()
    main()

Just empty the element. It will look like this,

thankyou so much. I was also trying similar kind of technique using empty(), but somehow that didnt work. Thankyou so much.