python-visualization / folium

Python Data. Leaflet.js Maps.
https://python-visualization.github.io/folium/
MIT License
6.72k stars 2.21k forks source link

Support function objects in Leaflet options. #1943

Open hansthen opened 1 month ago

hansthen commented 1 month ago

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?

Conengmo commented 2 weeks ago

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 options to have JsCode values? I would like that, but not sure how (without touching every template).

Update: is this maybe what you mean? https://github.com/python-visualization/folium/pull/1856/

hansthen commented 2 weeks ago

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.GeoJson documentation, 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.

E.g.

geojson = GeoJson(
    point_to_layer = JsCode("""
        function(feature, latlng) {
            if(feature.properties.type === "X") 
                return new CircleMarker(latlng, {radius: 10});
            else
                return new Circle(latlng, {radius: feature.properties.radius));
        }
    """)
)
hansthen commented 2 weeks ago

My plan of approach would be

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.