本文介绍了如何将独立的散景图嵌入到 django 模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 django 框架在我的 web 应用程序中显示 bokeh 库提供的图形,但我不想使用 bokeh-server 可执行文件,因为这不是好方法.那有可能吗?如果是,该怎么做?

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?

推荐答案

使用 嵌入散景图 由 Fabio Pliger 建议的文档示例,可以在 Django 中执行此操作:

Using the Embedding Bokeh Plots documentation example as suggested by Fabio Pliger, one can do this in Django:

views.py 文件中,我们输入:

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})

urls.py 文件中,我们可以放置:

in the urls.py file we can put :

from myapp.views import simple_chart
...
...
...
url(r'^simple_chart/$', simple_chart, name="simple_chart"),
...
...

在模板文件 simple_chart.html 中,我们将有:

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.bokeh.org/bokeh/release/bokeh-0.8.1.min.js"></script>
    <link rel="stylesheet" href="http://cdn.bokeh.org/bokeh/release/bokeh-0.8.1.min.css">
</head>
<body>

    {{ the_div|safe }}

    {{ the_script|safe }}
</body>
</html>

它有效.

这篇关于如何将独立的散景图嵌入到 django 模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:49