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.