如何在基于 folium 的地图中将动态字符串值从 Python 传递到 html?
How to pass a Dynamic String value from Python to html in a folium based Map?
我正在尝试为所有地图标记创建一个包含动态信息的地图。例如,在显示名称、图片和与该餐厅相关的其他信息的区域中带有餐厅标记的地图。
问题: 如何为地图中的每个标记将动态字符串值从 Python 传递到 HTML。
我能够 link 每个标记正确的图片,但不能 link 名称等文本字段。我把 html 放在里面还是外面都没关系for循环的它总是给我错误的静态值。
P.S - 我是编程新手
# creating map layout, center point & view
m_sat = folium.Map(location=[28.595793, 77.414752], zoom_start=13, tiles='https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', attr='Created by') # added Esri_WorldImagery
html = '''<img ALIGN="Right" src="data:image/png;base64,{}">\
<h1>Name: </h1>{{ var_pass }}<br />\
<h2>Location: </h2>Noida<br />'''.format
# creating for loop below for dynamic content
for plot_numb in range(address.shape[0]):
picture = base64.b64encode(open(str(plot_numb+1)+'.png','rb').read()).decode()
iframe = IFrame(html(picture), width=300+200, height=300+20)
popup = folium.Popup(iframe, max_width=650)
icon = folium.Icon(color="white", icon="cloud", icon_color='black')
tooltip = 'Click to view more about: '+address.iloc[plot_numb,0]
var_pass = address.iloc[plot_numb,0]
marker = folium.Marker(location=address.iloc[plot_numb,1],
popup=popup, tooltip=tooltip, icon=icon).add_to(m_sat)
m_sat
我应该可以在地图上显示每个标记的相关名称信息。
附上问题的最终结果图片:
"address" 数据帧示例:
Name Location
0 Farzi Cafe [28.562, 77.387]
1 Skylounge [28.562, 77.387]
2 Tamasha Cafe [28.562, 77.387]
3 Starbucks [28.565, 77.449]
4 Pizza Hut [28.620, 77.425]
试试这个:
var_name = 'restaurant_name'
var_loc = 'restaurant_location'
# var_picture = <base64 image data>
html = f'''<img ALIGN="Right" src="data:image/png;base64,{var_picture}">\
<h1>Name: </h1>{var_name}<br />\
<h2>Location: </h2>{var_loc}<br />\
'''
html
## '<img ALIGN="Right" src="data:image/png;base64,{}"><h1>Name: </h1>restaurant_name<br /><h2>Location: </h2>restaurant_location<br />'
现在,遍历所有名称和位置的数据框。
我正在尝试为所有地图标记创建一个包含动态信息的地图。例如,在显示名称、图片和与该餐厅相关的其他信息的区域中带有餐厅标记的地图。
问题: 如何为地图中的每个标记将动态字符串值从 Python 传递到 HTML。
我能够 link 每个标记正确的图片,但不能 link 名称等文本字段。我把 html 放在里面还是外面都没关系for循环的它总是给我错误的静态值。
P.S - 我是编程新手
# creating map layout, center point & view
m_sat = folium.Map(location=[28.595793, 77.414752], zoom_start=13, tiles='https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', attr='Created by') # added Esri_WorldImagery
html = '''<img ALIGN="Right" src="data:image/png;base64,{}">\
<h1>Name: </h1>{{ var_pass }}<br />\
<h2>Location: </h2>Noida<br />'''.format
# creating for loop below for dynamic content
for plot_numb in range(address.shape[0]):
picture = base64.b64encode(open(str(plot_numb+1)+'.png','rb').read()).decode()
iframe = IFrame(html(picture), width=300+200, height=300+20)
popup = folium.Popup(iframe, max_width=650)
icon = folium.Icon(color="white", icon="cloud", icon_color='black')
tooltip = 'Click to view more about: '+address.iloc[plot_numb,0]
var_pass = address.iloc[plot_numb,0]
marker = folium.Marker(location=address.iloc[plot_numb,1],
popup=popup, tooltip=tooltip, icon=icon).add_to(m_sat)
m_sat
我应该可以在地图上显示每个标记的相关名称信息。
附上问题的最终结果图片:
"address" 数据帧示例:
Name Location
0 Farzi Cafe [28.562, 77.387]
1 Skylounge [28.562, 77.387]
2 Tamasha Cafe [28.562, 77.387]
3 Starbucks [28.565, 77.449]
4 Pizza Hut [28.620, 77.425]
试试这个:
var_name = 'restaurant_name'
var_loc = 'restaurant_location'
# var_picture = <base64 image data>
html = f'''<img ALIGN="Right" src="data:image/png;base64,{var_picture}">\
<h1>Name: </h1>{var_name}<br />\
<h2>Location: </h2>{var_loc}<br />\
'''
html
## '<img ALIGN="Right" src="data:image/png;base64,{}"><h1>Name: </h1>restaurant_name<br /><h2>Location: </h2>restaurant_location<br />'
现在,遍历所有名称和位置的数据框。