Home > Back-end >  Why called function always appear on top instead of below
Why called function always appear on top instead of below

Time:09-27

So I am trying to call a streamlit upload button when a function call is triggered in st.button. But the upload button always get called on top of the button instead of below it. Any one have idea how to achieve it. Would help alot, Thnx!

import streamlit as st


def proc():
    st.file_uploader('')


if __name__ == '__main__':
    option = st.selectbox(
        'Which folder would you like to upload to?',
        ('1 docx-raw', '2 docx-text-only'), on_change=proc)

    st.write('You selected:', option)

Here, i need the upload button below it after triggering it's function screenshot

CodePudding user response:

I have a workaround..By introducing one more option.

import streamlit as st
def proc():
    st.file_uploader('')
    
option = st.selectbox('Which folder would you like to upload to?',
                      ('','1 docx-raw', '2 docx-text-only'),format_func=lambda x: 'Select an option' if x == '' else x)
if option=='2 docx-text-only':
    proc()
    st.write('You selected:', option)  
elif option=='1 docx-raw':
    proc()
    st.write('You selected:', option)     

    

This will give file uploader box below the select box. enter image description here

CodePudding user response:

Your question is not clear enough, but from my understanding, you want to do one of these two approches.
First approach:

import streamlit as st

def proc():
    upload_options = ['1 docx-raw', '2 docx-text-only']
    option = st.selectbox('Which folder would you like to upload to?', upload_options)
    st.write('You selected:', option)
    if option == upload_options[0]:
        upload_file = st.file_uploader('1 docx')
        if upload_file is not None:
            upload_button = st.button('Upload File', key="1 docx")
            if upload_button:
                # Do something with the file
                st.success("File Uploaded Successfully to 1 docx-raw")
    elif option == upload_options[1]:
        upload_file = st.file_uploader('2 docx')
        if upload_file is not None:
            upload_button = st.button('Upload File', key="2 docx")
            if upload_button:
                # Do something with the file
                st.success("File Uploaded Successfully to 2 docx-text-only")


def main():
    proc()


if __name__ == '__main__':
    main()

Output: enter image description here

Second approach:

import streamlit as st

def proc():
    upload_file = st.button('Upload File')
    if upload_file:
        st.file_uploader('')


def main():
    option = st.selectbox(
        'Which folder would you like to upload to?',
        ('1 docx-raw', '2 docx-text-only'))
    st.write('You selected:', option)
    proc()


if __name__ == '__main__':
    main()

Output:
Button not clicked enter image description here

Button clicked enter image description here

  • Related