how to embed standalone bokeh graphs into django templates

后端 未结 5 950
半阙折子戏
半阙折子戏 2020-11-28 21:22

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 g

5条回答
  •  一个人的身影
    2020-11-28 22:01

    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 :

    
    
    
        
        Experiment with Bokeh
        
        
    
    
    
        {{ the_div|safe }}
    
        {{ the_script|safe }}
    
     
    

    And it works.

提交回复
热议问题