widgets.FileUpload() not working

its me again…I experienced with the same notebook on Jupyter (instead of JupyterLab) and the widgets work! lots of time wasted, I do not know why it was not working on JupyterLab but remembered having seem some comments in the notes somewhere that some functions did not work on JupyterLab.

I hope this helps others,

2 Likes

I solved this on my Ubuntu Desktop machine (20.04 version) by installing the supposedly missing NODE.JS …as well as the before-mentioned ipywidgets via pip3.

Node.js definitely fixed the problem for me …it did require a restart. Good luck!

here’s a solution that worked for me in GCP Jupyter Lab, from jeffbiss

I’ve suffered all day with this problem. Button “uploadfile” has not appeared. Fortunately, I’ve found an issue. In my case it depended on the place where you creat your notebook. If you make it in fastai template, “uploadfile” will work but only in pythonlab. Otherwise it doesn’t work.

For Colab users, I found this solution. If you can upload an image but the code after that did not work. try to put # in front of this line of code: #uploader = SimpleNamespace(data = [‘images/chapter1_cat_example.jpg’])
this will disable the local image from being uploaded, and it will responded to the image that you actually uploading.

1 Like

IDE: Jupyter Notebook in the browser

Fix: Increase the websocket_max_message_size in your jupyter notebook config file.

Option 1: jupyter_notebook_config.py

Uncomment the following line:
c.NotebookApp.tornado_settings = {"websocket_max_message_size": 1073741824}

or

Options 2: jupyter_notebook_config.json

{
  "NotebookApp": {
    "tornado_settings": {
      "websocket_max_message_size": 1073741824
    },
  }
}

Source: Document workarounds for file upload size limit #2522


IDE: Visual Studio Code

The iPython ipywidget upload button is a known issue. There are quite a few issue trackers on the subject:

  1. FileUpload ipywidget returns empty dict #8084
  2. Extension host terminated uexpectedly when using ipywidgets.Fileupload #4525
  3. File upload widget crashes extension host when using non-jupyter (zmq) #10525

Workaround: Pass in the image path and name.

img_path = './images/'
img_file_name = 'cat-image.png'
img = img_path + img_file_name
is_cat, _, probs = learn.predict(img)

Note: Try not to let this issue slow you down. When you are ready to build your own app and want an upload button, you can address this issue in different ways.

2 Likes

I tried this solution with Colab, works like a charm.

My experience: I was making a very noob mistake using Google Colab trying to complete the testing of the cat/dog recognizer. I kept getting errors trying to run these cells getting errors like “No such file or directory: ‘images/chapter1_cat_example.jpg’” and “list index out of range”. My mistake was that every time I uploaded the image to the upload cell, I kept running the cell thinking that that was the way to save it. Uploading the image but not running the cell and then running the third cell (skipping the second cell used for when the first cell does not work) is what fixed this problem for me.

Hey everyone, new student here.

The PILImage.create() function requires the image we’re trying to upload. From the docs I read

    >>> import ipywidgets as widgets
    >>> uploader = widgets.FileUpload()

    # After displaying `uploader` and uploading a file:

    >>> uploader.value
    [
      {
        'name': 'example.txt',
        'type': 'text/plain',
        'size': 36,
        'last_modified': datetime.datetime(2020, 1, 9, 15, 58, 43, 321000, tzinfo=datetime.timezone.utc),
        'content': <memory at 0x10c1b37c8>
      }
    ]
    >>> uploader.value[0].content.tobytes()

That’s what we need to pass to PILImage.create(). Here’s the final code

def on_click_classify(change):
    img = fastbook.PILImage.create(btn_upload.value[0].content.tobytes())
    out_pl.clear_output()
    with out_pl: display(img.to_thumb(128,128))
    pred,pred_idx,probs = learn.predict(img)
    lbl_pred.value = f"Prediction: {pred}; Probability: {probs[pred_idx]:.04f}"