如何将 Django 应用程序连接到散景服务器?

How to connect a django app to bokeh server?

我目前正在尝试散景服务器为我的 django 应用程序提供一些交互式散景图表,但很难通过 views.py 连接。

浏览器returns OSError:

Cannot pull session document because we failed to connect to the server (to start the server, try the 'bokeh serve' command)

服务器本身returns请求的404错误:

200-03-31 08:17:09,179 404 GET /ws?bokeh-protocol-version=1.0&bokeh-session-id=ZhGn4XcmC1qDPYtF4SRPGPa1GzSgTqktqmvkIEGJke26 (127.0.0.1) 0.55ms

我的服务器设置如下:

bokeh_server
|
|--main.py

main.py是一张非常基本的散景图

main.py

from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.plotting import figure
from bokeh.models.sources import ColumnDataSource

source = ColumnDataSource(dict(x=list(range(5)), y=list(range(5))))
p = figure(width=300, height=300, tools=[], toolbar_location=None, name="test")
p.line(x='x', y='y', source=source)
curdoc().add_root(column(p, sizing_mode='scale_width'))

运行 服务器 (bokeh serve bokeh_server) 然后在浏览器中打开 https://localhost:5006 可以正确呈现图表 (https://localhost:5006/bokeh_server)

当我尝试从我的 Django 应用程序打开它时出现问题。

views.py

from django.shortcuts import render
from django.http import HttpResponse
from bokeh.client import pull_session

def testbserver(request):
    session = pull_session(url="http://localhost:5006/")
    script = server_session(model=None,
                            session_id=None,
                            url="http://localhost:5006/",
                            )
    return render(request, 'testserver.html', {'script':script})

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('testbserver/', views.testbserver, name='testbserver'),
]

testserver.html

{% extends "base_generic.html" %}

{% block content %}
<div class="container-fluid">
  <div class="row">
    <div class="col-sm-12">
    {{script | safe }}
    </div>
  </div>
</div>
{% endblock %}

我已经尝试 运行 允许使用 websockets 的服务器 (bokeh serve bokeh_server/ --allow-websocket-origin="*" --log-level trace) 但仍然出现相同的错误。

非常感谢收到关于尝试其他事情的任何建议!

感谢@Eugene Pakhomov 的指点,需要将 views.py 更新为:

views.py


from django.shortcuts import render
from django.http import HttpResponse
from bokeh.client import pull_session
from bokeh.embed import server_session

def testbserver(request):
    session = pull_session(url="http://localhost:5006/bokeh_server")
    script = server_session(model=None,
                            session_id=session.id,
                            url="http://localhost:5006/bokeh_server",
                            )
    return render(request, 'testserver.html', {'script':script})

testserver.html 中也存在问题 - 我没有使脚本安全:

testserver.html

{% extends "base_generic.html" %}

{% block content %}

<head>
<link href = "https://cdn.pydata.org./bokeh/release/bokeh-1.4.0.min.css" rel = "stylesheet" type = "text/css" >
<link href = "https://cdn.pydata.org./bokeh/release/bokeh-widgets-1.4.0.min.css" rel = "stylesheet" type = "text/css" >
<link href = "https://cdn.pydata.org./bokeh/release/bokeh-tables-1.4.0.min.css" rel = "stylesheet" type = "text/css" >

<script type = "text/javascript" src = "https://cdn.pydata.org./bokeh/release/bokeh-1.4.0.min.js"> </script>
<script type = "text/javascript" src = "https://cdn.pydata.org./bokeh/release/bokeh-widgets-1.4.0.min.js"> </script>
<script type = "text/javascript" src = "https://cdn.pydata.org./bokeh/release/bokeh-tables-1.4.0.min.js"> </script>

<h1>Test</h1>
{{script | safe}}
</head> 
<div style="margin-left:20px;margin-top:20px">

<body>
    {{div | safe}}
</body>

{% endblock %}