question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

context not consistently transfered to dash app

See original GitHub issue

Hi,

I have an issue where the context defined in the Django view is not consistently transferred to the Dash app. I am using Dash 2.0.0, Django 3.2.7, Django-plotly-dash 1.6.5. I was made aware of the version compatibility issue as per e.g. #353, which I have also experienced.

I got the basics working to my satisfaction using #364 and other resources.

In views.py I define the context as: dash_context = {"dash_context":{"my_key":{"value":str(my_value)}}} (my_value is obtained from Django and shows the value consistently and correctly populated.)

And then the page is rendered using: return render(request, 'page_1/page_1.html', context=dash_context)

When running the app a print statement in the view shows that the context is correctly constructed:

my-page Context: {‘dash_context’:{‘my_key’:{‘value’:‘somevalue’}}}

Checking the rest of the Django session attributes indicate that all is populated as expected.

The HTML template contains the following:

{% block content %}
        {% load plotly_dash %}
        <div class="{% plotly_class name='MyGraphs' %} card" style="height:100%; width:100%">
                {% plotly_app name='MyGraphs' ratio=0.65 initial_arguments=dash_context %}
        </div>
{% endblock %}

Looking at my callback function that is initially called I can see that the value in the context is correctly passed and it triggers the required callbacks to be executed.

My callback functions use the my_key value as an input to the callbacks. e.g.:

@app.callback(
    Output('id_overview_graph', 'figure'),
    [
        Input('period_selection', 'value'),
        Input('type_selection','value'),
        Input("my_key", "value")
    ]
)
def update_overview_graph(period, metricType, my_key,**kwargs):
    # The rest of the function

Inspecting the get command in the browser looks as follows:

http://172.31.11.19:8888/django_plotly_dash/app/MyGraphs/initial/dpd-initial-args-af9f58d8224b4e5ab959ec0dfda9a015/

On the HTML page I can see the graphs and also a hidden tag correctly populated:

This part of getting the values through the context does not work consistently and reliably. Symptoms seen:

  1. Sometimes the context is not passed or the value received in the Dash app and callback functions are empty.
    1. In the cases where the value is empty the callback is still triggered. This is indicated by some logs inside the callback function and the graph titles being correctly updated. Verified with a callback function only triggered by the context key received.
  2. Sometimes reloading the page once results in the context values being passed correctly.
  3. Sometimes if I reload the page on the browser multiple times, the context is correctly passed. There is no fixed pattern to this. Sometimes it works the first time the page is loaded. Sometimes it only works after ten consecutive page downloads.
  4. It also does not help to go to another page and then return to the page with the empty context.

The session_key authenticated user and other Django session related arguments stay the same, irrespective if the context is passed correctly or not.

Any thoughts on why this behaviour is experienced? Is it a Dash v2 compatibility related issue? (Not in a position to test it with v1.21.0 at the very moment)

Thank you for your help.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
GibbsConsultingcommented, Mar 28, 2022

@kotnikd3 one approach might be to use state within the application. In other words, instead of setting the initial arguments in the template call, create an instance of the DashApp model. This will essentially use the backend to share app state, and you’ll need some way to work out which instance to use (eg per user or similar).

If the issue is intermittent, it does sound like a cache miss or similar. Running with DEBUG=True could also be contributing to this as it may imply an ordering of calls in a given execution environment.

1reaction
ex3t3rcommented, Feb 1, 2022

@lBulgur

Try putting 'cache_arguments': False into your django settings.py file.

Have a look at Configuration options on how to add it.

I inserted that into my settings.py file and found an immediate improvement in the reliability of the initial arguments being passed through. Still testing but looking promising.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem accessing initial arguments from Dash app · Issue #364
My Dash app, as I understand it, should now be able to have the div with id ... context not consistently transfered to...
Read more >
Django plotly dash context not passing data always
Now i'm having a problem where sometimes the context doesnt load in my dash app but other times it will, it feels like...
Read more >
Make your Dash App Faster with Clientside Callbacks
We'll learn to use the Dash Clientside callback to speed things up and expand access to app interactivity. View my book - The...
Read more >
Dasher direct down - Vesuvio
The app is not giving you orders: Fast Pay gives Dashers the ability to cash out their earnings for a small fee of...
Read more >
Square Dashboard Troubleshooting
Outdated information or information not displaying correctly. Seeing a blank dashboard. Browser Troubleshooting. For browser issues, try the following ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found