我在 Python Folium 中有一张地图。我想在点击一个 Python 变量后将位置放在弹出窗口的坐标中,这样我就可以访问它以使用 st.write() 显示它,并且在那之后使用我拥有的信息做一些更多的事情对于数据框中的位置。
这是我尝试过的方法,但是当我尝试在另一个函数中 st.write(result) 时,我收到错误消息说结果未定义。我该如何解决它,或者是否有更好的方法以更简单或不同的方式解决我希望实现的上述目标?
map = folium.Map(location=[38,-96.5], zoom_start=4)
df.reset_index(drop=True,inplace=True)
cords = df[["lat","lon"]]
cord_list = cords.values.tolist()
for i in range(df.shape[0]):
row = df.iloc[i]
name = row["name"]
date = row["date"]
city = row["city"]
state = row["state"]
pop=folium.Popup(f"""<input type="text" value="{cord_list[i][0]}, {cord_list[i][1]}"{name} id="location"> <br>
{date} <br>
{city}, {state} <br>
<button onclick="myFunction()">Show More</button>
""", max_width=len(f"name= {name}")*7)
folium.Marker(cord_list[i], popup=pop).add_to(map)
el = folium.MacroElement().add_to(map)
el._template = jinja2.Template("""
{% macro script(this, kwargs) %}
function myFunction() {
/* Get the text field */
var copyText = document.getElementById("location");
/* Select the text field */
copyText.select();
copyText.setSelectionRange(0, 99999); /* For mobile devices */
/* Copy the text inside the text field */
var result = document.execCommand("copy");
{{ custom_function(result) }}
}
{% endmacro %}
""")
el._template.globals['custom_function'] = custom_function
i_map = st_folium(map, width=700, height=450)
我期待能够在 Python 函数 custom_function 中使用 result,但它没有用,我得到了一个错误。
This example copy it and is what my code is modeled on,但我不确定如何将它放入Python变量中,这是我访问坐标的目标。