在Django框架中为bokeh设置'Inline'js和css

时间:2018-08-27 20:58:32

标签: django bokeh django-2.1

我正在尝试运行从链接django-bokeh-example下载的项目。当我运行服务器并加载网页时,在检查时我只看到一个空白页面,我知道加载javascript和CSS时会出现一些错误。谷歌搜索后,我知道我必须设置某种静态参数。但是,我没有真正在下面得到如何以及在哪里设置这些参数的代码。我试图添加 from bokeh.resources import INLINE ,但这没有帮助。有人可以对此进行说明吗

views.py

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

from bokeh.plotting import figure, output_file, show
from bokeh.embed import components
from bokeh.resources import INLINE


def debug(x, name):
    print('\n' * 5)
    print('{} = {}'.format(name, x))
    print('type({}) = {}'.format(name, type(x)))
    print('\n' * 5)

def home(request):
    return render_to_response('home_links.html')

def plot(request):
    x = [1, 2, 3, 4, 5]
    y = [6, 7, 2, 4, 5]

    # output to static HTML file
    output_file("lines.html")

    # create a new plot with a title and axis labels
    p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')

    # add a line renderer with legend and line thickness
    p.line(x, y, legend="Temp.", line_width=2)
    # Store components
    script, div = components(p)

    # Feed them to the Django template.
    return render(request, 'bokeh_graph.html',
                              {'script': script, 'div': div})

def vbar(request):
    plot = figure(plot_width=800, plot_height=500, x_axis_type = 'datetime')

    from random import randint
    x = [i for i in range(15)]
    y = [randint(1, 100) for i in range(len(x))]
    plot.vbar(x=x, width=0.5, bottom=0, top=y, color="#CAB2D6")

    plot.xaxis.axis_label = 'Tempo'
    plot.yaxis.axis_label = 'Ocorrencias'


    plot.title.text_font = "times"
    plot.title.text_font_style = "italic"
    plot.title.text = 'A good title'
    plot.title.align = 'center'
    plot.title.text_font_size = '20pt'

    script, div = components(plot)

    # Feed them to the Django template.
    return render(request, 'bokeh_graph.html',
                  {'script': script, 'div': div})

bokeh-graph.html

{% extends 'base.html' %}

{% block title %} Bokeh child! {% endblock %}

{% block head %}
    <link rel="stylesheet" href="https://cdn.pydata.org/bokeh/release/bokeh-0.12.16.min.css" type="text/css" />
    <script type="text/javascript" src="https://cdn.pydata.org/bokeh/release/bokeh-0.12.16.min.js"></script>
    <script type="text/javascript">
        Bokeh.set_log_level("info");
    </script>
{% endblock %}

{% block main %}
    {{ div | safe }}
    {{ script | safe }}
{% endblock %}

urls.py

from django.urls import include, path
from . import views

urlpatterns = [

    path('', views.home, name='home'),
    path('plot', views.plot, name='plot'),
    path('vbar', views.vbar, name='bar'),
]

0 个答案:

没有答案