GibbsConsulting / django-plotly-dash

Expose plotly dash apps as django tags
MIT License
560 stars 125 forks source link

returning data from plotly to django #497

Closed tirohia closed 1 month ago

tirohia commented 7 months ago

A question if I may.

I'm relatively new to Django, I have used plotly a fair bit, and dash a little. After a bit of faffing about I've managed to get a plotly sunburst plot displaying in my django app.

I can't figure out how to return data from the sunburst plot to django though. The end goal is to use the sunburst plot as a navigation tool - click on a category in the sunburst plot, have it return an id to the django application and have the django app load a new set of data in the page.

I'm importing plotly_app.py in my views.py. In plotly_app.py, I have this :

import pandas as pd
import pickle
from hc.diseaseTree import DiseaseTree
from hc.models import CancerType, Sample
import dash
from dash import dcc, html
import plotly.graph_objs as go
import plotly.express as px
from django_plotly_dash import DjangoDash

<code to generate the data dataframe for the sunburst>

sunburst = px.sunburst(
    data,
    names='child',
    parents='parent',
    values='sampleCount',
    maxdepth=3,
    hover_data='cancerId',
)

app = DjangoDash('NavTest')

app.layout = html.Div([
    dcc.Graph(figure = sunburst),
])

My index template has this:

{% block body %}
    {%load plotly_dash%}
    <div class="{% plotly_class name='NavTest': %}"> 
    {%plotly_app name="NavTest"%}
{% endblock %}

All of this renders the the sunburst nicely (mostly, the iframe it displays in is very small, but that's a different problem). I've been looking through the documentation (sufficiently new a this that that's fairly opaque) and the demos. Demo 2 show's how to set an initial state and pass it from django to the dash app. Demo four is, I think, passing data back and forth between plotly apps? Reading the section on live updates, I get the impression that the Pipes/Channels are used for passing data from django to the dash app or for passing data between the dash apps. Demo 8 is again, passing data from django to the dash app.

Is it possible/how do I pass a variable back from the dash app to django? In my hover_data in the sunburst plot, I have a variable called cancerId. The behavior I am looking for is to hit the relevant section and have django load a new set of data on patients with the cancer type associated with that cancerId. In theory that new set of data would also be associated with some new plots for which I can make new apps with django_plotly_dash and pass an id between them as shown in the demos.

I am assuming this is possible. It might not be and maybe I should be looking at another solution? Any pointers (to how to do this or other solutions) would be much appreciated.

Cheers Ben.

GibbsConsulting commented 6 months ago

@tirohia the easiest way is to do the work during a callback. The callback is essentially wrapped up and executed inside a Django view, and the context is available in an extended callback