Problems with Panel and Django

Hello everyone.

I’m having serious trouble integrating Panel with Django. I have tried using the official guide (https://panel.holoviz.org/user_guide/Django_Apps.html), and also with the examples in GitHub (https://github.com/holoviz/panel/tree/master/examples/apps), but I always have the same problem:

    Exception inside application: __init__() takes 1 positional argument but 2 were given
Traceback (most recent call last):
  File "/Users/jumy/Documents/Development/Python/panel/examples/apps/django_multi_apps/venv/lib/python3.8/site-packages/channels/routing.py", line 71, in __call__
    return await application(scope, receive, send)
  File "/Users/jumy/Documents/Development/Python/panel/examples/apps/django_multi_apps/venv/lib/python3.8/site-packages/channels/sessions.py", line 47, in __call__
    return await self.inner(dict(scope, cookies=cookies), receive, send)
  File "/Users/jumy/Documents/Development/Python/panel/examples/apps/django_multi_apps/venv/lib/python3.8/site-packages/channels/sessions.py", line 254, in __call__
    return await self.inner(wrapper.scope, receive, wrapper.send)
  File "/Users/jumy/Documents/Development/Python/panel/examples/apps/django_multi_apps/venv/lib/python3.8/site-packages/channels/auth.py", line 181, in __call__
    return await super().__call__(scope, receive, send)
  File "/Users/jumy/Documents/Development/Python/panel/examples/apps/django_multi_apps/venv/lib/python3.8/site-packages/channels/middleware.py", line 26, in __call__
    return await self.inner(scope, receive, send)
  File "/Users/jumy/Documents/Development/Python/panel/examples/apps/django_multi_apps/venv/lib/python3.8/site-packages/channels/routing.py", line 150, in __call__
    return await application(
  File "/Users/jumy/Documents/Development/Python/panel/examples/apps/django_multi_apps/venv/lib/python3.8/site-packages/asgiref/compatibility.py", line 33, in new_application
    instance = application(scope)
TypeError: __init__() takes 1 positional argument but 2 were given
HTTP GET /sliders 500 [0.33, 127.0.0.1:58367]
Exception inside application: __init__() takes 1 positional argument but 2 were given
Traceback (most recent call last):
  File "/Users/jumy/Documents/Development/Python/panel/examples/apps/django_multi_apps/venv/lib/python3.8/site-packages/channels/routing.py", line 71, in __call__
    return await application(scope, receive, send)
  File "/Users/jumy/Documents/Development/Python/panel/examples/apps/django_multi_apps/venv/lib/python3.8/site-packages/channels/sessions.py", line 47, in __call__
    return await self.inner(dict(scope, cookies=cookies), receive, send)
  File "/Users/jumy/Documents/Development/Python/panel/examples/apps/django_multi_apps/venv/lib/python3.8/site-packages/channels/sessions.py", line 254, in __call__
    return await self.inner(wrapper.scope, receive, wrapper.send)
  File "/Users/jumy/Documents/Development/Python/panel/examples/apps/django_multi_apps/venv/lib/python3.8/site-packages/channels/auth.py", line 181, in __call__
    return await super().__call__(scope, receive, send)
  File "/Users/jumy/Documents/Development/Python/panel/examples/apps/django_multi_apps/venv/lib/python3.8/site-packages/channels/middleware.py", line 26, in __call__
    return await self.inner(scope, receive, send)
  File "/Users/jumy/Documents/Development/Python/panel/examples/apps/django_multi_apps/venv/lib/python3.8/site-packages/channels/routing.py", line 150, in __call__
    return await application(
  File "/Users/jumy/Documents/Development/Python/panel/examples/apps/django_multi_apps/venv/lib/python3.8/site-packages/asgiref/compatibility.py", line 33, in new_application
    instance = application(scope)
TypeError: __init__() takes 1 positional argument but 2 were given
HTTP GET /favicon.ico 500 [0.94, 127.0.0.1:58367]

Any idea of what can be the cause?

Also, pip fails when I try to install requirements.txt:

ERROR: Cannot install -r requirements.txt (line 13), -r requirements.txt (line 8) and param==1.9.2 because these package versions have conflicting dependencies.

The conflict is caused by:
    The user requested param==1.9.2
    colorcet 2.0.2 depends on param>=1.7.0
    holoviews 1.13.2 depends on param<2.0 and >=1.9.3

To fix this you could try to:
1. loosen the range of package versions you've specified
2. remove package versions to allow pip attempt to solve the dependency conflict

ERROR: ResolutionImpossible: for help visit https://pip.pypa.io/en/latest/user_guide/#fixing-conflicting-dependencies

So I have to install the required packages manually.

Any idea on how I can make this run?

Thanks in advance.

OK, I managed to make run one of the examples, but not before tweaking some of the dependencies.

I had to start with the requirements.txt from the django_multi_apps example. When I tried to install it, I was having the following conflicts:

The conflict is caused by:
    The user requested param==1.9.2
    colorcet 2.0.2 depends on param>=1.7.0
    holoviews 1.13.2 depends on param<2.0 and >=1.9.3

So I removed the version lock to those packages. However, it was still having some other conflicts:

The conflict is caused by:
    The user requested bokeh==1.4.0
    hvplot 0.5.2 depends on bokeh>=1.0.0
    panel 0.9.3 depends on bokeh>=2.0.0

I sorted that by removing the version lock for bokeh.

At the end, my requirements.txt ended like this:

asgiref==3.2.3
attrs==19.3.0
autobahn==19.11.1
Automat==0.8.0
bokeh
cffi==1.13.2
channels==2.4.0
colorcet
constantly==15.1.0
cryptography==3.2
daphne==2.4.1
Django==3.0.7
holoviews
hvplot==0.5.2
hyperlink==19.0.0
idna==2.8
incremental==17.5.0
Jinja2==2.10.3
Markdown==3.1.1
MarkupSafe==1.1.1
numpy==1.18.0
packaging==20.0
pandas==0.25.3
panel==0.9.3
param
Pillow==7.1.0
pyasn1==0.4.8
pyasn1-modules==0.2.7
pycparser==2.19
pyct==0.4.6
PyHamcrest==1.9.0
pyOpenSSL==19.1.0
pyparsing==2.4.6
python-dateutil==2.8.1
pytz==2019.3
pyviz-comms==0.7.4
PyYAML==5.2
service-identity==18.1.0
six==1.13.0
sqlparse==0.3.0
tornado==6.0.3
Twisted==20.3.0
txaio==18.8.1
zope.interface==4.7.1

That allowed the example to work.

There are a few things that I still have to tweak, but this is at least a good start :slight_smile:

I hope this can help to anyone else trying to give a go to Panel in Django.

Hi @realjumy

Welcome to the community and thanks a lot for sharing your problem and solution. Happy you figured it out.

1 Like

I have now a new error when trying to use Gunicorn or Daphne:

http://localhost/gbm/autoload.js?bokeh-autoload-element=1003&bokeh-app-path=/gbm&bokeh-absolute-url=http://localhost/gbm

Any idea on how can I sort this?

Thanks in advance.

OK, I managed to make it work. In case anyone else is interested, this is what is missing in the example:

File asgi.py :

import os
import django
from channels.routing import get_default_applicationos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'yourdjangoappname.settings.dev')
django.setup()
application = get_default_application()

At the end of the base.py :

ASGI_APPLICATION = 'yourdjangoappname.routing.application'

And then use Daphne to serve it:

daphne -p 8001 yourdjangoappname.asgi:application

I hope it can be of help to anyone else trying to do something with ASGI and Django.

1 Like

Awesome. Great you got it solved. And thanks for sharing the knowledge with the community.

Uvicorn or Daphne are needed for taking these Django-Panel apps to production, so I thought it would be interesting to share my solution with the community.

3 Likes

Very helpful, thank you @realjumy! Anything we should put in the Django user guide in Panel?

That’s a tricky question.

It would be useful to be able to explain how to use Daphne as Gunicorn can’t be used for this… and I wasn’t able to make this to work with Uvicorn… So adding how to use Daphne to the user guide is something that definitely people would find useful. Also, it’s important the addition of ASGI_APPLICATION = 'yourdjangoappname.routing.application' in the base.py file (substituting yourdjangoappname with the name of the app for the example, of course).

There’s also a problem in urls.py: I was only able to make the app to run if I commented out from bokeh.server.django import static_extensions, an then urlpatterns += static_extensions(). I don’t know the implications of that, but it seems to be a problem importing static_extensions.

Also, the versioning of the modules used is very tricky… I was only able to have everything running with a very specific combination of versions, as I explained in one of my earlier comments… And that can be the explanation why I wasn’t able to import static_extensions.

All these problems with the versioning, and the fact that I’m not sure of the consequences of not using static_extensions is what makes the question of what to add to the user guide tricky…

1 Like

Hey @realjumy,
related to the the multiapps example. What ist the base.py file you edited?

It was my configuration file for my Django app. It’s the equivalent to your examples/apps/django2/project/settings.py. Sorry, I forgot to use the conventional name in my previous entries.

1 Like

I’m having an issue with the official examples as well. I downgraded to channels=2 and that seems to solve it for now, but it appears that channels will be dropping support for django=2 at some point in the near future, so support for channels=3 needs to be added.

It looks like it’s primarily changing how init() and call() are implemented. More here:
https://channels.readthedocs.io/en/stable/releases/3.0.0.html

Yes, version compatibility between packages is a headache. It took me a good while to find the right version for each of them. And it becomes a bit of a hell once you are using a Django project that is a bit complex.