How to upload and display contents of a file using ipywidgets?

I want to use upload as a button as well to display some message. I want to display the text file I have uploaded. The following is the code I have tried but the text file is not displayed.

import ipywidgets as widgets
from IPython.display import display
uploader = widgets.FileUpload(accept=‘.txt’, multiple=False)
display(uploader)
def on_upload_change(change):
for filename, file_info in uploader.value.items():
content = file_info[‘content’].decode(‘utf-8’) # Decode bytes to string
print(f"Filename: {filename}“)
print(f"Content:\n{content}”)
uploader.observe(on_upload_change, names=‘value’)

Please consider the Output widget.There are many examples on this forum for different tricks, try searching for widget output.

It must be displayed first, but then will update with anything put into it when used as a context manager.

from ipywidgets import Output

out = W.Output()
display(out)
with out:
  print("whatever")
1 Like

Bottom code block here under ’ Just give me a simple HELLO WORLD example for ipywidgets’ uses the output widget to show text entered in another ipywidgets widget. You can adapt that to tie that in to the upload process.


Also, please learn about posting code in formatted form. If this had been ‘good’ code or on the way to being so, others trying to help you would not be able to use it ‘as-is’. Remember, as much as you can to try and help those trying to help you.
I have suggested exactly this this before to you, see here.

Finally this code worked for me i am able to upload and display .csv file as dataframe.

import pandas as pd
import ipywidgets as widgets
from IPython.display import display, clear_output
upload = widgets.FileUpload(accept=‘.csv’, multiple=False)
out = widgets.Output()
def handle_upload(change):
out.clear_output()
with out:
if upload.value:
uploaded_file = upload.value[0]
content = uploaded_file[‘content’]
df = pd.read_csv(pd.io.common.BytesIO(content))
display(df)
upload.observe(handle_upload, names=‘value’)
display(widgets.VBox([upload, out]))

1 Like

Formatted in a way it is useable:

import pandas as pd
import ipywidgets as widgets
from IPython.display import display, clear_output
upload = widgets.FileUpload(accept='.csv', multiple=False)
out = widgets.Output()
def handle_upload(change):
    out.clear_output()
    with out:
        if upload.value:
            uploaded_file = upload.value[0]
            content = uploaded_file['content']
            df = pd.read_csv(pd.io.common.BytesIO(content))
            display(df)
upload.observe(handle_upload, names='value')
display(widgets.VBox([upload, out]))

See how to add as markdown (sort of; it would be better if the final ticks were on last line but hopefully helpful since this at least got most of the way there):

```python
import pandas as pd
import ipywidgets as widgets
from IPython.display import display, clear_output
upload = widgets.FileUpload(accept='.csv', multiple=False)
out = widgets.Output()
def handle_upload(change):
    out.clear_output()
    with out:
        if upload.value:
            uploaded_file = upload.value[0]
            content = uploaded_file['content']
            df = pd.read_csv(pd.io.common.BytesIO(content))
            display(df)
upload.observe(handle_upload, names='value')
display(widgets.VBox([upload, out]))```