Pass initial argument from django to django dash app
See original GitHub issueI want to pass a variable from django view to DjangoDash application. In details, I want to use the id that I receive from get method in Django view to the DjangoDash in order to make some plots with filtered data based on that id.
I followed this approach https://github.com/GibbsConsulting/django-plotly-dash/issues/173 which was very helpful, but still can’t have access of the variable in the DjangoDash.
views.py
def getID(request):
queryid= request.GET['queryid']
context = {'data':{"query_id":queryid}}
template_name="djangoDash.html"
return render(request, template_name, context)
In order to use the DjangoDash with initial arguments with the content data from the view.
djangoDash.html
.....
{% plotly_app name='djangoDash' ratio=0.45 initial_arguments=data %}
....
When I define the djangoDash app I use this
app = DjangoDash('djangoDash', id='query_id', serve_locally=False, suppress_callback_exceptions=True)
And I use this id as an input over my layout to use it for the queries with the bellow callback.
layout = html.Div([
dcc.Input(id='query_id'),
html.Div(id='result'),
])
@app.expanded_callback(dash.dependencies.Output('result', 'children'),
[dash.dependencies.Input('query_id', 'value')])
def display_output(query_id):
result= query_id
return result
The error I get when I load the page is
File "/home/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/.local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/.local/lib/python3.6/site-packages/django_plotly_dash/views.py", line 74, in update
return _update(request, ident, stateless, **kwargs)
File "/home/dimicham/.local/lib/python3.6/site-packages/django_plotly_dash/views.py", line 102, in _update
resp = app.dispatch_with_args(request_body, arg_map)
File "/home/.local/lib/python3.6/site-packages/django_plotly_dash/dash_wrapper.py", line 647, in dispatch_with_args
res = self.callback_map[target_id]['callback'](*args, **argMap)
File "/home/.local/lib/python3.6/site-packages/dash/dash.py", line 994, in add_context
output_value = func(*args, **kwargs) # %% callback invoked %%
TypeError: display_page() got an unexpected keyword argument 'dash_app_id'
HTTP POST /djangoDash/app/djangoDash/initial/dpd-initial-args-52e61040fc9b4edd9519db95eadaa6bc/_dash-update-component 500 [0.17, 160.40.70.30:49439]
File "/home/.local/lib/python3.6/site-packages/django_plotly_dash/dash_wrapper.py", line 647, in dispatch_with_args
res = self.callback_map[target_id]['callback'](*args, **argMap)
File "/home/.local/lib/python3.6/site-packages/dash/dash.py", line 994, in add_context
output_value = func(*args, **kwargs) # %% callback invoked %%
TypeError: display_page() got an unexpected keyword argument 'dash_app_id'
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (2 by maintainers)
Puh, took me some while to understand how to use the initial_arguments thingy, so here are my steps that made it work for me:
1.) When you render and pass a context with Django, then also create a dash_context, e.g.:
2.) Pass plotly-context to plotly-dash-app in your template:
3.) The corresponding dash-elements with ids “dropdown1” and “dropdown2” are going to receive the given values as initial values. Make sure that a “value”-attribute and maybe also a “persistence”-attribute is included, otherwise it might be buggy.
Example element:
dcc.Input(id="dropdown2", persistence=False, value="test")
4.) Enjoy!
@redtensor22
I think you have an exta apostrophe in your code, which is causing the error.
Instead of
{%plotly_app name="tutorial_1" initial_arguments='{"target_id": {"value": "67"}}' %}
try:
{%plotly_app name="tutorial_1" initial_arguments= {"target_id": {"value": "67"}} %}