Search code examples
python-3.xpandasdataframejupyter-notebookipywidgets

ipywidgets - widgets.FileUpload, updated CSV file read the CSV file


I am using jupyterhub and hosting the .ipynb file and hosted on server. I have usecase to upload a CSV from localdrive file and read the same for other dataframe tasks.

uploader = widgets.FileUpload(
    accept='*.csv',  # Accepted file extension e.g. '.txt', '.pdf', 'image/*', 'image/*,.pdf'
    multiple=False  # True to accept multiple files upload else False
)

display(uploader)


[input_file] = uploader.value
print(input_file)
pd.read_csv(input_file)

print(input_file) - is printing Test.csv which is CSV file name I am able to print [input_file] but `pd.read_csv(input_file)' is throwing below error

FileNotFoundError: [Errno 2] No such file or directory: 'Test.csv'

Not sure were the CSV is uploaded, how can i read that data. Please help.


Solution

  • I don't have your exact ipywidgets version, but can you try this:

    input_file = list(uploader.value.values())[0]
    content = input_file['content']
    content = io.StringIO(content.decode('utf-8'))
    df = pd.read_csv(content)