Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can use ctx in django plotly dash callbacks? #467

Open
davood-hadiannejad opened this issue Jul 17, 2023 · 2 comments
Open

How can use ctx in django plotly dash callbacks? #467

davood-hadiannejad opened this issue Jul 17, 2023 · 2 comments

Comments

@davood-hadiannejad
Copy link

I am trying to distinguish which component hast triggered in callbacks to determine a certain action to perform.

 def _register_app2_callbacks(self):
        @self.app2.callback(
            [
                Output("sidebar-content", "children"),
                Output("network-graph-chart", "children"),
                Output("graph-title-text", "children"),
                Output("word-cloud-title-text", "children"),
                Output("top-10-urls-title-text", "children"),
                Output("word-cloud-graph", "children"),
                Output("top-10-urls-graph", "children"),
                Output("search-error-message", "children"),
                Output("ls-loading-output-top2vec", "children"),
            ],
            [
                Input("sidebar-graph", "clickData"),
                Input("search-top2vec-input", "value"),
            ],
        )
        def update_charts_word_graph_app2(click_data, search_value):
            return self._update_charts_word_graph(click_data, search_value)  # Return all outputs

def _update_charts_word_graph(self, click_data=None, search_value=None):
    #if click_data is not None:
    if ctx.triggered_id == 'sidebar-graph' and click_data is not None:
        click_components = self._process_click_data(object_graph, click_data)
        return click_components

    # if search_value is not None and search_value.strip() != "":
    if ctx.triggered_id == 'search-top2vec-input' and search_value.strip() != "":
        search_components = self._process_search_keywords(object_graph, search_value)
        return search_components

but I am getting the following error:

File "/usr/local/lib/python3.9/site-packages/dash/_callback_context.py", line 19, in assert_context   if not context_value.get():
LookupError: <ContextVar name='callback_context' at 0x7f318dffa5e0>

Is there a way to use the ctx in django plotly dash?
or a sulotion to perform different actions based on the click event ( if click on the bar chart) and the search input (if a search is performed). how can I ensure that the search functionality works even if the click_data is not None

@delsim
Copy link
Contributor

delsim commented Jul 20, 2023

@davood-hadiannejad the global ctx variable is not encouraged. In general global variables are not a safe design approach.

Rather, django-plotly-dash takes the information that would otherwise be in this global and provides it as an additional 'callback_context` argument to each extended callback. See the extended callback documentation for further information.

It is possible that the structure and content of this data is not the same as that of the dash global variable; if so please do report any differences.

Finally, note that the same context information is available in the dash.callback_context global variable, but its use is not encouraged. In particular there is nothing in place to ensure that this global variable has the correct content when multiple threads or async IO are in use.

@delsim
Copy link
Contributor

delsim commented Jul 20, 2023

From the dash documentation it looks like dash 2.4 added some additional variables to the callback that are not populated (yet) when running through dpd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants