Black Lives Matter. Please consider donating to Black Girls Code today.

UnBoundLocalError in documentation code of Upload component

I’m trying to run the first code section in the documentation of the Upload component:

Uploading to the running example on the page does nothing.

When I run the example locally and upload a image file, I get “UnboundLocalError: local variable ‘df’ referenced before assignment”.
The full trace is:

Traceback (most recent call last):
  File "/home/2024/sandbox/", line 86, in update_output
    children = [
  File "/home/2024/sandbox/", line 87, in <listcomp>
    parse_contents(c, n, d) for c, n, d in
  File "/home/2024/sandbox/", line 65, in parse_contents
UnboundLocalError: local variable 'df' referenced before assignment

Hi @2024
If “df” in this line of code “data=df.to_dict(‘records’)” is the first time you mention “df”, then the error is that you didnt define df outside the callback.

Python has lexical scoping by default, which means that although an enclosed scope can access values in its enclosing scope, it cannot modify them (unless they’re declared global with the global keyword). A closure binds values in the enclosing environment to names in the local environment. The local environment can then use the bound value, and even reassign that name to something else, but it can’t modify the binding in the enclosing environment. Real problem is that when python sees an assignment inside a function then it considers that variable as local variable and will not fetch its value from enclosing or global scope when we execute the function. To modify a global variable inside a function, you must use the global keyword ,otherwise resulting in an UnboundLocalError