如何从Bokeh Point Draw Tool中提取数据-生成的html表用于python

时间:2018-09-01 19:31:03

标签: python html bokeh

在经历了很多麻烦之后,我终于能够使Bokeh的点绘图工具能够工作,但又遇到了另一个问题:使用从该工具生成的数据。下面的代码生成一个交互式绘图,并且放置/移动点会实时更新表格。但是,我无法弄清楚如何将该表中的数据提取到例如csv文件中。我尝试了几种策略,例如Excel >> Data >> From Web >> table,并使用bs4库,但是没有运气。我对html一无所知,但是在打开使用点绘制工具后生成的html文件时,初始点的数据以及该工具以后放置点的数据似乎位于单独的位置。这可能是问题的原因。基本上,我只需要将html表中的数据转换成可以在python中轻松使用的格式。感谢您提供的任何帮助。

源代码(python3.6.2)

from datetime import date
import time
from random import randint

from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models import ColumnDataSource, PointDrawTool
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn
from bokeh.plotting import figure

output_file("pointDrawResults.html")

data = { "x": [5, 2, 8], "y": [5, 7, 8]}

src = ColumnDataSource(data)

columns = [
        TableColumn(field = "x", title = "xs"),
        TableColumn(field = "y", title = "ys"),
        ]
data_table = DataTable(source = src, columns = columns, width = 400, height = 280)

plot = figure(x_range = (0, 10), y_range = (0, 10), width = 400, height = 400, title = "Point Draw Tool")

renderer = plot.circle("x", "y", size = 15, fill_color = "blue", fill_alpha = 0.8, source = src)

draw_tool = PointDrawTool(renderers = [renderer], empty_value = 1)
plot.add_tools(draw_tool)
plot.toolbar.active_drag = draw_tool

show(widgetbox(data_table))
show(plot)

1 个答案:

答案 0 :(得分:0)

我最终只编写了以下函数,以从HTML文件本身提取从点绘制工具生成的新顶点。给这个困境中的其他任何人一个提示,请确保您增加显示的表的大小,以便一次显示所有顶点(没有垂直滚动条)。否则,将从HTML文件中省略顶点,并且该函数将找不到/提取顶点。

import re

def extractHtmlVerts(self):
        indexList = []

        with open(self.vertexFileE.get(), 'r') as htmlFile:
            htmlCont = htmlFile.read()

        tokens = re.finditer("(\d+\.\d+)</span>", htmlCont)

        type = "x"
        for match in tokens:
            if type == "x":
                indexList.append(round(float(match.group(1))))
                type = "y"
            else:
                type = "x"

        return indexList