天天看点

Panel Django Apps例子运行asgiref\compatibility.py报错的解决

Panel Django Apps例子运行asgiref\compatibility.py报错的解决

    • 案例梳理
    • 文件目录结构
    • 项目同名文件夹mydjpro下setting.py注意点:

问题出在channels的版本上,默认使用pip 或conda安装的都是最新的,在使用 python manage.py runserve 运行时会报错:

File "D:\Program Files\python\python3.8.2\lib\site-packages\asgiref\compatibility.py", line 33, in new_application
    instance = application(scope)
TypeError: __init__() takes 1 positional argument but 2 were given
           

卸载channels,重新安装 pip install channels==2.4.0 报错不在出现。

案例梳理

官网上的:https://panel.holoviz.org/user_guide/Django_Apps.html,流程已经很清楚,这里简单梳理一下,添加多个app的情况:

  1. 项目所在目录下创建新的app: python manage.py startapp polls(polls是app的名称);
  2. 在新创建的app目录下 创建pn_app.py文件用来去取数据 创建图形 如例中:
def app(doc):
    sw = SineWave()
    row = pn.Row(sw.param, sw.plot) # 创建panel的一系列的操作
	row.server_doc(doc) # 最后调用 server_doc() 
           
  1. app目录下创建urls.py 进行路由 例中:
from django.urls import path
from . import views

app_name = 'sliders'
urlpatterns = [
    path('', views.sliders, name='sliders'),
]
           
  1. app目录下创建views.py 控制视图展示,例中
from bokeh.embed import server_document
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render


def sliders(request: HttpRequest) -> HttpResponse:
    script = server_document(request.build_absolute_uri())
    return render(request, "sliders/base.html", dict(script=script))
           
  1. 在项目目录的项目同名文件夹下的urls.py中添加新的app的路由 和bokeh_app;
.......
import sliders.pn_app as sliders_app
urlpatterns = [
	.......
    path('sliders/', include('sliders.urls')),
]
bokeh_apps = [
	......
    autoload("sliders", sliders_app.app),
]
           
  1. 项目目录下的templates文件下新建对应的app同名文件夹 存放模板 使用javascripty显示加载图片,加载完成后隐藏,默认模板主要内容是:
{% block content %}
    {{ script|safe }}
  {% endblock %}
 # 修改成:-----------------------------------------------------
 <!DOCTYPE html>
<html>
  <head>
    <title>ship_traffice_django</title>
    <style>
		#loading_bg{background: #fdfdfd; width: 100%;height: 100%;position: fixed;}
		#loading_bg img{position: fixed;left: 0;right: 0;margin: 0 auto;}
		#loading_bg p{position: fixed; left: 0; right: 0; text-align: center; top: 286px;color: #777;font-weight: bold;}
	</style>
  </head>
  <body>
  <div id="loading_bg">
		<img src="/static/images/loading3.gif"/>
		<p>页面加载中,请稍后...</p>
  </div>
  {% block content %}
    {{ script|safe }}
  {% endblock %}
  <script type="text/javascript">
     xhr.onreadystatechange = function(){
          //请求完成 隐藏掉加载图的效果
          if(xhr.readyState === 4 && xhr.status === 200){
              document.getElementById("loading_bg").style.display = 'none';
          }
      }
  </script>
  </body>
</html>
           

处理数据比较大的时候,页面加载的过程很长 效果如下

Panel Django Apps例子运行asgiref\compatibility.py报错的解决

文件目录结构

Panel Django Apps例子运行asgiref\compatibility.py报错的解决
Panel Django Apps例子运行asgiref\compatibility.py报错的解决

项目同名文件夹mydjpro下setting.py注意点:

添加:

ASGI_APPLICATION = "mydjpro.routing.application"  # mydjpro 改成你的项目名
from bokeh.settings import bokehjsdir
STATICFILES_DIRS = [bokehjsdir(), os.path.join(BASE_DIR, 'static')]