如何将独立的散景图嵌入到django模板中 [英] how to embed standalone bokeh graphs into django templates
问题描述
使用嵌入式散景图文档示例,由Fabio Pliger建议,可以在Django中执行此操作:
views.py
文件,我们将:django.shortcuts中的
导入render
从bokeh.plotting导入图
从bokeh.resources导入CDN
从bokeh.embed导入组件
def simple_chart(request):
plot = figure()
plot.circle([1,2],[3,4])
脚本,div =组件(plot,CDN)
return render(request,simple_chart.html,{the_script:script,the_div:div})
pre pre $ p $ from myapp.views import simple_chart
...
...
...
url(r'^ simple_chart / $',simple_chart,name =simple_chart ),
...
...
在模板文件中 simple_chart.html
我们将有:
<!DOCTYPE html>
< html lang =en>
< head>
< meta charset =UTF-8>
< title>实验散景< / title>
< script src =http://cdn.pydata.org/bokeh/release/bokeh-0.8.1.min.js>< / script>
< link rel =stylesheethref =http://cdn.pydata.org/bokeh/release/bokeh-0.8.1.min.css>
< / head>
< body>
{{the_div | safe}}
{{the_script | safe}}
< / body>
< / html>
它的工作原理。
I want to display graphs offered by the bokeh library in my web application via django framework but I don't want to use the bokeh-server executable because it's not the good way. so is that possible? if yes how to do that?
Using the Embedding Bokeh Plots documentation example as suggested by Fabio Pliger, one can do this in Django:
in the views.py
file, we put:
from django.shortcuts import render
from bokeh.plotting import figure
from bokeh.resources import CDN
from bokeh.embed import components
def simple_chart(request):
plot = figure()
plot.circle([1,2], [3,4])
script, div = components(plot, CDN)
return render(request, "simple_chart.html", {"the_script": script, "the_div": div})
in the urls.py
file we can put :
from myapp.views import simple_chart
...
...
...
url(r'^simple_chart/$', simple_chart, name="simple_chart"),
...
...
in the template file simple_chart.html
we'll have :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Experiment with Bokeh</title>
<script src="http://cdn.pydata.org/bokeh/release/bokeh-0.8.1.min.js"></script>
<link rel="stylesheet" href="http://cdn.pydata.org/bokeh/release/bokeh-0.8.1.min.css">
</head>
<body>
{{ the_div|safe }}
{{ the_script|safe }}
</body>
</html>
And it works.
这篇关于如何将独立的散景图嵌入到django模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!