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.