Linked Questions

Popular Questions

Differences between global, local and import variables

Asked by At

Accessing global and import variables in a def update function in dash (developed by plotly). This seems to be specific to def_update callback functions in dash.

I need to declare global variable as global before I am able to use it in the update function. However I dont need to do it for the other_var variable that I imported. Why do I need to do this?

My code is like this:

from other_script import other_var
var = 0
@app.callback(Output(id, children), Input(id, value))
def update_graph(value):
    global var
    return var*value*other_var

EDIT: If I dont have global, it shows this error: "unresolved reference..."

UPDATE: So I read the Dash docs, and apparently it says "Dash Callbacks must never modify variables outside of their scope. It is not safe to modify any global variables. " Ref:

I guess what I was doing was just bad practice as I was trying to modify the global variable var

Related Questions