-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[Feat] tooltip template syntax #9551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
While this lays out a bunch of options, I think this is missing a discussion about how it fits in with the existing templating in |
I envision that we build a solid templating feature We should use it wherever we want to support templating as long as we can do so without breaking existing code (needs to be checked for getTooltip).
Good question. I looked for existing templating code but could not find it. If we have it, it could be one of the candidates. PS -A big motivation for having a strong templating feature in core is that AI assistants can easily generate inline deck.gl visualizations as long as they are declarative. |
http://deck.gl/docs/api-reference/json/conversion-reference If I remember correctly, @donmccurdy wrote the library many moons ago and it was eventually in-sourced |
You are referring to the expression evaluation library used to write simple declarative "callbacks" for accessors. That's not a string templating library in the way other such systems work, though it could perhaps be used that way if extended with formatting primitives - or perhaps combined with a templating library to add more powerful capabilities. |
Could you provide some examples of what you'd like to render? I could be misunderstanding something since I don't have a lot of experience using the expression system. I think the json module does a basic form of string templating in these examples. For tooltip it could look something like I'm mainly mentioning it for completeness and can poke holes at its limitations. |
If you have data with price and itemname fields, you would use a feature similar to Plotly's hovertemplate like this new InfoWidget({
mode: 'hover',
layerId: 'item-price-layer',
template: ' <b>%{itemname}</b> <i>Price</i>: $%{y:.2f}'
}) The Update: We'd probably need to add a |
Or maybe we could support multiple layers: new InfoWidget({
mode: 'hover',
layerTemplates: {
'item-price-layer': ' <b>%{itemname}</b> <i>Price</i>: $%{y:.2f}'
}
}) |
Target Use Case
Declarative use of tooltips and new "PopupWidget". Currently requires javascript to build the HTML string.
Use of javascript options should of course still be supported.
Proposal
Below is a summary that covers popular templating systems for data‐driven tooltips—focusing on those familiar to both JavaScript and Python data visualization users. This summary highlights each system’s syntax and, in particular, details their numeric formatting capabilities. We also include a section on Plotly’s hovertemplate and a look at systems built around d3-format.
Templating Systems for Data-Driven Tooltips in Deck.gl
A Comparative Overview for JavaScript & Python
Data visualization libraries often rely on declarative templating systems to embed data into tooltips or labels. Many systems are popular in both the JavaScript and Python ecosystems. Below are several approaches that have been widely adopted by data scientists and visualization experts:
Each system provides a different balance of declarative syntax, inline formatting power (especially for numbers), and integration complexity.
Plotly’s Hovertemplate
Overview:
Plotly’s hovertemplate is used to define custom, data-driven tooltips in Plotly charts. Its syntax is entirely declarative and designed for inline formatting.
Syntax & Formatting Options:
%{variable}
to insert a data value.%{value:.2f}
displays a number with two decimal places.%{value:,}
adds thousand separators.%{date:%Y-%m-%d}
formats a date using a custom format.Familiarity:
Widely used in both Plotly.py (Python) and Plotly.js (JavaScript), making it very intuitive for users coming from data visualization backgrounds.
Mustache.js (Logic-less Templates)
Overview:
Mustache is known as a “logic-less” templating system using double curly braces (
{{...}}
). It is favored for its simplicity and minimalism.Syntax & Formatting Options:
{{value}}
simply replaces the placeholder with the corresponding value.{{#condition}}...{{/condition}}
to render content only if a value is truthy.Familiarity:
Its syntax is similar to templating languages like Jinja2 (Python) and is popular in lightweight web applications. However, its lack of inline formatting may require extra preprocessing when precise numeric presentation is needed.
Handlebars.js (Enhanced Mustache)
Overview:
Handlebars extends Mustache by adding control structures (such as
if/else
and loops) and, importantly, custom helpers that enable inline formatting.Syntax & Formatting Options:
{{value}}
Familiarity:
The syntax is close to Mustache and Jinja2, making it accessible to both JavaScript and Python users. Its additional power comes at the cost of a slightly larger library size.
Lodash _.template (ERB-Style Templates)
Overview:
Lodash’s templating function uses ERB-style delimiters to allow embedding arbitrary JavaScript code. This approach grants full access to JavaScript’s formatting methods.
Syntax & Formatting Options:
<%= value %>
for variable interpolation (automatically HTML-escaped).<% ... %>
for arbitrary JavaScript code.'<%= value.toFixed(2) %>'
'<%= d3.format(".2f")(value) %>'
Familiarity:
This approach is very powerful for developers comfortable with JavaScript code, but its “code-heavy” nature may feel less declarative compared to other systems.
Python: Jinja2
Overview:
Jinja2 is the go-to templating engine for Python, used extensively in web frameworks like Flask and in data visualization projects.
Syntax & Formatting Options:
{{ variable }}
to insert values.{% if %}
,{% for %}
, etc.Familiarity:
Jinja2’s syntax is similar to Handlebars and Mustache, making it very familiar to Python developers and data scientists. Its robust filtering system makes inline formatting straightforward.
D3-format–Based Custom Templating
Overview:
While there isn’t a standalone templating engine built solely around d3-format, it’s common to integrate d3-format into existing templating systems (such as Handlebars or Lodash templates) to achieve powerful numeric formatting.
Approach & Formatting Options:
d3.format(".2f")
for two decimal places,d3.format(",")
for thousand separators).'<%= d3.format(".2f")(value) %>'
%{value:.2f}
) and internally routes numeric placeholders through d3.format. This would give users a familiar, concise format specification without the overhead of a full templating engine.Familiarity:
For users already comfortable with d3 and its formatting capabilities (common in data visualization workflows), this approach can be very appealing. It bridges the gap between declarative configuration (as in Plotly) and the power of JavaScript’s formatting functions.
Comparison Table
%{value:.2f}
%{value:,}
or%{date:%Y-%m-%d}
){{value}}
{{format value ".2f"}}
<%= value.toFixed(2) %>
or<%= d3.format(".2f")(value) %>
floatformat
or custom filters) allow similar formatting to Plotly (e.g., `{{ value{{d3format value ".2f"}}
(via Handlebars or Lodash integration)d3.format(".2f")
) to provide advanced numeric formatting (percentages, SI prefixes, etc.)Conclusion
For Deck.gl tooltips, where a declarative, data-driven approach is desired, the choice of templating system depends on the desired balance between simplicity and inline formatting power:
floatformat
) and extensibility through custom filters, making it very familiar and powerful for Python data scientists.Choosing the right system for Deck.gl will depend on your user base and the complexity of the formatting required. For many applications, adopting a Plotly hovertemplate–like syntax or integrating a Handlebars/Jinja2 style with d3-format helpers may offer the best mix of declarative simplicity and robust inline formatting.
The text was updated successfully, but these errors were encountered: