Django中的散景图返回空白页

时间:2019-04-01 10:52:43

标签: python django bokeh

我正在尝试通过tutorial来在django页上显示一个简单的散景图,但是该网页在加载时为空白-没有图表。

有一个类似的堆栈溢出问题,其中在html文件中引用了错误的bokeh版本-我检查以确保此处不是这种情况。我还尝试使用render函数,因为render_to_response显然已被弃用,但发生了同样的事情。

<!DOCTYPE html>
<html lang='en'>
    <head>
        <link href="http://cdn.pydata.org/bokeh/release/bokeh-1.0.4.min.css" rel="stylesheet" type="text/css">
        <link href="http://cdn.pydata.org/bokeh/release/bokeh-widgets-1.0.4.min.css" rel="stylesheet" type="text/css">
        <script src="http://cdn.pydata.org/bokeh/release/bokeh-1.0.4.min.js"></script>
        <script src="http://cdn.pydata.org/bokeh/release/bokeh-widgets-1.0.4.min.js"></script>
        {{ script | safe }}
        <title>testing bokeh...</title>
    </head>

    <body>
        {{ div | safe }}
    </body>
</html>
from django.shortcuts import render, render_to_response
from bokeh.plotting import figure, output_file, show
from bokeh.embed import components
def bokehTutorial(request):
    x, y, = [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]
    #Setup graph plot
    plot = figure(title = 'Line Chart', x_axis_label = 'X axis', y_axis_label = 'Y axis', plot_width = 400, plot_height = 400)
    #plot line
    plot.line(x, y, line_width = 2)
    #store components
    script, div = components(plot)
    #return to django homepage with components sent as arguments which will then be displayed
    return render_to_response('appName/bokehTutorial.html', {'script': script, 'div': div})
    #return render(request, 'appName/bokehTutorial.html', {'script': script, 'div': div})

我希望网页显示折线图。但是,该网页在加载时显示为空白。

2 个答案:

答案 0 :(得分:0)

我认为您需要首先放置div,因为script正在搜索以将代码放入其中。同样,只需使用INLINE.render()CDN.render()自动链接资源(除非您知道自己在做什么)。因此,请尝试以下操作:

index.html

<!DOCTYPE html>
<html lang='en'>
    <head>
        {{ resources | safe }}
        <title>testing bokeh...</title>
    </head>  
    <body>
        {{ div | safe }}
        {{ script | safe }}
    </body>
</html>

django_app.py

from django.shortcuts import render, render_to_response
from bokeh.plotting import figure, output_file, show
from bokeh.embed import components
from bokeh.resources import INLINE

def bokehTutorial(request):
    x, y, = [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]
    #Setup graph plot
    plot = figure(title = 'Line Chart', x_axis_label = 'X axis', y_axis_label = 'Y axis', plot_width = 400, plot_height = 400)
    #plot line
    plot.line(x, y, line_width = 2)
    #store components
    script, div = components(plot)
    #return to django homepage with components sent as arguments which will then be displayed
    return render_to_response('appName/bokehTutorial.html', {'resources' = INLINE.render(), 'script': script, 'div': div})

答案 1 :(得分:0)

我在最后一行收到合成错误。有什么想法吗? return render('fish / ticket_class.html',{'resources'= INLINE.render(),'script':script,'div':div})