Reputation: 375
I am using streamlit version v0.68 and currently working on CSV file for data analysis.
st.title('Report Analysis')
uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None:
data = pd.read_csv(uploaded_file, low_memory=False)
st.write(data.shape)
First it works, but if I rerun the program in my localhost it gives me the error:
EmptyDataError: No columns to parse from file
Traceback:
File "D:\My Programs\Projects\ReportAnalysis\venv\lib\site-packages\streamlit\script_runner.py", line 324, in _run_script
exec(code, module.__dict__)
File "D:\My Programs\Projects\ReportAnalysis\epl\app.py", line 9, in <module>
data = pd.read_csv(uploaded_file, low_memory=False)
File "D:\My Programs\Projects\ReportAnalysis\venv\lib\site-packages\pandas\io\parsers.py", line 686, in read_csv
return _read(filepath_or_buffer, kwds)
File "D:\My Programs\Projects\ReportAnalysis\venv\lib\site-packages\pandas\io\parsers.py", line 452, in _read
parser = TextFileReader(fp_or_buf, **kwds)
File "D:\My Programs\Projects\ReportAnalysis\venv\lib\site-packages\pandas\io\parsers.py", line 946, in __init__
self._make_engine(self.engine)
File "D:\My Programs\Projects\ReportAnalysis\venv\lib\site-packages\pandas\io\parsers.py", line 1178, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "D:\My Programs\Projects\ReportAnalysis\venv\lib\site-packages\pandas\io\parsers.py", line 2008, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas\_libs\parsers.pyx", line 540, in pandas._libs.parsers.TextReader.__cinit__
How to handle this error?
Upvotes: 8
Views: 5111
Reputation: 1730
For Streamlit 1.5.0, I tried all of the suggested solutions and none of them worked. The workaround I found was to write the content to a temporary file and then read from it:
uploaded_file = st.file_uploader("Choose a file", type=["csv", "txt"])
content = StringIO(uploaded_file.getvalue().decode("utf-8")).read()
temp_filepath = f"/tmp/{uuid4()}"
with open(temp_filepath, "w") as f:
f.write(content)
data = read_csv(temp_filepath)
Upvotes: 0
Reputation: 803
According to this post from the Streamlit community page, it is because they are returning the same buffer on the second time the app refreshes. Since pd.read_csv depletes the buffer, the second time you call read_csv will return no rows.
Adding a seek(0)
to reset the buffer worked for me.
e.g.
st.title('Report Analysis')
uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None:
uploaded_file.seek(0)
data = pd.read_csv(uploaded_file, low_memory=False)
st.write(data.shape)
Upvotes: 10
Reputation: 35
This will help you. Works with the current version 0.69.1.
global train_upload
train_upload = st.file_uploader("Upload csv data", type=['csv'])
if (train_upload is not None):
train_features = train_upload.read()
train_features = str(train_features,'utf-8')
train_features = StringIO(train_features)
train_features = pd.read_csv(train_features)
st.write(train_features)
Upvotes: 1
Reputation: 1604
This problem only occurs with the new version v0.68.1. As a work-around, you can always go back to an older version, e.g. 0.66, using:
pip install streamlit=0.66
Upvotes: 2