python - Django中的散景图返回一个空白页
问题描述
我正在尝试通过教程在 django 页面上显示一个简单的散景图,但是加载时网页是空白的 - 没有图表。
有一个类似的 Stack Overflow 问题,其中在 html 文件中引用了错误版本的散景 - 我检查以确保这里不是这种情况。我也尝试使用渲染功能,因为 render_to_response 显然已被弃用,但同样的事情发生了。
<!DOCTYPE html>
<html lang='en'>
<head>
<link href="http://cdn.bokeh.org/bokeh/release/bokeh-1.0.4.min.css" rel="stylesheet" type="text/css">
<link href="http://cdn.bokeh.org/bokeh/release/bokeh-widgets-1.0.4.min.css" rel="stylesheet" type="text/css">
<script src="http://cdn.bokeh.org/bokeh/release/bokeh-1.0.4.min.js"></script>
<script src="http://cdn.bokeh.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})
我希望网页显示折线图。但是,网页在加载时显示为空白。
解决方案
我认为您需要放置第div
一个,因为script
正在搜索它以将代码放入其中。也只需使用INLINE.render()
或CDN.render()
自动链接资源(除非您知道自己在做什么)。所以尝试以下方法:
索引.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})
推荐阅读
- python - Keras 层中的 np.newaxis 操作:乘法与串联
- javascript - Kibana 自动登录
- python - DBSCAN scikit python eps 意外
- ssl - 如何在 pfsense 中使用 squid 记录 https 内容?
- python - 如何在 ffill 或 bfill 等浮点列中填充 0.00 值?
- gradle - gradle checkstyle 错误:预期的文件集合只包含一个文件,但是,它包含 14 个文件
- java - 如何从用户界面获取字符串将其放入方法中并将其作为函数返回?
- c - Lwip tcp_write payload pbuf 使用 memcpy 给出不一致的值
- java - 我们可以在默认目录之外的 ftp 服务器上上传文件吗?
- android - Firebase In App Messaging 不断删除显示事件侦听器