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.

How to access template initial_arguments from dash app?

See original GitHub issue

For example I have the template.html:

{% extends "dashboards/base_generic.html" %} {% block content %} {% load plotly_dash %} {% plotly_app name="descriptive" ratio=0.75 initial_arguments='{"vehicle_data":{"make":"{{Make}}","model":"{{Model}}","year":"{{Year}}"}}' %} {% endblock %}

And in dash_app.py I have: app = DjangoDash(name='descriptive', id='vehicle_data')

Kind of confused what method I would use to access ‘vehicle_data’? Hopefully this is the right usage, new to Django and Dash.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
jfrostburkecommented, Dec 7, 2019

Result from me random-walking to the solution. For context, I was trying to access the id of a target in my database in an app that plots astronomical spectra.

Views.py

My views.py was already returning a context about, so I just added to it. This set the layers to unpack (dash_context, target_id, value):

context['dash_context'] = {'target_id': {'value': self.get_object().id}}

template.html

In my template I had to pass that new context to the app:

{% plotly_app name="Spectra" ratio=1.0 initial_arguments=dash_context %}

dash_app.py

I did this when I defined the app:

app = DjangoDash(name='Spectra', id='target_id')

When I defined the layout of the app I passed the target id in as a hidden input. The filler text got immediately overwritten so it’s not the most elegant:

app.layout = html.Div([
    . . .
    dcc.Input(id='target_id', type='hidden', value='filler text'),
    . . .

Then I had to pass in the value (the end goal that I wanted to access) in the callback:

 @app.expanded_callback(
    . . .
    Input('target_id', 'value')])
def display_output(. . . value):
    print(value)

Hopefully this is helpful to anyone trying to solve this in the future.

0reactions
pperum002ccommented, Jul 28, 2021

@nikhilnaregal - I also have similar requirement and I wasn’t able to find any solution for passing input from Django to Dash. However I found a workaround - that suits my requirement. Please check if its useful in your case.

views.py

from django.shortcuts import render

# Create your views here.

def home(request):
    return render(request, 'entry_ui_portion/home.html')

home.html

{%load plotly_dash%}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Release Plan</title>
</head>

<body>
    {%plotly_app name="ReleasePlanning" ratio=0.75%}
</body>

</html>

Now In the actual dash layout file - call django model and iterate thru the query - DOING JUST THIS WILL NOT FIX THE ISSUE (Instead - In the dash layout file use live updates with this method: https://dash.plotly.com/live-updates

example_dash_file.py

from <app_name>.models import <modelname>
other imports....
...

app = DjangoDash(name='ReleasePlanning', suppress_callback_exceptions=True)

def getContent():
    datadf = []
    for rows in <modelname>.objects.all():
        da = {}
        da['fname'] = rows.fname
        da['lname'] = rows.lname
        da['experience'] = float(rows.experience)
        da['domain'] = rows.domain

        datadf.append(da)
    return datadf

def serve_layout():

    resultdf = getContent()

    return html.Div(children=[
        html.Div(children=[
            html.H1('Heading')
        ], style={'font-family': 'Verdana', 'text-align': 'center', 'padding-down': '20px'}),

app.layout = serve_layout

#callbacks below if any - if datadf used in any callback - call getContent again

This may not be an effective solution, but it served the purpose of what a data feed from django to dash does is cleared in this method.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I access initial arguments within Django dash?
It seems I was able to send intial_arguments at /django_plotly_dash/app/SimpleExample/initial/dpd-initial-args-f51525f2777a46f4…
Read more >
Send initial arguments from a Django view to Dash and read it ...
I have an scatter plot on Dash. This scatterplot takes the values from SQL depending on the input parameter id . After querying...
Read more >
Template tags — django-plotly-dash documentation
This tag inserts a DjangoDash app within a page as a responsive iframe element. The tag arguments are: ... At least one of...
Read more >
django-plotly-dash Documentation - Read the Docs
Dash application are mapped to Django ones, and an application is embedded into a webpage through the use of a template tag.
Read more >
How to Create a Multipage Dash App | by Michael McManus
Now that all the required packages are installed, our first step is to create a Python file call app.py . This file will...
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