Skip to content

Conversation

@lewymati
Copy link
Contributor

@lewymati lewymati commented Sep 29, 2025

Displaying a st_folium map with multiple elements requiring plugin, e.g. PolyLineTextPath would add multiple duplicates of the same plugin script link causing other issues (e.g. the layer control not working properly).

For example, the legend in the following app is broken (doesn't work with st_folium but works with folium_static)

import folium
import streamlit as st
from folium.plugins import PolyLineTextPath
from streamlit_folium import st_folium

st.title("Streamlit Folium Polyline Text Example")

# Create the map
m = folium.Map(location=[51.505, -0.09], zoom_start=13)


# create feature group
feature_group_a = folium.FeatureGroup(name="Polyline A").add_to(m)


# First polyline
line1 = folium.PolyLine(
    locations=[[51.505, -0.09], [51.51, -0.1], [51.52, -0.12]],
    color="blue",
    weight=5,
).add_to(feature_group_a)

# Add text along the first polyline
PolyLineTextPath(
    line1,
    " Line 1 Text → ",
    repeat=True,
    offset=7,
    attributes={"fill": "blue", "font-weight": "bold", "font-size": "12"},
).add_to(feature_group_a)


feature_group_b = folium.FeatureGroup(name="Polyline B").add_to(m)

# Second polyline
line2 = folium.PolyLine(
    locations=[[51.505, -0.08], [51.51, -0.07], [51.52, -0.06]],
    color="red",
    weight=5,
).add_to(feature_group_b)

# Add text along the second polyline
PolyLineTextPath(
    line2,
    " Line 2 Text → ",
    repeat=True,
    offset=7,
    attributes={"fill": "red", "font-weight": "bold", "font-size": "12"},
).add_to(feature_group_b)

folium.LayerControl(collapsed=False).add_to(m)

# Render the map in Streamlit
st_folium(
    m,
    width=700,
    height=500,
)

Adding the deduplication of js_links as in this PR fixes it.

Adding multiple elements requiring plugin, e.g. PolyLineTextPath would add multiple duplicates of the same plugin link causing issues downstream (e.g. the layer control not working properly).
@randyzwitch
Copy link
Owner

@hansthen This seems like a reasonable PR, but does it also highlight there's an upstream issue in Folium? I haven't traced through it directly, but does this indicate that Folium is injecting duplicate JS libraries into the page?

If this really is just something we're doing, happy to just merge this and post a new version.

@hansthen
Copy link
Collaborator

hansthen commented Sep 30, 2025

I ran the example code, but that does not generate duplicate js links if I call map.save() at the end. I think it is an artifact of the way we generate css and js links by walking the object tree in streamlit-folium. Personally I'd like to see why that is before accepting this PR.

@hansthen
Copy link
Collaborator

It seems the problem can also be solved by making css_links and js_links set objects and calling update instead of extend in lines 417-426. This feels a bit more clean to me. Also fine to accept the PR as is. And good catch @lewymati.

@randyzwitch
Copy link
Owner

I’m fine accepting as-is, don’t want to make this the type of project that takes weeks or longer to merge a perfectly valid contribution

@randyzwitch randyzwitch merged commit cc7aa08 into randyzwitch:master Sep 30, 2025
6 checks passed
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

Successfully merging this pull request may close these issues.

3 participants