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.