streamlit-folium
streamlit-folium copied to clipboard
Problem using st_folium
Hi, I have an error when I using st_folium(map) instead of folium_static(map). When I usign st_folium I have the error bellow:
leaflet = m._template.module.script(m) AttributeError: 'TemplateModule' object has no attribute 'script'
When I using folium_static(map) the maps renders perfect. Thanks!
Code:
#m map generated with folium st.title('GDM') #static_map_=folium_static(m, width=1000, height=1000) st_data_ = st_folium(m, width = 725) print(sta_data_) m.save('./GDM_streamlit_'+ parcela + '.html')
Hi @jepeloa -
Can you provide the code that is creating the map?
Best, Randy
Hi, yes of course, thanks for the reply. I created a cloropeth map using tiff image with geojson polygons and popup. I've created with GeoJsonTooltip a popup using html code from a column of a pandas dataframe. This code works perfect with "folium_static"
the code is:
m= folium.Map(location=[centery, centerx], width=1000,height=1000, zoom_start=20, min_zoom=17,max_zoom=30, tiles='Stamen Terrain')
select_data = st.sidebar.selectbox("Variable",("vainas", "total_seeds", "seeds_per_pod"))
my_scale = np.linspace(0, inferencias[select_data].max()+1000, 10) cp=folium.Choropleth( geo_data=GDM_plots, data=inferencias, columns=['id_plot', select_data], key_on='feature.properties.id_plots_shape', #'feature.properties.ensayo' nan_fill_color="White", name='GDM', fill_color='YlOrRd', style_function=lambda x: {'color':'black','fillColor':'transparent','weight':0.5}, fill_opacity=0.5, line_weight=1, line_opacity = 0.5, zoom_control=True, legend_name=select_data, #title of the legend threshold_scale=my_scale, highlight=True, control_scale=True, ).add_to(m)
raster_layers.ImageOverlay( image=image, bounds=[[ymin, xmin], [ymax, xmax]] ).add_to(m)
#videos_mapa='ensayo_30_03/videos_30_03'
def getHTML(row): #tengo que agregar en GDM_plot una columna con el nombre de archivo if row['tablon']<22: videos_mapa='ensayo_30_03/videos_30_03' else: videos_mapa='ensayo_12_04/videos_12_04' if parcela=='izquierda': if row['entrada']==1: surco='surco_02' if row['entrada']==2: surco='surco_06' if row['entrada']==3: surco='surco_10' if row['entrada']==4: surco='surco_14' if row['entrada']==5: surco='surco_18' if parcela=='derecha': if row['entrada']==1: surco='surco_03' if row['entrada']==2: surco='surco_07' if row['entrada']==3: surco='surco_11' if row['entrada']==4: surco='surco_15' if row['entrada']==5: surco='surco_19' return """<iframe src="""" + "/home/javier/Desktop/GDM/procesamiento/bags/videos/"+ videos_mapa + "/" + surco + "linea"+str(row['entrada'])+"tablon"+str(row['tablon'])+"_.mp4" + """" width="470" height="300" frameborder="0" allowfullscreen><iframe src="""" + "./plotly_graphs/"+row['id_plots_shape']+".html" + """" width="470" height="300" frameborder="0"> <iframe src="""" + "./plotly_graphs/"+row['id_plots_shape']+"_seeds.html" + """" width="470" height="300" frameborder="0">"""
GDM_plots["html_to_display"] = GDM_plots.apply(getHTML, axis=1)
popup = GeoJsonPopup(
labels= False, fields = ["html_to_display"],
localize=True
)
style = {'fillColor': '#00000000', 'color': '#00000000'}
g = folium.GeoJson( GDM_plots, popup=popup, style_function=lambda x: style, tooltip = folium.GeoJsonTooltip(fields=('ensayo','entrada','tablon', 'index','vainas', 'seeds', 'seeds_per_pod'), aliases=('ensayo','entrada','tablon','TOP','vainas','granos', 'granos/vaina')) ).add_to(m)
logo_image = 'logo.jpg'
FloatImage(logo_image, bottom=87, left=5).add_to(m)
from folium.plugins import MeasureControl, Draw
plugins.Fullscreen( position="topright", title="Expand", title_cancel="Exit", force_separate_button=True, ).add_to(m)
m.add_child(MeasureControl())
folium.Marker( location=[-34.557137,-60.432608], popup='RTK GPS base station', ).add_to(m)
MousePosition().add_to(m)
formatter = "function(num) {return L.Util.formatNum(num, 3) + ' º ';};"
MousePosition( position="topright", separator=" | ", empty_string="NaN", lng_first=True, num_digits=20, prefix="Coordinates:", lat_formatter=formatter, lng_formatter=formatter ).add_to(m)
#streamlit
st.title('GDM') static_map_=folium_static(m, width=1000, height=1000) #st_data_ = st_folium(m, width = 725)
Thanks in advance!
That code isn't quite fully functional, because of some missing data variables. I tried pulling out various pieces of it, but was unable to reproduce that error. Would you either be able to provide the full working code, or provide a simplified example that shows the error?
@jepeloa Please enclose your code with triple back ticks(```), so that the code is formatted. Nearly impossible to read as is...
Click to see Code
m= folium.Map(location=[centery, centerx], width=1000,height=1000, zoom_start=20, min_zoom=17,max_zoom=30, tiles='Stamen Terrain')
select_data = st.sidebar.selectbox("Variable",("vainas", "total_seeds", "seeds_per_pod"))
my_scale = np.linspace(0, inferencias[select_data].max()+1000, 10)
cp=folium.Choropleth(
geo_data=GDM_plots,
data=inferencias,
columns=['id_plot', select_data],
key_on='feature.properties.id_plots_shape', #'feature.properties.ensayo'
nan_fill_color="White",
name='GDM',
fill_color='YlOrRd',
style_function=lambda x: {'color':'black','fillColor':'transparent','weight':0.5},
fill_opacity=0.5,
line_weight=1,
line_opacity = 0.5,
zoom_control=True,
legend_name=select_data, #title of the legend
threshold_scale=my_scale,
highlight=True,
control_scale=True,
).add_to(m)
raster_layers.ImageOverlay(
image=image,
bounds=[[ymin, xmin], [ymax, xmax]]
).add_to(m)
#videos_mapa='ensayo_30_03/videos_30_03'
def getHTML(row): #tengo que agregar en GDM_plot una columna con el nombre de archivo
if row['tablon']<22:
videos_mapa='ensayo_30_03/videos_30_03'
else:
videos_mapa='ensayo_12_04/videos_12_04'
if parcela=='izquierda':
if row['entrada']==1:
surco='surco_02'
if row['entrada']==2:
surco='surco_06'
if row['entrada']==3:
surco='surco_10'
if row['entrada']==4:
surco='surco_14'
if row['entrada']==5:
surco='surco_18'
if parcela=='derecha':
if row['entrada']==1:
surco='surco_03'
if row['entrada']==2:
surco='surco_07'
if row['entrada']==3:
surco='surco_11'
if row['entrada']==4:
surco='surco_15'
if row['entrada']==5:
surco='surco_19'
return """<iframe src="""" + "/home/javier/Desktop/GDM/procesamiento/bags/videos/"+ videos_mapa + "/" + surco + "linea"+str(row['entrada'])+"tablon"+str(row['tablon'])+"_.mp4" + """" width="470" height="300" frameborder="0" allowfullscreen></iframe><iframe src="""" + "./plotly_graphs/"+row['id_plots_shape']+".html" + """" width="470" height="300" frameborder="0"></iframe> <iframe src="""" + "./plotly_graphs/"+row['id_plots_shape']+"_seeds.html" + """" width="470" height="300" frameborder="0"></iframe>"""
GDM_plots["html_to_display"] = GDM_plots.apply(getHTML, axis=1)
popup = GeoJsonPopup(
labels= False, fields = ["html_to_display"],
localize=True
)
style = {'fillColor': '#00000000', 'color': '#00000000'}
g = folium.GeoJson(
GDM_plots,
popup=popup, style_function=lambda x: style,
tooltip = folium.GeoJsonTooltip(fields=('ensayo','entrada','tablon', 'index','vainas', 'seeds', 'seeds_per_pod'),
aliases=('ensayo','entrada','tablon','TOP','vainas','granos', 'granos/vaina'))
).add_to(m)
logo_image = 'logo.jpg'
FloatImage(logo_image, bottom=87, left=5).add_to(m)
from folium.plugins import MeasureControl, Draw
plugins.Fullscreen(
position="topright",
title="Expand",
title_cancel="Exit",
force_separate_button=True,
).add_to(m)
m.add_child(MeasureControl())
folium.Marker(
location=[-34.557137,-60.432608],
popup='RTK GPS base station',
).add_to(m)
MousePosition().add_to(m)
formatter = "function(num) {return L.Util.formatNum(num, 3) + ' º ';};"
MousePosition(
position="topright",
separator=" | ",
empty_string="NaN",
lng_first=True,
num_digits=20,
prefix="Coordinates:",
lat_formatter=formatter,
lng_formatter=formatter
).add_to(m)
#streamlit
st.title('GDM')
static_map_=folium_static(m, width=1000, height=1000)
#st_data_ = st_folium(m, width = 725)
@jepeloa Can you please provide a minimal reproducible code snippet, formatted with ```s which shows the issue you are experiencing? Otherwise we won't be able to diagnose this issue or potentially find a solution.
Closing, will re-open pending user follow-up