Repository: randyzwitch/streamlit-folium (language: python)

Issue:
Compatibility with folium 0.15.0
I upgraded to folium 0.15.0, and the 2 maps I tested returned as grey, no crash or anything.

From the changelog, i did not identified what could be the issue :
https://[raw.githubusercontent.com/python-visualization/folium/main/CHANGES.txt](https://raw.githubusercontent.com/python-visualization/folium/main/CHANGES.txt)


Discussion / hints:
My guess would be this does something we don’t expect

> - Change internally where layers are added to the map (@Conengmo #1690)
Might very well be the case: https://github.com/python-visualization/folium/pull/1690. I don't know much about Streamlit, but let me know if I can help on the Folium side!


@BastienGauthier could you share something about the maps that caused the issue? And can you look at your browser console logs if there are any errors there?

Note that the Stamen tiles were deprecated, so if you are using those that might also cause issues.
@Conengmo , @randyzwitch : some precisions on the issue

No stamen tiles, you can actually test it with the simplest streamlit folium app:

```python
from streamlit_folium import st_folium  
import folium  
maptest = folium.Map()  
st_data = st_folium(maptest)  
```
And you will get a grey map :
![image](https://github.com/randyzwitch/streamlit-folium/assets/108142642/7fcce1d7-7ee7-46e6-af8a-8288c07a1b0e)

Regarding the console, I am not sure what to look at, but here is an extract : 
![image](https://github.com/randyzwitch/streamlit-folium/assets/108142642/fb10310d-c41b-4121-93a6-90912f373740)

(I am on Microsoft Edge Version 119.0.2151.44 - quite up to date, and same issue on Chrome)
Thanks Bastien.

I looked into it a little bit and it seems indeed related to the change to the `Layer` class. In Streamlit-Folium I suspect this line is contributing, where `folium_map` is a `Map` object, on which `render()` is called:  https://github.com/randyzwitch/streamlit-folium/blob/960f6a40d39219f72b3d928dd9910ed12a3e60bd/streamlit_folium/__init__.py#L249

But in Folium layers are now added to the map through the parent `Figure`: https://github.com/python-visualization/folium/blob/84459ba089da13f2a699670a44fbe6e88e662e7b/folium/map.py#L69. It's basically an assumption in Folium that a `Map` is added to a `Figure`, and the `Figure` is being rendered. That's why often `m.get_root().render()` is used to get the full html representation.

Not sure what the full problem is and how it should be resolved. Maybe on the Folium side the element that adds a `Layer` to its parent should be a separate `MacroElement` child class.
For information, I just tested tu change the folium_map.render() into folium_map.get_root().render(), but I did not resolved magically this issue.

@blackary, maybe you have a better view on what could we test here to solve this ?
@BastienGauthier I tried the same, and found the same result. In theory, we just need a consistent way to get the script out that will render the map with all the objects on it. I will get some time to play with this eventually, but if anyone else can find a solution, that would be excellent.
I am kind of out of my league here so I will let you have a look, but I guess the failed test results may be of help, comparing the 0.15 results with the 0.14 assertions : 
https://github.com/randyzwitch/streamlit-folium/actions/runs/6813281431/job/18527487502
I did some investigation and created a small exemple to complete the issue.

```python
import streamlit as st
from streamlit_folium import st_folium

import folium

m = folium.Map(location=[46.9, 3], zoom_start=6, prefer_canvas=True)

coucou_group = folium.FeatureGroup(name = "test", show=True).add_to(m)

popup_list = [
    [(46.9, 3), 40],
    [(47.9, 3), 40],
    [(49, 3.5), 30],
    [(46.2, 2), 50],
    ]

for i, popup in enumerate(popup_list):
    circle = folium.Circle(popup[0],radius=popup[1],popup=f"{i:03d}")
    circle.add_to(coucou_group)
``` 
Here are the results with v0.14 and v0.15 folium :
[test_v14.txt](https://github.com/randyzwitch/streamlit-folium/files/13321887/test_v14.txt)
[test_v15.txt](https://github.com/randyzwitch/streamlit-folium/files/13321888/test_v15.txt)

The only missing part is therefore the ".addTo(map_div)" missing everywhere, which is consistant with the explainations hereabove.

@Conengmo, do you think we could use some magic as for https://github.com/python-visualization/folium/blob/84459ba089da13f2a699670a44fbe6e88e662e7b/folium/map.py#L58C9-L58C26 here ? I didn't succeed to make this work, but do you think this way is a good solution ?
Seems that folium.raster_layers.ImageOverlay show parameter is ignored. This is why the maps turn out grey.
They can only be displayed if LayerControl is enabled from the upper right corner widget.

Went back to folium==0.14.0 and streamlit-folium==0.15.0 and it works correctly.
> Seems that folium.raster_layers.ImageOverlay show parameter is ignored. This is why the maps turn out grey. They can only be displayed if LayerControl is enabled from the upper right corner widget.
> 
> Went back to folium==0.14.0 and streamlit-folium==0.15.0 and it works correctly.

@egelados : Not sure to understand what you found here : the show parameter should be handled by the Layer class heritage in the folium.raster_layers.ImageOverlay. The point is to make streamlit-folium work with folium==0.15.0. 
Also, folium_static (i.e. the one-way html component) is working : only the direct reading of the _template in _generate_leaflet_string of st_folium seems the issue. An equivalent of folium.Layer._add_layer_to_map sould be used I guess (I tried without success up to now)
Hello @giswqs, you could be of help here !  Did you succeed to correct this folium compatibility in your package ? If the answer is yes, do you think the solution is applicable here ?
I've been looking into a fix and I think I got something. @BastienGauthier do you have time to validate this fix? https://github.com/python-visualization/folium/pull/1834 Much appreciated.
I am not avaiblable now but I will look at this next week, thanks for the fix ! 
I tested it and its works like a charm ! Thanks @Conengmo !
If you can release a 0.15.1 with this fix, it will be much appreciated ! 
Great, thanks for checking! Next steps are making sure tests pass, merge and then do a v0.15.1 release indeed.
Awesome, thanks for working with us to find the fix!

Once this makes it into folium, we’ll make it work on our side (cc @blackary )
Hey,

I think it has made it into folium 0.15.1 :)
https://github.com/python-visualization/folium/releases

Thank you for your work guys !

Fix patch:
diff --git a/requirements.txt b/requirements.txt
index 1acd608..2b78e61 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,5 +1,5 @@
 streamlit>=1.13.0
-folium>=0.13,<0.15
+folium>=0.13,!=0.15.0
 jinja2
 branca
 geopandas
\ No newline at end of file
diff --git a/setup.py b/setup.py
index 1050cf7..ca89f75 100644
--- a/setup.py
+++ b/setup.py
@@ -2,7 +2,7 @@
 
 setuptools.setup(
     name="streamlit_folium",
-    version="0.16.0",
+    version="0.16.1",
     author="Randy Zwitch",
     author_email="rzwitch@gmail.com",
     description="Render Folium objects in Streamlit",
@@ -13,5 +13,5 @@
     include_package_data=True,
     classifiers=[],
     python_requires=">=3.8",
-    install_requires=["streamlit>=1.13.0", "folium>=0.13,<0.15", "jinja2", "branca"],
+    install_requires=["streamlit>=1.13.0", "folium>=0.13,!=0.15.0", "jinja2", "branca"],
 )


Test patch:
diff --git a/tests/requirements.txt b/tests/requirements.txt
index 05c4e94..eb88eb4 100644
--- a/tests/requirements.txt
+++ b/tests/requirements.txt
@@ -1,6 +1,6 @@
 streamlit>=1.13.0
 pytest>=7.1.2
-folium>=0.13,<0.15
+folium>=0.13,!=0.15.0
 pytest-playwright
 pytest-rerunfailures
 geopandas
\ No newline at end of file
diff --git a/tests/test_package.py b/tests/test_package.py
index ea5733e..a669dc0 100644
--- a/tests/test_package.py
+++ b/tests/test_package.py
@@ -4,12 +4,14 @@ def test_map():
     from streamlit_folium import _get_map_string
 
     map = folium.Map()
+    map.render()
+
     leaflet = _get_map_string(map)
     assert (
         """var map_div = L.map(
     "map_div",
     {
-        center: [0, 0],
+        center: [0.0, 0.0],
         crs: L.CRS.EPSG3857,
         zoom: 1,
         zoomControl: true,
@@ -21,7 +23,7 @@ def test_map():
 
     assert "var tile_layer_div_0 = L.tileLayer(" in leaflet
 
-    assert ").addTo(map_div);" in leaflet
+    assert ".addTo(map_div);" in leaflet
 
 
 def test_layer_control():
@@ -48,11 +50,11 @@ def test_draw_support():
     map.render()
     leaflet = _get_map_string(map)
     assert "map_div.on(L.Draw.Event.CREATED, function(e) {" in leaflet
-    assert "drawnItems.addLayer(layer);" in leaflet
+    assert "drawnItems_draw_control_div_1.addLayer(layer);" in leaflet
 
     assert (
         """map_div.on('draw:created', function(e) {
-    drawnItems.addLayer(e.layer);
+    drawnItems_draw_control_div_1.addLayer(e.layer);
 });"""
         in leaflet
     )