hey assistent I have this DMAX_source = ColumnDataSource(
data=dict(x=[x_DMAX[-1]], y=[y_DMAX[-1]], label=["Dmax information"])
)
p.scatter([x_DMAX[-1]], [y_DMAX[-1]], color="orange", size=10, marker="asterisk")
label_decel = Label(
x=x_DMAX[-1],
y=y_DMAX[-1],
text="Dmax",
text_color="orange",
text_align="center",
text_font_size="10pt",
x_offset=0,
y_offset=10,
) I need to be able to click this button and show info about it
Assistant
To enable an interactive feature within your plot using Bokeh, you can integrate a callback that responds to clicks on your scatter points or associated labels. Here's how you could implement it using a CustomJS callback:
Setup your ColumnDataSource and Plot: You have already done most of the setup. Your plot includes the scatter plot with a label.
Create a CustomJS Callback: This JavaScript callback will be executed when you click on the scatter point. It will access and display the desired information about the point.
Add a TapTool to the Plot: This tool allows you to register click events on your plot.
Here is your code updated with a CustomJS interaction:
python
from bokeh.models import ColumnDataSource, Label, CustomJS, TapTool
from bokeh.plotting import figure, show
# Assume x_DMAX and y_DMAX are previously defined lists of coordinates
# Example data
x_DMAX = [10, 20, 30]
y_DMAX = [15, 25, 35]
# 1. Create a ColumnDataSource
DMAX_source = ColumnDataSource(data=dict(x=[x_DMAX[-1]], y=[y_DMAX[-1]], label=["Dmax information"]))
# 2. Create a plot
p = figure(title="Click on the point for more info", tools="tap")
# 3. Scatter the most recent point
p.scatter('x', 'y', source=DMAX_source, color="orange", size=10, marker="asterisk")
# 4. Add a label to the same point
label_decel = Label(
x=x_DMAX[-1],
y=y_DMAX[-1],
text="Dmax",
text_color="orange",
text_align="center",
text_font_size="10pt",
x_offset=0,
y_offset=10,
)
p.add_layout(label_decel)
# 5. Create a CustomJS callback to show information about clicked points
callback = CustomJS(args=dict(source=DMAX_source), code="""
const selected_index = source.selected.indices[0];
if (selected_index != null) {
const data = source.data;
const x = data['x'][selected_index];
const y = data['y'][selected_index];
const label = data['label'][selected_index];
alert(`Point clicked:\\nX: ${x}\\nY: ${y}\\nLabel: ${label}`);
}
""")
# 6. Add callback to the TapTool
taptool = TapTool(callback=callback)
p.add_tools(taptool)
# 7. Display the figure
show(p)
Explanation:
ColumnDataSource (DMAX_source): Keeps track of the data for the scatter point and its label.
CustomJS Callback: Alerts information about the selected point when it is clicked.
TapTool: Allows the plot to capture click events, triggering the callback.
JavaScript Alert: When a point is clicked, an alert box pops up showing the point's x, y coordinates and label information.
This approach assumes you are using a Python environment such as Jupyter Notebook or a server capable of running Bokeh applications. Make sure your Bokeh environment is set up correctly for this interactivity to work.