import streamlit as st
state = st.session_state
if ‘clicked’ not in state:
state.clicked = False
def set_clicked():
state.clicked = True
if st.button(“Load test file”, on_click=set_clicked)
if state.clicked:
uploaded_file = st.file_uploader(“select a test case file”, key = ‘uploaded_test_case_file’)
print(f"\nupload_file_name is {uploaded_file}"
For some reason, this always returns None from the file_uploader. Even though I press the browse key that appears and select a file.
If you want the file upload to dissapear after uploading the file, you need to keep the result in session_state. uploaded_file doesn’t exist in the next rerun.
Still not working and keeps returning None. Maybe I’m using file_uploader incorrectly. I’m only trying to identify a file (by browing for it), then will handle loading the file myself, knowing its name. Should I be using something other than “file_uploader”? Like, “file_selector” or something else? (if it exists)
That is certainly not the use case for file_uploader(), but it might work and I don’t have a better idea. If you post properly formatted code that I can run I will try to help you to fix it.
Note that web applications usually don’t have access to files in the client computer.
thx for your help I have it working now but I’m curious about the proper use-case for file_uploader. Maybe taking care of the loading of the file “manually” is not the best.
Basically I’m trying to populate the fields on my screen with data from a file, that I saved after previously filling out the screen (tedious).
The use case for the file uploader is loading files from the client’s computer to the application. That means the bytes stored in the file, not just the file name.
So there are a couple of issues with “will handle loading the file myself, knowing its name”:
In the more general case you cannot do that, because the file and the application are in different computers.
You already have the bytes in the file uploader, opening the file to read the bytes again is a waste of resources.
So, if for example, I used pickle to dump my data structure into a file, can I use file_uploader to read it? and store it into an identical data structure?
The file uploader will read the bytes in the file and will send them to the application. Then the application can use the pickle module to deserialize the data structure.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.