File_uploader always returns None

Iā€™m using the following:

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.

ahhhh. Iā€™ll give it a try. Thanks!

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.

1 Like

@jefflariviere can u deploy your app in streamlit cloud (its free!) or share the repo, so that I can review your codebase.

I donā€™t see any thing wrong with the shared code, but would be more than happy to review your code

1 Like

Thx for your help, I have it working now, but I think there must be a better way to do what I want to doā€¦

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.

No goyo even better is to write the bytes directly into a temporary file.

I donā€™t think it is. Unless you need to call an interface that requires a file name, which fortunately doesnā€™t happen often.