Skip to content
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

Pass number format to folium legend #2014

Open
TenThumbsWPG opened this issue Oct 21, 2024 · 2 comments
Open

Pass number format to folium legend #2014

TenThumbsWPG opened this issue Oct 21, 2024 · 2 comments
Labels
enhancement Feature request or idea about how to make folium better

Comments

@TenThumbsWPG
Copy link

We would like to use folium to create choropleths in more than one language. So far (unless I am missing something), it appears that folium is using the system localization to set the language format for numbers in folium legend.

We, and I anticipate others, have more than one use for these maps:

  1. Online as the intended HTML output
  2. Print versions (digitally as images, parts of PDF files and also hard copies).

The problem is that in order to change number formats (in this case comma vs. period for decimal), the language localization needs to be reset globally, in most cases requiring a kernel reset (again, unless I am missing something).

I would like to be able to pass a number format to the legend to change the language format of the numbers in the scale, either by:

  1. passing a format string, or
  2. specifying a localization that folium can then reference and set the text accordingly
  3. Have some documentation on how to inject an equivalent instruction by HTML.

Ultimately, I would like to do the following in a single code execution:

  1. Create folium map (language localization unimportant since the html will use the localization of the system it is hosted on)
  2. Change/re-render choropleths to two languages (with number formats) so that images can be saved for alternate media use

Since headerless browser can be used to generate images and text can be passed in any language, the only thing that is needed is to interact with the format of the numbers in the legend scale.

@Conengmo
Copy link
Member

My first thought is something you could try right now: to change the format with Javascript after the page is rendered. You could do something like get the css selector for the element with the date, parse it, change the locale, then update the element.

You can add arbitrary Javascript to your Folium map. I’m on mobile now so don’t have a link for you, but there are examples in our Isssues, maybe also docs.

For a more clean solution, having the locale as option when creating a choropleth, or as an argument on colormap… I’m not sure right now, could be interesting. Probably exposing it on colormap in the Branca library is the way to go for that. Choropleth is already quite crowded so I wouldn’t add more arguments there, but through the choropleth object more advanced users could access the colormap object.

We don’t really have dev capacity, so if you manage to find something that works for you please share it here :)

@Conengmo Conengmo added the enhancement Feature request or idea about how to make folium better label Oct 22, 2024
@TenThumbsWPG
Copy link
Author

I have looked at the HTML generated. There are a few things I can do for the <style>, such as add font size and weight. I cannot seem to add a "lang:" argument, even though this should be allowed within a <style>. This is likely where it would have to go.

The other option would be to set a language directly in the text line for the legend, but unfortunately this is set as a variable that is in turn fed into the javascript for the associated d3 script (<script src="https://d3js.org/d3.v4.min.js"></script>). This may mean any change here needs to involve changing this script (which is hard-coded into the addon). It may not be possible to pass a format code along with the legend text if the JS is expecting a string with only the legend text to insert into a format defined therein...

I am not good enough at HTML to know how/if I can inject some additional style script to change the number format on the legend scale... I will keep hunting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature request or idea about how to make folium better
Projects
None yet
Development

No branches or pull requests

2 participants