试图将Bokeh Wordcloud2传递给Django Templete

时间:2019-10-29 19:16:14

标签: django django-templates bokeh word-cloud wordcloud2

我正在使用Bokeh库将数据可视化传递给Django项目。我能够通过标准的Bokeh可视化,但是当我尝试使用外部wordcloud2库时,我的项目崩溃了。我收到消息“发生服务器错误。请与管理员联系。”任何想法将不胜感激?!

使用的进口:

        from bokeh.plotting import figure, output_file, show
        from bokeh.embed import components
        # Bokeh WordCloud
        from bokeh.io import show
        from bokeh.models import ColumnDataSource
        from bokeh_wordcloud2 import WordCloud2

views.py

下面的代码有效:

        x = [1,2,3,4,5]
        y = [1,2,3,4,5]

        plot = figure(title='Line Graph', x_axis_label='x_stuff', y_axis_label='y_stuff', plot_width=400, plot_height=400)
        plot.line(x, y, line_width=2)

        #auto_div - dynamically generates div elemetent
        script, auto_div = components(plot)

基于上面的示例,我试图以非常相似的方式传递wordcloud的值,但是它不起作用:

        titles = ['lorem ipsum dolor sit amet',
                'consectetur adipiscing elit',
                'cras iaculis semper odio',
                'eu posuere urna vulputate sed']
        test1 = ColumnDataSource({'titles':titles})  
        wordcloud = WordCloud2(source=test1,wordCol="titles",color=['pink','blue','green']) 

        script, auto_div = components(wordcloud)

        context = {
            'script':script,
            'auto_div':auto_div,
        }

        return render(request, 'two_boxes/home.html', context)

根据我的测试,问题似乎出现在script, auto_div = components(wordcloud)语句

home.html

        <head>
        {% block bokeh_dependencies %}
        <link href="http://cdn.bokeh.org/bokeh/release/bokeh-1.3.4.min.css" rel=”stylesheet” type=”text/css”>
        <link href="http://cdn.bokeh.org/bokeh/release/bokeh-widgets-1.3.4.min.css" rel=”stylesheet” type=”text/css”>

        <script src="http://cdn.bokeh.org/bokeh/release/bokeh-1.3.4.min.js"></script>
        <script src="http://cdn.bokeh.org/bokeh/release/bokeh-widgets-1.3.4.min.js"></script>
        {{ script | safe }}
        {% endblock %}
        </head>

        <body>
        <h1>Bokeh Stuff</h1>
        {{ auto_div | safe }}
        </body>

资源:

https://hackernoon.com/integrating-bokeh-visualisations-into-django-projects-a1c01a16b67a https://github.com/joranbeasley/bokeh_wordcloud2

0 个答案:

没有答案