i got a problem with the Fileupload Widget in Jupyter
import pandas as pd
import ipywidgets as widgets
from ipywidgets import FileUpload
from IPython.display import display
upload = FileUpload(accept='.xlsx', multiple=False)
def on_file_upload(change):
uploaded_file = list(change['new'].values())[0]
content = uploaded_file['content']
df = pd.read_excel(content)
display(df.head())
upload.observe(on_file_upload, names='value')
display(upload)
In uploaded_file = list(change['new'].values())[0]
i get the error
AttributeError: 'tuple' object has no attribute 'values'
Already tried everything and read the documentation but cant find a solution. Hope someone can help.
A table is in the excel and i got 2 sheets inside.
i got a problem with the Fileupload Widget in Jupyter
import pandas as pd
import ipywidgets as widgets
from ipywidgets import FileUpload
from IPython.display import display
upload = FileUpload(accept='.xlsx', multiple=False)
def on_file_upload(change):
uploaded_file = list(change['new'].values())[0]
content = uploaded_file['content']
df = pd.read_excel(content)
display(df.head())
upload.observe(on_file_upload, names='value')
display(upload)
In uploaded_file = list(change['new'].values())[0]
i get the error
AttributeError: 'tuple' object has no attribute 'values'
Already tried everything and read the documentation but cant find a solution. Hope someone can help.
A table is in the excel and i got 2 sheets inside.
Share Improve this question asked Feb 14 at 7:34 credencocredenco 2872 silver badges14 bronze badges2 Answers
Reset to default 0Are you sure that uploaded file get dictionary from which you are getting 'new' key and try to get the values() from it.
First of all check the type of "change" that is it dictionary or tuple, if yes it is dictionary then do the given below code otherwise it will show error every time.
uploaded_file = list(change['new'].values())[0]
With the help of this post i was able to make this.
import ipywidgets as widgets
import pandas as pd
from IPython.display import display
import io
file_upload = widgets.FileUpload()
output = widgets.Output()
df_uploaded = None
def on_upload_change(change):
global df_uploaded
with output:
output.clear_output()
file_contents = change.new[0].content
df_uploaded = read_excelfile(file_contents)
display(df_uploaded)
def read_excelfile(file_content):
df = pd.read_excel(io.BytesIO(file_content))
return df
file_upload.observe(on_upload_change, names='value')
display(file_upload)
display(output)
ChatGPT helped with the df_uploaded = None
. I dont understand why this is needed.