Skip to content

Add support for formatters to ValueRef#13650

Merged
mattpap merged 11 commits intobranch-3.5from
mattpap/13599_ValueRef_formatters
Jun 14, 2024
Merged

Add support for formatters to ValueRef#13650
mattpap merged 11 commits intobranch-3.5from
mattpap/13599_ValueRef_formatters

Conversation

@mattpap
Copy link
Contributor

@mattpap mattpap commented Jan 17, 2024

Example (side-by-side comparison):

Screenshot from 2024-01-17 15-12-58

import numpy as np

from bokeh.models import HoverTool, Styles
from bokeh.models.dom import Div, Index, ValueRef
from bokeh.palettes import Spectral11
from bokeh.plotting import figure, show

N = 1000
x = np.random.random(size=N) * 100
y = np.random.random(size=N) * 100
radii = np.random.random(size=N) * 1.5
colors = np.random.choice(Spectral11, size=N)

p = figure(
    title="Demonstrates hover tool with advanced and regular tooltip formatting side-by-side",
    tools="pan,wheel_zoom,box_select,crosshair",
)

p.circle(x, y, radius=radii, fill_color=colors, fill_alpha=0.6, line_color=None)

grid = Div(
    style=Styles(
        display="grid",
        grid_template_columns="auto auto",
        column_gap="10px",
    ),
    children=[
        "index:",  Div(children=["#", Index()]),
        "(x, y):", Div(children=["(", ValueRef(field="x"), ", ", ValueRef(field="y"), ")"]),
        "radius:", ValueRef(field="radius", format="%.2f", formatter="printf"),
    ],
)

hover_advanced = HoverTool(
    description="Advanced hover",
    tooltips=grid,
    attachment="left",
)
p.add_tools(hover_advanced)

hover_regular = HoverTool(
    description="Regular hover",
    tooltips=[
        ("index", "$index"),
        ("(x,y)", "(@x, @y)"),
        ("radius", "@radius{%.2f}"),
    ],
    formatters={
        "@radius": "printf",
    },
    attachment="right",

)
p.add_tools(hover_regular)

show(p)

fixes #13599

@mattpap mattpap added this to the 3.4 milestone Jan 17, 2024
@mattpap
Copy link
Contributor Author

mattpap commented Jan 17, 2024

When working on this I noticed that the template case only renders tooltip entry for the last selected index, not all of the selected indices as in the default (tuple) case.

@codecov
Copy link

codecov bot commented Jan 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.59%. Comparing base (e61219b) to head (b5ba116).
Report is 70 commits behind head on branch-3.5.

Current head b5ba116 differs from pull request most recent head 057f06e

Please upload reports for the commit 057f06e to get more accurate results.

Additional details and impacted files
@@              Coverage Diff               @@
##           branch-3.5   #13650      +/-   ##
==============================================
+ Coverage       91.57%   92.59%   +1.02%     
==============================================
  Files             326      325       -1     
  Lines           20737    20733       -4     
==============================================
+ Hits            18990    19198     +208     
+ Misses           1747     1535     -212     

@mattpap mattpap force-pushed the mattpap/13599_ValueRef_formatters branch from 5e5d5b8 to 5bf80e4 Compare January 18, 2024 01:05
@mattpap mattpap force-pushed the mattpap/13599_ValueRef_formatters branch 2 times, most recently from 5606cb2 to fbc9362 Compare February 20, 2024 01:13
@mattpap mattpap force-pushed the mattpap/13599_ValueRef_formatters branch 2 times, most recently from fe501ce to b5ba116 Compare February 28, 2024 08:51
@mattpap mattpap modified the milestones: 3.4, 3.5 Feb 28, 2024
@mattpap mattpap changed the base branch from branch-3.4 to branch-3.5 March 14, 2024 16:50
@mattpap mattpap force-pushed the mattpap/13599_ValueRef_formatters branch from b5ba116 to bbe2610 Compare May 30, 2024 10:05
@mattpap mattpap force-pushed the mattpap/13599_ValueRef_formatters branch from bbe2610 to 89903b7 Compare June 13, 2024 09:09
@mattpap
Copy link
Contributor Author

mattpap commented Jun 13, 2024

This is tentatively ready. Some more unit testing will follow.

@mattpap mattpap requested a review from hoxbro June 13, 2024 09:11
),
children=[
"index:", Div(children=["#", Index()]),
"(x, y):", Div(children=["(", ValueRef(field="x"), ", ", ValueRef(field="y"), ")"]),
Copy link
Contributor

Choose a reason for hiding this comment

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

Just thinking out loud here, having a convenience class like this could make sense to me.

        "(x, y):", Div(children=[ValueRefs("({x}, {y})", fields=["x", "y"])]),

or

        "(x, y):", ValueRefs("({x}, {y})", fields=["x", "y"]),

Likely out-of-scope for this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ValueRef is a low-level API meant for having full control over formatting, instead of relying on an ad-hoc syntax. However, most of the time users will choose the syntax out of convenience. Thus I don't see a need for additional convenience here. If only PEP 501 (general purpose string interpolation) wasn't deferred, then this could be made really convenient while retaining complete control.

column_gap="10px",
),
children=[
"index:", Div(children=["#", Index()]),
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add a style to this Div.

I feel like this example shows the API but not why to use it, which is a bit sad.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is also a bit much. I will see if I can simplify this with HTML() model.

Copy link
Contributor Author

@mattpap mattpap Jun 13, 2024

Choose a reason for hiding this comment

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

The "improved" example is still quite much, but is shows styling and better shows how to reference things.

@mattpap mattpap force-pushed the mattpap/13599_ValueRef_formatters branch from 89903b7 to 057f06e Compare June 13, 2024 17:21
@mattpap
Copy link
Contributor Author

mattpap commented Jun 13, 2024

With some styling:
image

@mattpap mattpap merged commit c93d808 into branch-3.5 Jun 14, 2024
@mattpap mattpap deleted the mattpap/13599_ValueRef_formatters branch June 14, 2024 14:22
Chiemezuo pushed a commit to Chiemezuo/bokeh that referenced this pull request Aug 27, 2024
* Add support for formatters to ValueRef

* Robustify handling of templatable DOM trees

* Allow multiple tooltip entries by cloning

* Rename FormatterType -> BuiltinFormatter

* Add interaction/tools/hover_tooltip-advanced.py

* Add visual integration tests

* Add MDN links to ValueRef.formatter docstring

* Update a unit test description

* Make HTML inherit from DOMElement

* Use HTML model and styling in the example

* Added release notes
@github-actions
Copy link

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 Oct 25, 2024
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.

[FEATURE] Support formatters when using Template as HoverTool

2 participants