-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Support python-like token replacement and formatting #3358
Conversation
Does https://github.com/davidchambers/string-format support formatting numbers? Skimming through code/tests, I can't seem to find anything about it. |
@mourner ah nuts, you're right - I was accidentally using a branch davidchambers/string-format#2 locally and I forgot it wasn't in the published module yet. |
@danpat I'm worried that this PR is 2.5 years old without any signs of life or reaction from the maintainer. I think we should look for a different, better-maintained module. |
We're in general going to be reluctant to extend the semantics of the interpolation "mini-language", per mapbox/mapbox-gl-style-spec#104. In particular, we know that people are already using token names that include a literal colon character, per mapbox/mapbox-gl-style-spec#362, so changes such as this would likely be backwards-incompatible in those cases. |
@jfirebaugh what do you think about adding a @danpat BTW, is this reproducible for any |
Ok, the existence of @mourner yes, the same problem occurs with I could work around this by pre-formatting the numbers as string properties, but that's putting formatting logic in the tile generation - that's something that should be decided at render time IMO. Definitely open to other suggestions for specifying numeric precision. @mourner You're suggesting something like this ?
Configurable on a per-layer basis would work pretty well I think. I can definitely see problems in the future if it's a style-global value. |
—> #3361 |
I wonder what it'd look like to implement this functionality via mapbox/mapbox-gl-function#28 |
Launch Checklist
This PR introduces advanced (Python
str.format()
-like) token formatting when using{fieldname}
string replacement.The change is backwards compatible with existing syntax, we very conveniently (deliberately?) chose the curly-bracket syntax that is basically compatible with Python's
str.format()
.The primary motivation for this change is number formatting:
float
anddouble
fields encoded in vector tiles are currently unformatted. Values like0.1
encoded as afloat
currently end up as text labels like0.100000005932
. This change allows you to specify number formatting precision in the Python syntax, like:{float_field::.2}
for 2 decimal places.There is an alternatively library at https://github.com/xfix/python-format, which seems to have better test coverage, but does not support using dictionary keys as field names.