天天看點

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')]