阅读量:143
要创建可供用户下载或导出的报告或图表数据,您可以使用Bokeh的导出功能。以下是一个简单的示例代码,演示如何使用Bokeh创建一个图表,并提供下载按钮以将图表数据导出为CSV文件:
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource, Button
from bokeh.layouts import column
from bokeh.io import output_file
import pandas as pd
# 创建数据
data = {'x': [1, 2, 3, 4, 5],
'y': [6, 7, 2, 4, 5]}
df = pd.DataFrame(data)
# 创建ColumnDataSource
source = ColumnDataSource(df)
# 创建图表
p = figure(title="Simple Line Plot", x_axis_label='x', y_axis_label='y')
p.line('x', 'y', source=source)
# 创建下载按钮
button = Button(label="Download CSV")
button.callback = CustomJS(args=dict(source=source), code="""
const data = source.data;
const filetext = 'x,y\\n';
for (let i = 0; i < data xss=removed xss=removed xss=removed csv;charset=utf-8;' xss=removed xss=removed xss=removed xss=removed>)
# 输出到HTML文件
output_file("plot.html")
# 显示图表和下载按钮
show(column(p, button))
运行上述代码后,将会生成一个包含图表和下载按钮的HTML文件。当用户点击下载按钮时,将会自动下载包含图表数据的CSV文件。
您还可以根据需要定制下载功能,以实现更复杂的数据导出需求。希望这个示例能够帮助您创建可供用户下载或导出的报告或图表数据。