GibbsConsulting / django-plotly-dash

Expose plotly dash apps as django tags
MIT License
548 stars 124 forks source link

Passing Django request.user state into Dash app #128

Closed robd003 closed 5 years ago

robd003 commented 5 years ago

I'm wondering if anyone has advice on passing in request.user to the Dash app

Each of my logged in users has an integer associating that user with a specific data set and I'd like to limit each users visibility to viewing their own data only.

GibbsConsulting commented 5 years ago

Have you looked at the session example in the documentation?

Its an attempt to walkthrough using information from a Django user session and I think you should be able to to build on it (for example, look in 'request.user' during the initial view) to get information from your user into the Dash app state.

robd003 commented 5 years ago

Just curious, if I were to pass something like request.user in would all the keys & values show up as plaintext in the javascript / JSON thats passed to the browser?

GibbsConsulting commented 5 years ago

@robd003 if you're using the django-plotly-dash extended callback, you shouldn't; the session information is provided on the server side as part of the extended callback arguments, and not sent to the client. Of course, depending what you do with it in the callback, some of the information might turn up in the browser.

Debug settings would be something to look out for, also.,

robd003 commented 5 years ago

Great! Thanks for the info and I really appreciate the quick response

sorenwacker commented 2 years ago

@robd003 Did you get this to work?

KuechlerO commented 2 years ago

@robd003 Yeah, same question here: Did you get it to work?

I took a look at Extended Callbacks and really liked it. However, I cannot make it run.

I copied the first example of the given website and tried to make it run, but failed so far.

Error

Internal Server Error: /django_plotly_dash/app/Ex2/_dash-update-component
Traceback (most recent call last):
  File "/Users/oliverkuchler/Studium/Apollo/aging-database/Django-files/apollo_pgsql_db/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/oliverkuchler/Studium/Apollo/aging-database/Django-files/apollo_pgsql_db/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/oliverkuchler/Studium/Apollo/aging-database/Django-files/apollo_pgsql_db/venv/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/oliverkuchler/Studium/Apollo/aging-database/Django-files/apollo_pgsql_db/venv/lib/python3.7/site-packages/django_plotly_dash/views.py", line 74, in update
    return _update(request, ident, stateless, **kwargs)
  File "/Users/oliverkuchler/Studium/Apollo/aging-database/Django-files/apollo_pgsql_db/venv/lib/python3.7/site-packages/django_plotly_dash/views.py", line 91, in _update
    resp = app.dispatch_with_args(request_body, arg_map)
  File "/Users/oliverkuchler/Studium/Apollo/aging-database/Django-files/apollo_pgsql_db/venv/lib/python3.7/site-packages/django_plotly_dash/dash_wrapper.py", line 700, in dispatch_with_args
    res = callback(*args, **argMap)
  File "/Users/oliverkuchler/Studium/Apollo/aging-database/Django-files/apollo_pgsql_db/venv/lib/python3.7/site-packages/dash/_callback.py", line 151, in add_context
    output_value = func(*func_args, **func_kwargs)  # %% callback invoked %%
  File "/Users/oliverkuchler/Studium/Apollo/aging-database/Django-files/apollo_pgsql_db/DashApp/Minimal_Example_for_Select_Bug.py", line 95, in callback_c
    da = kwargs['dash_app']
KeyError: 'dash_app'

My minimal code:

import dash
import dash_core_components as dcc
import dash_html_components as html
from django_plotly_dash import DjangoDash

a2 = DjangoDash("Ex2")

a2.layout = html.Div([
    dcc.RadioItems(id="dropdown-one",options=[{'label':i,'value':j} for i,j in [
    ("O2","Oxygen"),("N2","Nitrogen"),("CO2","Carbon Dioxide")]
    ],value="Oxygen"),
    html.Div(id="output-one")
    ])

@a2.callback(
    dash.dependencies.Output('output-one','children'),
    [dash.dependencies.Input('dropdown-one','value')]
    )
def callback_c(*args,**kwargs):
    da = kwargs['dash_app']
    return "Args are [%s] and kwargs are %s" %(",".join(args), kwargs)

Django-Dash relevant libraries listed by Pip:

dash                      2.0.0
dash-bootstrap-components 1.0.2
dash-core-components      2.0.0
dash-cytoscape            0.3.0
dash-html-components      2.0.0
dash-renderer             1.9.1
dash-table                5.0.0
Django                    3.2.12
django-plotly-dash        1.6.6
Flask                     2.0.1
Flask-Compress            1.10.1
pip                       21.2.4
plotly                    5.3.1
robd003 commented 2 years ago

Honestly I gave up on Plotly shortly after this. I've had great luck with doing charts myself in React using Nivo