You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support function objects in Leaflet options. #1943
Is your feature request related to a problem? Please describe.
This is not so much a problem as rather a cleanup. All leaflet objects can be configured using an options dictionary. Many of these objects allow javascript functions inside options. So far in Folium, these javascript functions were typically implemented in the folium templates and could not be supplied by users. (With a few exceptions).
Describe the solution you'd like
We can use JsCode to allow users to create javascript functions as part of the configuration. Several Layers could benefit from this addition. For instance: folium.GeoJson could be extended with methods for on_each_feature and point_to_layer. By adding javascript method handlers, we increase the flexibility of the user. It would allow the user more interactivity. E.g. showing something else in a popup than just the properties of a geojson layer, or adding new shapes.
Describe alternatives you've considered
The main alternative is that we do nothing. This has the advantage of keeping Folium simple. However, the extra power and flexibility outweigh the additional complexity. (At least in my view). It would probably be best to document these as advanced features.
Additional context
I noted that in several locations, the folium objects generate their own implementations of javascript options. E.g. folium.GeoJson generates an on_each_feature object if a popup or tooltip is configured. We would need to decide how to handle this. The easiest solution would be to disallow certain combinations. Staying with the GeoJson example, if you pass on_each_feature you cannot also pass a popup or tooltip argument.
Implementation
folium is maintained by volunteers. Can you help make a PR if we want to implement this feature?
It's something different from #1856. To a certain extent it is about allowing any Folium class' options have JsCode arguments. And you are right, it would involve touching a lot of templates, but the work can be subdivided into smaller steps.
As an example: if one looks at the L.GeoJsondocumentation, the user can provide functions for pointToLayer and onEachFeature. This is currently not possible in Folium.
I would like the user to be able to create a folium.GeoJson object with point_to_layer and on_each_feature function objects.
Step 1
Go through all Folium children of branca.Element and make sure that when the class accepts a **kwargs argument that is passed to the Leaflet class, it will check if any of the arguments is a JsCode object and convert it accordingly. Some Folium classes generate their own version of these javascript function objects. Like GeoJson. In that case ensure that the one from **kwargs cannot blindly override the one generated in the Folium template.
Step 2
Go through all of the Folium children of branca.Element that have a corresponding Leaflet class. If the Leaflet class documents function objects, add these as explicit arguments to the Folium class __init__ method and document them.
Activity
Conengmo commentedon May 18, 2024
I'm not sure if I completely follow, could you maybe give a short code snippet as an example?
Is this about allowing any Folium
optionsto haveJsCodevalues? I would like that, but not sure how (without touching every template).Update: is this maybe what you mean? #1856
hansthen commentedon May 18, 2024
It's something different from #1856. To a certain extent it is about allowing any Folium class'
optionshaveJsCodearguments. And you are right, it would involve touching a lot of templates, but the work can be subdivided into smaller steps.As an example: if one looks at the
L.GeoJsondocumentation, the user can provide functions forpointToLayerandonEachFeature. This is currently not possible in Folium.I would like the user to be able to create a
folium.GeoJsonobject withpoint_to_layerandon_each_featurefunction objects.E.g.
hansthen commentedon May 18, 2024
My plan of approach would be
Step 1
Go through all Folium children of
branca.Elementand make sure that when the class accepts a**kwargsargument that is passed to the Leaflet class, it will check if any of the arguments is aJsCodeobject and convert it accordingly. Some Folium classes generate their own version of these javascript function objects. Like GeoJson. In that case ensure that the one from**kwargscannot blindly override the one generated in the Folium template.Step 2
Go through all of the Folium children of
branca.Elementthat have a corresponding Leaflet class. If the Leaflet class documents function objects, add these as explicit arguments to the Folium class__init__method and document them.hansthen commentedon Dec 11, 2024
I think this can be closed with release 0.19.0