散景服务器和Django

时间:2018-09-03 22:52:43

标签: django nginx localhost bokeh gunicorn

最终,我正在尝试在网站上提供交互式散景图。我正在使用Ubuntu 18.04

nginx / 1.14.0,Django == 2.1,gunicorn == 19.9.0,Python 3.6.5,bokeh-0.13.0

我成功地在本地看到了散景图,通过使用nginx的website.conf中的指令,我在没有django的情况下成功了。

location /bkapp {     
    proxy_pass http://127.0.0.1:5100;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host:$server_port;
    proxy_buffering off;
}

和:

bokeh serve bkapp.py --port 5100 --prefix=bkapp --allow-websocket-origin=www.website.com

...在浏览器中(而非本地)与www.website.com/bkapp上的bokeh服务器进行通信。我不必在路由器上为端口5100添加任何端口触发器或虚拟服务器。

!!!!!!!!!!!!!!!!!!!!

但是,我会喜欢通过django / gunicorn路线: 当前我收到服务器错误(500)

!!!!!!!!!!!!!!!!!!!!

我认为一旦请求进入views.py,我的问题就是路由/通信。我认为我的问题就在这里,我一直在调整 Embed an interactive Bokeh in django views

views.py已正确配置为响应典型请求,但无法与本地bokeh服务器连接/工作

为了将其提供给互联网,我使用以下命令:

bokeh serve perceptron.py --port 5100 --prefix=perceptron --allow-websocket-origin=localhost:5100

views.py:

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
session = pull_session(url="http://localhost:5100/")
script = autoload_server(model=None,
                         app_path="/bkapp",
                         url="http://localhost:5100/",
                         session_id = session.id)

return render(request, 'bkapp/index.html', {
    'script': script})

bkapp / index.html:

{% extends 'home/base.html' %}
{% block content %}

<div >

<link href="https://cdn.pydata.org/bokeh/release/bokeh-0.13.0.min.css" rel="stylesheet" type="text/css">
<link href="https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.13.0.min.css" rel="stylesheet" type="text/css">
<link href="https://cdn.pydata.org/bokeh/release/bokeh-tables-0.13.0.min.css" rel="stylesheet" type="text/css">
<script src="https://cdn.pydata.org/bokeh/release/bokeh-0.13.0.min.js"></script>
<script src="https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.13.0.min.js"></script>
<script src="https://cdn.pydata.org/bokeh/release/bokeh-tables-0.13.0.min.js"></script>
{{ script | safe }}

</div>

{% endblock %}

nginx的website.conf:

upstream website_server {
    server unix:/home/jeff/dev/run/gunicorn.sock fail_timeout=0;
}

server {
    listen 80;
    server_name website.com www.website.com;
    return 301 https://$server_name$request_uri;
}


server {

    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name website.com www.website.com;

    add_header Strict-Transport-Security "max-age=31536000";

    ssl_certificate /etc/letsencrypt/live/website.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/website.com/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'XXX';


    root /usr/share/nginx/html;
    index index.html index.htm;

    location /static/ {
        alias   /home/jeff/website/static/;
    }

    location /media/ {
        expires -1;
        alias /home/jeff/website/media/;
        add_header Cache-Control "public";
    }


    client_max_body_size 4G;
    access_log /home/jeff/website/logs/nginx-access.log;
    error_log /home/jeff/website/logs/nginx-error.log;



    location / {
        proxy_pass http://127.0.0.1:5100;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        #proxy_set_header Host $http_host;
        proxy_set_header Host $host:$server_port;
        proxy_buffering off;
        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://website_server;
            break;
        }
    }



    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /home/jeff/website/static/;
    }
}

这是您可以模仿的bkapp.py(从bokeh的教程中复制):

from random import random

from bokeh.layouts import column
from bokeh.models import Button
from bokeh.palettes import RdYlBu3
from bokeh.plotting import figure, curdoc

# create a plot and style its properties
p = figure(x_range=(0, 100), y_range=(0, 100), toolbar_location=None)
p.border_fill_color = 'black'
p.background_fill_color = 'black'
p.outline_line_color = None
p.grid.grid_line_color = None

# add a text renderer to our plot (no data yet)
r = p.text(x=[], y=[], text=[], text_color=[], text_font_size="20pt",
       text_baseline="middle", text_align="center")

i = 0

ds = r.data_source

# create a callback that will add a number in a random location
def callback():
    global i

    # BEST PRACTICE --- update .data in one step with a new dict
    new_data = dict()
    new_data['x'] = ds.data['x'] + [random()*70 + 15]
    new_data['y'] = ds.data['y'] + [random()*70 + 15]
    new_data['text_color'] = ds.data['text_color'] + [RdYlBu3[i%3]]
    new_data['text'] = ds.data['text'] + [str(i)]
    ds.data = new_data

    i = i + 1

# add a button widget and configure with the call back
button = Button(label="Press Me")
button.on_click(callback)

# put the button and plot in a layout and add to the document
curdoc().add_root(column(button, p))

2018年9月4日星期二,太平洋夏令时间0955 AM: 在views.py中添加了session = pull_session()和会话ID来编写脚本, 现在,每次致电网站/ bkapp都会导致: bokeh服务器错误:

 404 GET /ws?bokeh-protocol-version=1.0&bokeh-session-id=wBvizF5w1yUuFqsa55Ikm3zngMSpcauhG3IvtoICU4Sb (127.0.0.1) 1.66ms

独角兽错误:

File "/home/jeff/dev/lib/python3.6/site-packages/bokeh/client/session.py", line 343, in pull
raise IOError("Cannot pull session document because we failed to connect to the server (to start the server, try the 'bokeh serve' command)")
OSError: Cannot pull session document because we failed to connect to the server (to start the server, try the 'bokeh serve' command)

0 个答案:

没有答案