现在时间是:
当前位置:首 页 >> 网站开发>> 文章列表

在Flask中嵌入Bokeh图表

作者:   发布时间:2017-03-07 17:16:04   浏览次数:1412
from bokeh.embed import components 

script, div = components(plot)
return render_template('graph.html', script=script, div=div)
# html格式
<html>
<head>
<link rel="stylesheet" href="http://cdn.pydata.org/bokeh/release/bokeh-0.12.0.min.css" type="text/css" />
<script type="text/javascript" src="http://cdn.pydata.org/bokeh/release/bokeh-0.12.0.min.js"></script>
{{ script | safe }}
</head>
<body>
<div class=page>
<h1>Header text</h1>
{{ div | safe }}
</div>
</body>
</html>

注意bokeh的安装版本和JS以及CSS的版本要对应

 

当然也可以引入Bootstrap

 

{% extends "bootstrap/base.html" %}
{% block metas %}
    {{ super() }}
    <meta charset="utf-8">
<meta name="description" content="努力尝试一种新的营销模式,这里是数据化第一站,谢谢访问">
{% endblock %} {% block title %}海豹战队{% endblock %} {% block head %} {{ super() }} <script type="text/javascript" src="http://cdn.pydata.org/bokeh/release/bokeh-0.12.7.min.js"></script>
<!-- COPY/PASTE SCRIPT HERE -->
{{ script|safe }} {% endblock %} {% block styles %} {{ super() }} <link rel="stylesheet" href="http://cdn.pydata.org/bokeh/release/bokeh-0.12.7.min.css" type="text/css"/>
{% endblock %} {% block content %} <!-- INSERT DIVS HERE -->
<div style="margin-left: 30%">  {{ div|safe }} </div>
{% endblock %}
@haibaoweb.route('/graph', methods=['GET', 'POST'])
def graph():
post_id = request.args.get("postid") # 从地址获取post_id
from bokeh.embed import components from bokeh.charts import Line, show, output_file # build a dataset where multiple columns measure the same thing
data = dict(python=[2, 3, 7, 5, 26, 221, 44, 233, 254, 265, 266, 267, 120, 111],
pypy=[12, 33, 47, 15, 126, 121, 144, 233, 254, 225, 226, 267, 110, 130],
jython=[22, 43, 10, 25, 26, 101, 114, 203, 194, 215, 201, 227, 139, 160],
test=['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar',
'foo', 'bar', 'foo', 'bar']
)

# create a line chart where each column of measures receives a unique color and dash style
line = Line(data, y=['python', 'pypy', 'jython'],
dash=['python', 'pypy', 'jython'],
color=['python', 'pypy', 'jython'],
legend_sort_field = 'color',
legend_sort_direction = 'ascending',
title="Interpreter Sample Data", ylabel='Duration', legend=True, tooltips=[('series', '@series')])

script, div = components(line)
print(script)
print(div)
# print(div)
# import re
# div_list = re.findall('<div class="bk-plotdiv".*?div>',str(div))
# div = '<div class="bk-root">'+ '<div class="bk-root">'.join(div_list)
# print(script, div)
return render_template('haibao/graph.html',script=script, div=div)

 







上一篇:没有了    下一篇:没有了

Copyright ©2018    易一网络科技|www.yeayee.com All Right Reserved.

技术支持:自助建站 | 领地网站建设 |短信接口 版权所有 © 2005-2018 lingw.net.粤ICP备16125321号 -5