Here’s the complete code:
1 .Button
import os
import random
import numpy as np
import pandas as pd
from bokeh.io import curdoc
from bokeh.models import Panel,ColumnDataSource, CustomAction,CustomJS,RangeTool,MultiLine,FuncTickFormatter,HoverTool, Select, TextInput, Button, Paragraph, PreText,DataTable, TableColumn, PointDrawTool,BoxEditTool,PolyDrawTool,LinearAxis
from bokeh.plotting import figure
from bokeh.layouts import column, row
from pscript import py2js
hover = HoverTool(tooltips=[
("index", "$index"),
("(x,y)", "($x, $y)"),
])
tools_slef=[hover,'box_select,xwheel_zoom,xpan,xwheel_pan,undo,redo,reset']
p_line1 = figure(tools=tools_slef,plot_width=800, plot_height=600, x_range=(0, 100))
# 1.1 #
source_table1 = ColumnDataSource({'x': [], 'y': []})
renderer_table1 = p_line1.scatter(
x='x', y='y', source=source_table1, color='red', size=10)
draw_table1 = PointDrawTool(renderers=[renderer_table1], empty_value=0)
p_line1.add_tools(draw_table1)
p_line1.toolbar.active_tap = draw_table1
# 1.2 #
source_toline1 = ColumnDataSource(dict(
x=[],
y=[],
)
)
glyph_MultiLine = MultiLine(xs="x", ys="y", line_color="red", line_width=2)
p1 = p_line1.add_glyph(source_toline1, glyph_MultiLine)
draw_line1 = PolyDrawTool(renderers=[p1])
p_line1.add_tools(draw_line1)
# 1.3 #
src1 = ColumnDataSource({
'x': [],
'y': [],
'width': [],
'height': [],
})
renderer_line1 = p_line1.rect('x',
'y',
'width',
'height',
source=src1,
alpha=0.3,
color='red'
)
draw_box1 = BoxEditTool(renderers=[renderer_line1], empty_value=0)
p_line1.add_tools(draw_box1)
x1 = np.linspace(0,99, 100)
y1 = np.array([random.randint(-10,10) for _ in range(100)])
source_line1 = ColumnDataSource(data=dict(x=x1,y=y1))
p_line1.line('x', 'y', line_color="navy",line_width=1, source=source_line1)
line1_min = source_line1.data["y"].min()
line1_max = source_line1.data["y"].max()
button_label_line = Button(label="label_line", button_type="success",width=400)
def label_line():
x_line1 = source_table1.data["x"]
len_x_line1 = len(x_line1)
x_lines1 = [[x_line1[i],x_line1[i]] for i in range(len_x_line1)]
y_lines1 = [[line1_min,line1_max] for i in range(len_x_line1)]
source_toline1.data = {'x': x_lines1, 'y': y_lines1}
button_label_line.on_click(label_line)
button_delete_label = Button(label="delete_label", button_type="success",width=400)
def delete_label():
# source_table1.clear()
# source_toline1.clear()
# src1.clear()
source_table1.data = {'x': [], 'y': []}
source_toline1.data = {'x': [], 'y': []}
src1.data = {'x': [], 'y': [], 'width': [], 'height': []}
button_delete_label.on_click(delete_label)
curdoc().add_root(column(row(button_label_line,button_delete_label),p_line1))
2.CustomAction
import os
import random
import numpy as np
import pandas as pd
from bokeh.io import curdoc
from bokeh.models import Panel,ColumnDataSource, CustomAction,CustomJS,RangeTool,MultiLine,FuncTickFormatter,HoverTool, Select, TextInput, Button, Paragraph, PreText,DataTable, TableColumn, PointDrawTool,BoxEditTool,PolyDrawTool,LinearAxis
from bokeh.plotting import figure
from bokeh.layouts import column, row
from pscript import py2js
hover = HoverTool(tooltips=[
("index", "$index"),
("(x,y)", "($x, $y)"),
])
tools_slef=[hover,'box_select,xwheel_zoom,xpan,xwheel_pan,undo,redo,reset']
p_line1 = figure(tools=tools_slef,plot_width=800, plot_height=600, x_range=(0, 100))
# 1.1 #
source_table1 = ColumnDataSource({'x': [], 'y': []})
renderer_table1 = p_line1.scatter(
x='x', y='y', source=source_table1, color='red', size=10)
draw_table1 = PointDrawTool(renderers=[renderer_table1], empty_value=0)
p_line1.add_tools(draw_table1)
p_line1.toolbar.active_tap = draw_table1
# 1.2 #
source_toline1 = ColumnDataSource(dict(
x=[],
y=[],
)
)
glyph_MultiLine = MultiLine(xs="x", ys="y", line_color="red", line_width=2)
p1 = p_line1.add_glyph(source_toline1, glyph_MultiLine)
draw_line1 = PolyDrawTool(renderers=[p1])
p_line1.add_tools(draw_line1)
# 1.3 #
src1 = ColumnDataSource({
'x': [],
'y': [],
'width': [],
'height': [],
})
renderer_line1 = p_line1.rect('x',
'y',
'width',
'height',
source=src1,
alpha=0.3,
color='red'
)
draw_box1 = BoxEditTool(renderers=[renderer_line1], empty_value=0)
p_line1.add_tools(draw_box1)
x1 = np.linspace(0,99, 100)
y1 = np.array([random.randint(-10,10) for _ in range(100)])
source_line1 = ColumnDataSource(data=dict(x=x1,y=y1))
p_line1.line('x', 'y', line_color="navy",line_width=1, source=source_line1)
line1_min = source_line1.data["y"].min()
line1_max = source_line1.data["y"].max()
def label_line():
x_line1 = source_table1.data["x"]
len_x_line1 = len(x_line1)
x_lines1 = [[x_line1[i],x_line1[i]] for i in range(len_x_line1)]
y_lines1 = [[line1_min,line1_max] for i in range(len_x_line1)]
source_toline1.data = {'x': x_lines1, 'y': y_lines1}
def delete_label():
# source_table1.clear()
# source_toline1.clear()
# src1.clear()
source_table1.data = {'x': [], 'y': []}
source_toline1.data = {'x': [], 'y': []}
src1.data = {'x': [], 'y': [], 'width': [], 'height': []}
lable_line_js = py2js(label_line)
delete_lable_js = py2js(delete_label)
Custom_lable_line = CustomAction(action_tooltip = " lable_line ",
icon = "line.png",
callback = CustomJS(code = lable_line_js ))
p_line1.add_tools(Custom_lable_line)
Custom_delete_lable = CustomAction(action_tooltip = " delete_lable ",
icon = "delete.png",
callback = CustomJS(code = delete_lable_js ))
p_line1.add_tools(Custom_delete_lable)
curdoc().add_root(p_line1)