How does one modify a dataframe with a callback? [former: circular import errors]

The problem with global variables is when multiple users are accessing your dashboard at the same time, and/or when you have multiple agents that can serve incoming requests. The reason is that a global variable is agent-specific and not user-specific. That means the following:

  1. if you have one agent, serving multiple users: changes in the global variable will be changes for all users currently accessing the dashboard
  2. if you have multiple agents, serving any number of users: if your first call, that changes the global variable, is handled by agent 1, that change is only available on agent 1. If you do a second call, and the load balancer of the server passes your request to agent 2 this time, you won’t have access to the changed value on agent 1. Secondly, you can have the issue of point 1, where changes to any of the global variables are applied for all users.

If you need to store user-specific data, the easy way is to use dcc.Store components, that store the data in the browser of the user. This data then needs to be passed along every callback to make it available in the server. Of course, this is not always feasible, for example when dealing with very large dataframes. In that case you will have to use other tricks. Examples of this are available via the pages linked by Ann-Marie.

Lastly, since Dash 2.17.0, there are callbacks with no outputs. This could be convenient if you have a use-case where you don’t want to change anything in your dashboard but need to change something behind the scenes. This comes with a disclaimer: to properly use these, it’s important to get a good grasp on how and where your server will run your tool and how many users you are expecting (see above).

2 Likes