django-plotly-dash: {% plotly_app name='SimpleExample'%} renders a TypeError issue with Dash 1.11.0

When I try to render a plot from the tutorial by in my Django website, I get a TypeError. See below my code snippets and a traceback that I get. I’ve been trying to understand where the problem appears but nothing comes into my mind. The error disappears when I remove “{% plotly_app name=“SimpleExample” %}”, so I assume it’s related to django-plotly-dash package.

In Urls.py I’ve included the required “path(‘django_plotly_dash/’, include(‘django_plotly_dash.urls’))”

simpleexample.py

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

app = DjangoDash('SimpleExample')   # replaces dash.Dash

app.layout = html.Div([
    dcc.RadioItems(
        id='dropdown-color',
        options=[{'label': c, 'value': c.lower()} for c in ['Red', 'Green', 'Blue']],
        value='red'
    ),
    html.Div(id='output-color'),
    dcc.RadioItems(
        id='dropdown-size',
        options=[{'label': i,
                  'value': j} for i, j in [('L','large'), ('M','medium'), ('S','small')]],
        value='medium'
    ),
    html.Div(id='output-size')

])

@app.callback(
    dash.dependencies.Output('output-color', 'children'),
    [dash.dependencies.Input('dropdown-color', 'value')])
def callback_color(dropdown_value):
    return "The selected color is %s." % dropdown_value

@app.callback(
    dash.dependencies.Output('output-size', 'children'),
    [dash.dependencies.Input('dropdown-color', 'value'),
     dash.dependencies.Input('dropdown-size', 'value')])
def callback_size(dropdown_color, dropdown_size):
    return "The chosen T-shirt is a %s %s one." %(dropdown_size,
                                                  dropdown_color)

welcome.html (I moved that part to base.html when testing to better show the error in the traceback)

{%  extends 'base.html' %}
    {% load static %}
    {% block content %}
        {% load plotly_dash %}
    
    <block>
    <div>
        {% plotly_app name='SimpleExample' ratio=0.45 %}
    </div>
    </block>
    {% endblock %}

views.py

from django.urls import path
    from . import views
    from home.dash_apps.finished_apps import simpleexample
    
    urlpatterns = [
        path('', views.home, name='home')

This is the traceback which I get after placing the content welcome.html in base.html to better identify where the problem lies

Environment: Request Method: GET Request URL: http://127.0.0.1:8000/

Django Version: 3.0
Python Version: 3.7.7
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'home.apps.HomeConfig',
 'django_plotly_dash.apps.DjangoPlotlyDashConfig',
 'channels',
 'channels_redis']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template C:\Users\milos\statisticsofcorona\templates\base.html, error at line 55
   expected string or bytes-like object
   45 :         <div class="container-fluid">
   46 : 
   47 :           <!-- Page Heading -->
   48 :           <div class="d-sm-flex align-items-center justify-content-between mb-4">
   49 :             <h1 class="h3 mb-0 text-gray-800">Dashboard</h1>
   50 :             <a href="#" class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm"><i class="fas fa-download fa-sm text-white-50"></i> Generate Report</a>
   51 :           </div>
   52 :         <div class="row">
   53 :           <div>
   54 :             {% load plotly_dash %}
   55 :              {% plotly_app name="SimpleExample" %} 
   56 :           </div>



Traceback (most recent call last):
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\milos\statisticsofcorona\home\views.py", line 29, in home
    return render(request, 'base.html', context)
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\django\shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\django\template\base.py", line 171, in render
    return self._render(context)
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\django\template\library.py", line 214, in render
    _dict = self.func(*resolved_args, **resolved_kwargs)
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\django_plotly_dash\templatetags\plotly_dash.py", line 76, in plotly_app
    da, app = _locate_daapp(name, slug, da, cache_id=cache_id)
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\django_plotly_dash\templatetags\plotly_dash.py", line 43, in _locate_daapp
    da, app = DashApp.locate_item(name, stateless=True, cache_id=cache_id)
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\django_plotly_dash\models.py", line 200, in locate_item
    app = dash_app.as_dash_instance(cache_id=cache_id)
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\django_plotly_dash\dash_wrapper.py", line 165, in as_dash_instance
    return self.do_form_dash_instance(cache_id=cache_id)
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\django_plotly_dash\dash_wrapper.py", line 201, in do_form_dash_instance
    return self.form_dash_instance(replacements, ndid, base_pathname)
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\django_plotly_dash\dash_wrapper.py", line 213, in form_dash_instance
    serve_locally=self._serve_locally)
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\django_plotly_dash\dash_wrapper.py", line 312, in __init__
    **kwargs)
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\dash\dash.py", line 355, in __init__
    self.init_app()
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\dash\dash.py", line 384, in init_app
    @self.server.errorhandler(PreventUpdate)
  File "C:\Users\milos\statisticsofcorona\myvenv\lib\site-packages\dash\exceptions.py", line 6, in __init__
    super(DashException, self).__init__(dedent(msg).strip())
  File "C:\Users\milos\AppData\Local\Programs\Python\Python37\lib\textwrap.py", line 430, in dedent
    text = _whitespace_only_re.sub('', text)

Exception Type: TypeError at /
Exception Value: expected string or bytes-like object

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

PR #243 merged and released as 1.3.1

Hmm, it seems like 1.3.0 still has the Django 2.n restriction. I did a pip install -U django-plotly-dash and it downgraded Django for me.


(base) C:\Users\pgupta6\Desktop\PyCharmProjects\personal-work-repo>pip install -U django-plotly-dash
Collecting django-plotly-dash
  Downloading django_plotly_dash-1.3.0-py3-none-any.whl (44 kB)
     |████████████████████████████████| 44 kB 550 kB/s
Collecting Django<3,>=2
  Downloading Django-2.2.12-py3-none-any.whl (7.5 MB)
     |████████████████████████████████| 7.5 MB 1.1 MB/s
Requirement already satisfied, skipping upgrade: plotly in c:\anaconda3\lib\site-packages (from django-plotly-dash) (4.6.0)
Requirement already satisfied, skipping upgrade: Flask>=1.0.2 in c:\anaconda3\lib\site-packages (from django-plotly-dash) (1.1.2)
Requirement already satisfied, skipping upgrade: dpd-components in c:\anaconda3\lib\site-packages (from django-plotly-dash) (0.1.0)
Requirement already satisfied, skipping upgrade: dash-renderer in c:\anaconda3\lib\site-packages (from django-plotly-dash) (1.3.0)
Requirement already satisfied, skipping upgrade: dash-core-components in c:\anaconda3\lib\site-packages (from django-plotly-dash) (1.9.0)
Processing c:\users\pgupta6\appdata\local\pip\cache\wheels\96\99\b7\dd0abccbf36509041224628ed0f3ea1f82c59100437eb71370\dash-1.10.0-cp37-none-any.whl
Requirement already satisfied, skipping upgrade: dash-html-components in c:\anaconda3\lib\site-packages (from django-plotly-dash) (1.0.3)
Requirement already satisfied, skipping upgrade: sqlparse in c:\anaconda3\lib\site-packages (from Django<3,>=2->django-plotly-dash) (0.3.1)
Requirement already satisfied, skipping upgrade: pytz in c:\anaconda3\lib\site-packages (from Django<3,>=2->django-plotly-dash) (2019.3)
Requirement already satisfied, skipping upgrade: six in c:\anaconda3\lib\site-packages (from plotly->django-plotly-dash) (1.14.0)
Requirement already satisfied, skipping upgrade: retrying>=1.3.3 in c:\anaconda3\lib\site-packages (from plotly->django-plotly-dash) (1.3.3)
Requirement already satisfied, skipping upgrade: itsdangerous>=0.24 in c:\anaconda3\lib\site-packages (from Flask>=1.0.2->django-plotly-dash) (1.1.0)
Requirement already satisfied, skipping upgrade: click>=5.1 in c:\anaconda3\lib\site-packages (from Flask>=1.0.2->django-plotly-dash) (7.1.1)
Requirement already satisfied, skipping upgrade: Werkzeug>=0.15 in c:\anaconda3\lib\site-packages (from Flask>=1.0.2->django-plotly-dash) (1.0.1)
Requirement already satisfied, skipping upgrade: Jinja2>=2.10.1 in c:\anaconda3\lib\site-packages (from Flask>=1.0.2->django-plotly-dash) (2.11.2)
Requirement already satisfied, skipping upgrade: dash-table==4.6.2 in c:\anaconda3\lib\site-packages (from dash<1.11->django-plotly-dash) (4.6.2)
Requirement already satisfied, skipping upgrade: future in c:\anaconda3\lib\site-packages (from dash<1.11->django-plotly-dash) (0.18.2)
Requirement already satisfied, skipping upgrade: flask-compress in c:\anaconda3\lib\site-packages (from dash<1.11->django-plotly-dash) (1.4.0)
Requirement already satisfied, skipping upgrade: MarkupSafe>=0.23 in c:\anaconda3\lib\site-packages (from Jinja2>=2.10.1->Flask>=1.0.2->django-plotly-dash) (1.1.1)
Installing collected packages: Django, dash, django-plotly-dash
  Attempting uninstall: Django
    Found existing installation: Django 3.0.5
    Uninstalling Django-3.0.5:
      Successfully uninstalled Django-3.0.5
  Attempting uninstall: django-plotly-dash
    Found existing installation: django-plotly-dash 1.1.5
    Uninstalling django-plotly-dash-1.1.5:
      Successfully uninstalled django-plotly-dash-1.1.5
Successfully installed Django-2.2.12 dash-1.10.0 django-plotly-dash-1.3.0

PR #240 removes the Django 2.n restriction. This has been released as version 1.3.0 of django-plotly-dash

Hi pallavg,

Many many thanks for your help. My app finally works as it should. Yesterday I tried installing old packages of django_plotly_dash and django but it didn’t help. For some reason I didn’t think about dash.

I’ve been trying to find the reason by testing the code of django_plotly_dash and I figured out that the method below in models.py (line 232) doesn’t execute. However, I didn’t finish investigating all underlying methods

app = dash_app.as_dash_instance(cache_id=cache_id)

The error appears in one of its underlying methods get_local_stateless_by_name(name) in dash_wrapper.py but I haven’t finished testing this function. I hope this helps.

Good afternoon,

I just want to add that I am also having the same issue. I am using this (great) plugin on Django 3.0.5 on python 3.7.6. In my setup, the plugin works fine with my example if I use the following versions of the dependent components:

OK: dash 1.10.0 dash-bootstrap-components 0.9.2 dash-core-components 1.9.0 dash-html-components 1.0.3
dash-renderer 1.3.0 dash-table 4.6.2 django-plotly-dash 1.1.5 pandas 1.0.3

NOT OK: dash 1.11.0 dash-bootstrap-components 0.9.2 dash-core-components 1.9.1
dash-html-components 1.0.3 dash-renderer 1.4.0 dash-table 4.6.2 django-plotly-dash 1.1.5 pandas 1.0.3

So something has changed in dash from v1.10.0 -> v1.11.0 that is causing this:

Stack trace:

> Environment:
> 
> 
> Request Method: GET
> Request URL: http://127.0.0.1:8000/dashboard/
> 
> Django Version: 3.0.5
> Python Version: 3.7.6
> Installed Applications:
> ['whitenoise.runserver_nostatic',
>  'admin_tools',
>  'admin_tools.theming',
>  'admin_tools.menu',
>  'admin_tools.dashboard',
>  'django.contrib.admin',
>  'django.contrib.auth',
>  'django.contrib.contenttypes',
>  'django.contrib.sessions',
>  'django.contrib.messages',
>  'django.contrib.staticfiles',
>  'django.contrib.sites',
>  'crispy_forms',
>  'wakawaka',
>  'bootstrap4',
>  'django_plotly_dash.apps.DjangoPlotlyDashConfig',
>  'simple_history',
>  'jsoneditor',
>  'adminsortable2',
>  'apps.prodsetup.apps.ProdSetupConfig',
>  'apps.traceconvert.apps.TraceConvertConfig',
>  'apps.lisetup.apps.LiSetupConfig',
>  'apps.dashboard.apps.ProjectDashConfig',
>  'debug_toolbar']
> Installed Middleware:
> ['tvpv_portal.middleware.HealthCheckMiddleware',
>  'django.middleware.security.SecurityMiddleware',
>  'whitenoise.middleware.WhiteNoiseMiddleware',
>  'django.contrib.sessions.middleware.SessionMiddleware',
>  'django.middleware.common.CommonMiddleware',
>  'django.middleware.csrf.CsrfViewMiddleware',
>  'django.contrib.auth.middleware.AuthenticationMiddleware',
>  'django.contrib.messages.middleware.MessageMiddleware',
>  'django_plotly_dash.middleware.BaseMiddleware',
>  'django_plotly_dash.middleware.ExternalRedirectionMiddleware',
>  'django.middleware.clickjacking.XFrameOptionsMiddleware',
>  'tvpv_portal.middleware.AuthRequiredMiddleware',
>  'simple_history.middleware.HistoryRequestMiddleware',
>  'debug_toolbar.middleware.DebugToolbarMiddleware']
> 
> 
> 
> Traceback (most recent call last):
>   File "C:\Anaconda3\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
>     response = get_response(request)
>   File "C:\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
>     response = self.process_exception_by_middleware(e, request)
>   File "C:\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
>     response = wrapped_callback(request, *callback_args, **callback_kwargs)
>   File "C:\Users\pgupta6\Desktop\PyCharmProjects\personal-work-repo\python\src\tvpv_portal\apps\dashboard\views.py", line 17, in dashboard
>     return render(request, 'dashboard/dashboard.html')
>   File "C:\Anaconda3\lib\site-packages\django\shortcuts.py", line 19, in render
>     content = loader.render_to_string(template_name, context, request, using=using)
>   File "C:\Anaconda3\lib\site-packages\django\template\loader.py", line 62, in render_to_string
>     return template.render(context, request)
>   File "C:\Anaconda3\lib\site-packages\django\template\backends\django.py", line 61, in render
>     return self.template.render(context)
>   File "C:\Anaconda3\lib\site-packages\django\template\base.py", line 171, in render
>     return self._render(context)
>   File "C:\Anaconda3\lib\site-packages\django\test\utils.py", line 95, in instrumented_test_render
>     return self.nodelist.render(context)
>   File "C:\Anaconda3\lib\site-packages\django\template\base.py", line 936, in render
>     bit = node.render_annotated(context)
>   File "C:\Anaconda3\lib\site-packages\django\template\base.py", line 903, in render_annotated
>     return self.render(context)
>   File "C:\Anaconda3\lib\site-packages\django\template\loader_tags.py", line 150, in render
>     return compiled_parent._render(context)
>   File "C:\Anaconda3\lib\site-packages\django\test\utils.py", line 95, in instrumented_test_render
>     return self.nodelist.render(context)
>   File "C:\Anaconda3\lib\site-packages\django\template\base.py", line 936, in render
>     bit = node.render_annotated(context)
>   File "C:\Anaconda3\lib\site-packages\django\template\base.py", line 903, in render_annotated
>     return self.render(context)
>   File "C:\Anaconda3\lib\site-packages\django\template\loader_tags.py", line 62, in render
>     result = block.nodelist.render(context)
>   File "C:\Anaconda3\lib\site-packages\django\template\base.py", line 936, in render
>     bit = node.render_annotated(context)
>   File "C:\Anaconda3\lib\site-packages\django\template\base.py", line 903, in render_annotated
>     return self.render(context)
>   File "C:\Anaconda3\lib\site-packages\django\template\library.py", line 192, in render
>     output = self.func(*resolved_args, **resolved_kwargs)
>   File "C:\Anaconda3\lib\site-packages\django_plotly_dash\templatetags\plotly_dash.py", line 155, in plotly_class
>     da, app = _locate_daapp(name, slug, da)
>   File "C:\Anaconda3\lib\site-packages\django_plotly_dash\templatetags\plotly_dash.py", line 43, in _locate_daapp
>     da, app = DashApp.locate_item(name, stateless=True, cache_id=cache_id)
>   File "C:\Anaconda3\lib\site-packages\django_plotly_dash\models.py", line 232, in locate_item
>     app = dash_app.as_dash_instance(cache_id=cache_id)
>   File "C:\Anaconda3\lib\site-packages\django_plotly_dash\dash_wrapper.py", line 173, in as_dash_instance
>     return self.do_form_dash_instance(cache_id=cache_id)
>   File "C:\Anaconda3\lib\site-packages\django_plotly_dash\dash_wrapper.py", line 209, in do_form_dash_instance
>     return self.form_dash_instance(replacements, ndid, base_pathname)
>   File "C:\Anaconda3\lib\site-packages\django_plotly_dash\dash_wrapper.py", line 221, in form_dash_instance
>     serve_locally=self._serve_locally)
>   File "C:\Anaconda3\lib\site-packages\django_plotly_dash\dash_wrapper.py", line 320, in __init__
>     **kwargs)
>   File "C:\Anaconda3\lib\site-packages\dash\dash.py", line 355, in __init__
>     self.init_app()
>   File "C:\Anaconda3\lib\site-packages\dash\dash.py", line 384, in init_app
>     @self.server.errorhandler(PreventUpdate)
>   File "C:\Anaconda3\lib\site-packages\dash\exceptions.py", line 6, in __init__
>     super(DashException, self).__init__(dedent(msg).strip())
>   File "C:\Anaconda3\lib\textwrap.py", line 430, in dedent
>     text = _whitespace_only_re.sub('', text)
> 
> Exception Type: TypeError at /dashboard/
> Exception Value: expected string or bytes-like object

Hope this helps.

As a workaround, I will revert to using dash 1.10.0. It will be great if this can be fixed. Also, is there any progress on #215?

I see #236, which if implemented, would render this plugin useless to those of us who are unofficially using it in Django 3.0.5 😃.

Thank you.