Skip to content

Add support for grouping tools in plots#14259

Merged
mattpap merged 2 commits into
branch-3.7from
mattpap/5497_group_tools
Feb 12, 2025
Merged

Add support for grouping tools in plots#14259
mattpap merged 2 commits into
branch-3.7from
mattpap/5497_group_tools

Conversation

@mattpap

@mattpap mattpap commented Jan 28, 2025

Copy link
Copy Markdown
Contributor

Allows to group common tools in plots and figures (in bokehjs). Adds new Toolbar.group and Toolbar.group_types properties. See the new example/interaction/tools/tool_grouping.py example:

Screencast.from.2025-01-31.10-52-08.webm

fixes #5497

@mattpap mattpap added this to the 3.7 milestone Jan 28, 2025
@mattpap mattpap force-pushed the mattpap/5497_group_tools branch 2 times, most recently from f39d709 to 66cff8e Compare January 31, 2025 09:53
@mattpap mattpap requested a review from hoxbro January 31, 2025 09:55
@mattpap

mattpap commented Jan 31, 2025

Copy link
Copy Markdown
Contributor Author

Tentatively ready for review. I need to add tests.

@hoxbro hoxbro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TapTool does not seem to behave as expected:

Screencast.From.2025-02-03.16-41-34.mp4
code
from bokeh.layouts import column, row
from bokeh.models import CustomJS, Div, HoverTool, Switch, TapTool
from bokeh.plotting import figure, show

plot = figure(width=400, height=400, tools=["pan", "box_select", "save", "reset"])

cr1 = plot.scatter([1, 2, 3], [3, 3, 3], size=30, color="red")
cr2 = plot.scatter([1, 2, 3], [2, 2, 2], size=30, color="green")
cr3 = plot.scatter([1, 2, 3], [1, 1, 1], size=30, color="blue")

plot.add_tools(HoverTool(renderers=[cr1], description="Hover over red"))
plot.add_tools(HoverTool(renderers=[cr2], description="Hover over green"))
plot.add_tools(HoverTool(renderers=[cr3], description="Hover over blue"))

plot.add_tools(TapTool(renderers=[cr1], description="Tap on red"))
plot.add_tools(TapTool(renderers=[cr2], description="Tap on green"))
plot.add_tools(TapTool(renderers=[cr3], description="Tap on blue"))

text = Div(text="Group tools:")
switch = Switch(active=plot.toolbar.group)
switch.js_on_change("active", CustomJS(args=dict(plot=plot), code="""
export default ({plot}, {active: group}) => {
    plot.toolbar.group = group
}
"""))
layout = column([row([text, switch]), plot])

show(layout)

Also, is there a way to group two out of three hover tools? It's a bit hard to tell if this is the case.

Comment thread bokehjs/src/lib/core/has_props.ts
Comment thread bokehjs/src/lib/models/tools/toolbar.ts Outdated
Comment thread src/bokeh/models/tools.py Outdated
@mattpap

mattpap commented Feb 3, 2025

Copy link
Copy Markdown
Contributor Author

TapTool does not seem to behave as expected:

I forgot that you can't have multiple tap tools active at the same time (issue #3804). I suppose I will fix that next.

@mattpap mattpap force-pushed the mattpap/5497_group_tools branch from 66cff8e to 4cacc50 Compare February 3, 2025 17:24
@mattpap

mattpap commented Feb 3, 2025

Copy link
Copy Markdown
Contributor Author

Also, is there a way to group two out of three hover tools? It's a bit hard to tell if this is the case.

Currently not. Any suggestions regarding the API for this?

@hoxbro

hoxbro commented Feb 4, 2025

Copy link
Copy Markdown
Contributor

Any suggestions regarding the API for this?

At the top of my head, it would be something like this:

plot.add_tools(HoverTool(renderers=[cr1], description="Hover over red", group="one"))
plot.add_tools(HoverTool(renderers=[cr2], description="Hover over green", group="one"))
plot.add_tools(HoverTool(renderers=[cr3], description="Hover over blue", group="two"))

The same group (it could be another name) for the tool would be merged. If group is not added, it would merge all.

@mattpap

mattpap commented Feb 4, 2025

Copy link
Copy Markdown
Contributor Author

The same group (it could be another name) for the tool would be merged. If group is not added, it would merge all.

I was going to do exactly that.

@mattpap mattpap force-pushed the mattpap/5497_group_tools branch from 4cacc50 to 2f34e02 Compare February 4, 2025 11:11
@mattpap

mattpap commented Feb 4, 2025

Copy link
Copy Markdown
Contributor Author

I added support for Tool.group and removed "tap" from Toolbar.group_types for now:

Screencast.from.2025-02-04.12-14-49.webm

I also added support for indication of the number of tools being grouped.

@hoxbro

hoxbro commented Feb 5, 2025

Copy link
Copy Markdown
Contributor

I also added support for indication of the number of tools being grouped.

I think this should be disabled by default (if it is not already). From a user perspective, it would confuse as there is nothing the user can do with the information.

@mattpap mattpap force-pushed the mattpap/5497_group_tools branch 2 times, most recently from 6683a65 to 998b396 Compare February 5, 2025 12:10
@mattpap

mattpap commented Feb 5, 2025

Copy link
Copy Markdown
Contributor Author

I think this should be disabled by default (if it is not already). From a user perspective, it would confuse as there is nothing the user can do with the information.

This gives the user indication that they work with multiple tools at once. This will be more useful when I will implement implement partial active state (for when only a subset of tool group's tools is active) and a menu with access to individual tools. Note that this doesn't affect proxies in grid plot (etc.) toolbars.

@mattpap mattpap force-pushed the mattpap/5497_group_tools branch 4 times, most recently from 8d59acf to a38c0ba Compare February 9, 2025 23:34
@mattpap

mattpap commented Feb 9, 2025

Copy link
Copy Markdown
Contributor Author

I hid the tool tool counter for now. I will revisit this when implementing individual tool access in tool group buttons via a menu.

@mattpap mattpap force-pushed the mattpap/5497_group_tools branch from a38c0ba to 4abde69 Compare February 10, 2025 21:24

@hoxbro hoxbro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Very happy to see the added comments in the code 👍

cr4 = plot.scatter([1, 2, 3], [2, 2, 2], size=30, color="purple")
cr5 = plot.scatter([1, 2, 3], [1, 1, 1], size=30, color="yellow")

plot.add_tools(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
plot.add_tools(
# The description will be shown when hovering over the tool icon
plot.add_tools(

@mattpap mattpap merged commit 6eff773 into branch-3.7 Feb 12, 2025
@mattpap mattpap deleted the mattpap/5497_group_tools branch February 12, 2025 15:00
@github-actions

Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators May 28, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Group hover tool toolbar icons

2 participants