From 71099a5b5f2a22d585009340793a6fa75323cab8 Mon Sep 17 00:00:00 2001 From: Hans Then Date: Thu, 25 Apr 2024 18:30:42 +0200 Subject: [PATCH 1/2] Dynamically load javascript and css links (#181) * First version that works Horribly mutilated code. Still needs to be cleaned up. * WIP tests fixed There is also code included to ensure we can run without any js links, which is a should not happen scenario. Need to test this later and see if it can be removed again. Also, Dual Map tests do not work, but this is because the dual map feature is also broken. * Fix DualMap * Black * Dummy change to trigger build rerun --- README.md | 3 + streamlit_folium/__init__.py | 19 ++ streamlit_folium/frontend/public/index.html | 147 --------------- streamlit_folium/frontend/src/index.tsx | 187 ++++++++++++-------- tests/test_frontend.py | 67 ++++--- 5 files changed, 168 insertions(+), 255 deletions(-) diff --git a/README.md b/README.md index 646333d..a9ec2cf 100644 --- a/README.md +++ b/README.md @@ -30,3 +30,6 @@ Currently, there are two functions defined: ## Example ![streamlit_folium example](https://raw.githubusercontent.com/randyzwitch/streamlit-folium/master/tests/visual_baseline/test_basic/first_test/baseline.png) + + + diff --git a/streamlit_folium/__init__.py b/streamlit_folium/__init__.py index bb6150e..b62abaf 100644 --- a/streamlit_folium/__init__.py +++ b/streamlit_folium/__init__.py @@ -366,6 +366,23 @@ def bounds_to_dict(bounds_list: list[list[float]]) -> dict[str, dict[str, float] st.info("Layer control js:") st.code(layer_control_string) + def walk(fig): + if isinstance(fig, folium.plugins.DualMap): + yield from walk(fig.m1) + yield from walk(fig.m2) + if isinstance(fig, folium.elements.JSCSSMixin): + yield fig + if hasattr(fig, "_children"): + for child in fig._children.values(): + yield from walk(child) + + css_links = [] + js_links = [] + + for elem in walk(folium_map): + css_links.extend([href for _, href in elem.default_css]) + js_links.extend([src for _, src in elem.default_js]) + component_value = _component_func( script=leaflet, html=html, @@ -381,6 +398,8 @@ def bounds_to_dict(bounds_list: list[list[float]]) -> dict[str, dict[str, float] return_on_hover=return_on_hover, layer_control=layer_control_string, pixelated=pixelated, + css_links=css_links, + js_links=js_links, ) return component_value diff --git a/streamlit_folium/frontend/public/index.html b/streamlit_folium/frontend/public/index.html index 9bbb3c0..c605b70 100644 --- a/streamlit_folium/frontend/public/index.html +++ b/streamlit_folium/frontend/public/index.html @@ -6,153 +6,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -