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

Support function objects in Leaflet options. #1943

Open
hansthen opened this issue Apr 30, 2024 · 3 comments
Open

Support function objects in Leaflet options. #1943

hansthen opened this issue Apr 30, 2024 · 3 comments

Comments

@hansthen
Copy link
Collaborator

hansthen commented Apr 30, 2024

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
Copy link
Member

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

Update: is this maybe what you mean? #1856

@hansthen
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants