Skip to content

Support function objects in Leaflet options. #1943

@hansthen

Description

@hansthen

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions