context not consistently transfered to dash app
See original GitHub issueHi,
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:
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:
- Sometimes the context is not passed or the value received in the Dash app and callback functions are empty.
- 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.
- Sometimes reloading the page once results in the context values being passed correctly.
- 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.
- 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:
- Created 2 years ago
- Comments:13 (4 by maintainers)
Top GitHub Comments
@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.@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.