热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

从django视图的图象的Bokeh图-BokehCharttoimagefromdjangoview

HowdoIconvertabokehcharttoanimagethatIcansendtothewebclientfromdjango?如何将散景图转换为

How do I convert a bokeh chart to an image that I can send to the web client from django?

如何将散景图转换为可以从django发送到Web客户端的图像?

Here's my views.py

这是我的views.py

from bokeh.charts import TimeSeries
def stock_chart(request):
                pdata = DataReader('GOOG',  'yahoo', datetime(2015,1,1), datetime(2015,1,15))
                xyvalues = pd.DataFrame(dict(
                        price=pdata['Adj Close'],
                        Date=pdata.index.values,
                ))

                pt = TimeSeries(xyvalues, index='Date', legend=True,
                         title=symbol , ylabel='Stock Prices', width=400, height=200)
               #fig=pt.savefig()??????????? Here i need to convert pt to an image.
               return HttpResponse(fig)


urls.py

urlpatterns = patterns('',
            url(r'^stock_chart/$', views.stock_chart, name="stock_chart"),
)

2 个解决方案

#1


1  

I'd recommend using the bokeh.embed module:

我建议使用bokeh.embed模块:

http://bokeh.pydata.org/en/latest/docs/user_guide/embed.html#components

http://bokeh.pydata.org/en/latest/docs/user_guide/embed.html#components

from bokeh.embed import components
script, div = components(pt)

Then you just have to insert the script and div into your html template.

然后你只需要将脚本和div插入到你的html模板中。

Sarah Bird also gave a good talk on embedding Bokeh plots in Django apps at Pycon: (https://us.pycon.org/2015/schedule/presentation/369/)

Sarah Bird还就在Pycon的Django应用程序中嵌入Bokeh图进行了很好的讨论:(https://us.pycon.org/2015/schedule/presentation/369/)

#2


0  

Thank you. That worked!

谢谢。那很有效!

in views.py

import pandas as pd
from bokeh.charts import Histogram

def stock_chart(request):
    histo_xyvalues = pd.DataFrame(dict(normal=[1, 2, 3, 1], lognormal=[5, 4, 4, 1]))
    hm = Histogram(histo_xyvalues, bins=5, title='Histogram' , width=300, height=300)
    script, div = components(hm)
    return HttpResponse(script+div)

in urls.py
...
    url(r'^stock_chart/$', views.stock_chart, name="stock_chart"),
...

a call from Javascript/jQuery is,
...
$.get('/stock_chart/', function(data){
     $('#chart_area').append(data);
});


...

推荐阅读
author-avatar
军魂永驻1971
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有