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

时间:2020-03-31 07:51:55

标签: python django bokeh

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

浏览器返回OSError:

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

服务器本身为请求返回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 %}

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

很高兴收到其他尝试的建议!

1 个答案:

答案 0 :(得分:0)

感谢@Eugene Pakhomov提供的指针,需要将#include <stdio.h> #include <stdlib.h> #include <string.h> #define telenum 50 #define MAX_LINE_SIZE 1024 void extractNameFromBuffer(char *buffer) { int trash; char extractorBuffer[MAX_LINE_SIZE]; // Separate the string and throw the number away sscanf(buffer, "%d %s", &trash, extractorBuffer); // clear buffer to store the fresh-new name! memset(buffer, '\0', MAX_LINE_SIZE); // Now, store the gotten name into the buffer memcpy(buffer, extractorBuffer, strlen(extractorBuffer)); } char **readNames ( char fileName[], int amountOfLines ) { FILE *fp = NULL; int i = 0, lineLength = 0; char **names, buffer[MAX_LINE_SIZE]; fp = fopen(fileName,"r"); if (fp == NULL) { fprintf(stderr, "Error: unable to open file...\n"); return NULL; } names = (char **)malloc((amountOfLines) * sizeof(char *)); for(i=0; i<amountOfLines; i++) { // Clean the buffer before read a new line memset(buffer, '\0', MAX_LINE_SIZE); // Read the complete line, including spaces and \n's fgets(buffer, MAX_LINE_SIZE, fp); // If read nothing this time if(buffer[0] == '\0') { printf("This line is blank or does not exist, trying next line\n"); } else { // Calculate the lenth of the line (including \n and special/blank characters) lineLength = strlen(buffer); // Overwrite the \n with a \0, removing the \n of the buffer buffer[lineLength - 1] = '\0'; // Here we have the "165156 SOMENAME" strings // Separate name from number extractNameFromBuffer(buffer); // Here we have the "SOMENAME" string, already! // Updates lineLength (now we don't have the \n) lineLength = strlen(buffer); // Allocate memory to hold the name names[i] = (char *)malloc( (lineLength + 1) * sizeof(char) ); // Copy the name to the new memory space strcpy(names[i], buffer); } } fclose(fp); return names; } void printNames ( char **names, int amountOfLines ) { int i; for (i=0; i<amountOfLines; i++) { if(names[i] != NULL) { printf("%s\n", names[i]); free(names[i]); } else { printf("No names names[%d] space\n", i); } } free(names); } int main () { char **names = NULL; names = readNames("directory.txt",telenum); printNames(names,telenum); } 更新为:

views.py

views.py

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

testserver.html


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})