question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

File_uploader() - ValueError: I/O operation on closed file.

See original GitHub issue

Summary

I upgraded Streamlit and file_uploader() haven’t the expected behavior. I’m trying to load a file, then use a dropdown menu to select the right column to analyse. I adapted the proposed solution here , but I still have “ValueError: I/O operation on closed file.” when I select a value in the dropdown menu.

Steps to reproduce

What are the steps we should take to reproduce the bug:

  1. Select a file to upload
  2. Select a value in the dropdown menu

Screencast

Expected behavior:

Explain what you expect to happen when you go through the steps above, assuming there were no bugs.

Is this a regression?

That is, did this use to work the way you expected in the past? yes

Debug info

streamlit==0.70.0 streamlit-nightly==0.70.1.dev20201101

Additional information

Here is my code :

def upload_file():
    file = st.sidebar.file_uploader("Choisir un fichier", type=["csv"])
    if file is None:
        return None,None,None,None,None,None
    file.seek(0)
    df = pd.read_csv(file, sep=';',index_col=None, encoding = "utf-8")
    menu_value = st.sidebar.selectbox('Quelle colonne correspond au texte à analyser ?',df.columns)
    language,remove_hashtags,remove_usernames,ncomponents=sidebar()
    return(df,menu_value,language,remove_hashtags,remove_usernames,ncomponents)

def sidebar():
    language = st.sidebar.selectbox('Quelle est la langue du corpus ?',['french','english'])
    remove_hashtags=st.sidebar.checkbox('Supprimer les hashtags',value=False)
    remove_usernames=st.sidebar.checkbox('Supprimer les mentions',value=False)
    ncomponents=st.sidebar.slider("Combien de topics à détecter", min_value=0, max_value=100, value=20, step=1, format=None, key=None)
    return language,remove_hashtags,remove_usernames,ncomponents
    
def main():
    st.set_page_config(
        page_title="Topic Modeling",
        page_icon="🧊",
        layout="wide",
        initial_sidebar_state="expanded",
    )
    st.set_option('deprecation.showfileUploaderEncoding', False)

    st.sidebar.title('Paramètres')
    st.title('Topic Modelling')
    projet=st.sidebar.text_input("Donner un nom de projet", value='monProjet', max_chars=None, key=None, type='default')
    
    
    df,menu_value,language,remove_hashtags,remove_usernames,ncomponents=upload_file()
   
    if st.sidebar.button('Valider'):
        analyse(projet,language,remove_hashtags,remove_usernames,ncomponents,df_input,menu_value)
        list_results(path="./"+projet+"/")

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
karriebearcommented, Dec 16, 2020

Thanks for sending this along. The fix was recently merged and will be available in version 0.73 that will be coming out this week. If you would like to get the latest, you can use our streamlit-nightly package that is updated with the latest changes every day.

0reactions
mmcgufficommented, Dec 16, 2020

I think this is the best thread to leave this – I am encountering this error when using the file_uploader in combination with st.radio(), and uploaded_file.seek(0) does not solve the problem. Ill admit, I honestly do not understand what seek(0) is fundamentally doing, so maybe I am missing something obvious. Here is a minimal reproducible example:

import streamlit as st
import io

option = st.radio(
    'Choose method of submitting sequence:',
    ["Upload a file (.txt)", "Enter a sequence"])

txt = None

if option == "Upload a file (.txt)":

    uploaded_file = st.file_uploader("Choose a file:", type=['txt'])

    if uploaded_file is not None:
        uploaded_file.seek(0) #does not do anything
        text_io = io.TextIOWrapper(uploaded_file,encoding='UTF-8')
        txt=list(text_io.readlines())

elif option == "Enter a sequence":
    txt = st.text_area('Input sequence here:')

if txt:
    st.write(txt)

streamlit 0.71.0 and 0.72.0

When users upload a file, switch to manually entering text, and then try to upload again, the ValueError: I/O operation on closed file is thrown. This is a regression, as I did not encounter this error with a much older version of streamlit

any help would be much appreciated!

Read more comments on GitHub >

github_iconTop Results From Across the Web

ValueError : I/O operation on closed file - Stack Overflow
When I try to write to the file it reports the error: ValueError: I/O operation on closed file. python · csv · file-io...
Read more >
ValueError: I/O operation on closed file in Python | bobbyhadz
The Python ValueError: I/O operation on closed file occurs when we try to perform an operation on a closed file. To solve the...
Read more >
V0.71.0 - file_uploader : I/O operation on closed file - Streamlit
I've been getting this error from file_uploader when I adjust a slider or do anything that reruns the app. I saw posts talking...
Read more >
ValueError: I/O operations on closed file - STechies
ValueError : I/O operations on closed file. · Example: # Creating a file 'MyFile' f = open("MyFile",'w') # Entering data after opening the...
Read more >
It says "ValueError: I/O operation on closed file" but i opened it
import sys print('some text') a='moretext.1'.split('.') output = open('output.txt', 'w') print(a, file=output) output.close() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found