Reputation: 11
After much trouble, I was finally able to get Bokeh's point draw tool to work only to run into another problem: working with the data generated from this tool. The code below generates an interactive plot, and placing/moving points updates the table in real time. However, I cannot figure out how to extract the data in this table, for example, to a csv file. I have tried several strategies such as Excel>>Data>>From Web>>table and using the bs4 library but have had no luck. I do not know anything about html but upon opening the html file generated after using the point draw tool, the data for the initial points, and the points place later with the tool appear to be in separate places. This may be the cause of the problem. Basically, I just need to get the data from the html table into a format that I can easily utilize in python. Thank you for any help you can provide.
SOURCE CODE (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)
Upvotes: 0
Views: 1183
Reputation: 11
I ended up just writing the below function to extract the new vertices generated from the point draw tool from the HTML file itself. A tip to anyone else in this predicament, make sure you increase the size of the displayed table such that all vertices are displayed at one time (no vertical scroll bar). Otherwise vertices are omitted from the HTML file and wont be found/extracted by the function.
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
Upvotes: 1