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.

Pass initial argument from django to django dash app

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
KuechlerOcommented, Nov 3, 2021

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.:

return render(request, self.template_name, {"normal_context": context_data,
                                                    "dash_context": {"dropdown1": {"value": value1, "label1": label1},
                                                                     "dropdown2": {"value": value2, "label": label2}
                                                                     }})

2.) Pass plotly-context to plotly-dash-app in your template:

                {% load plotly_dash %}
                {% plotly_app_bootstrap name="Example" initial_arguments=dash_context aspect="16by9" %}

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!

1reaction
KuechlerOcommented, Dec 11, 2021

@redtensor22

@KuechlerO could you give more code for your actual app? I have followed the exact logic but cant get the value to actually show up in the dash.

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"}} %}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Send initial arguments from a Django view to Dash and read it ...
If you are passing simple values around the application and not full data sets I'd recommend using something like Redis to store and...
Read more >
How can I access initial arguments within Django dash?
I have searched a lot but still there is no information on how to access initial arguments. ... but I can't access it...
Read more >
django-plotly-dash Documentation - Read the Docs
use_frameborder = “0” HTML element property of the iframe containing the application. initial_arguments = None Initial arguments overriding app ...
Read more >
Demonstration application — django-plotly-dash documentation
In passing, we note that accepting parameters as part of the URL and passing them as initial parameters to the app through the...
Read more >
django-admin and manage.py
django -admin check --database default --database other ... Any arguments following a -- divider will be passed on to the underlying command-line client....
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