I have a map in Python Folium. I want to put the location in coordinates of a popup after it is clicked into a Python variable, so that I can access it to display it using st.write(), and also do some more stuff with it after that using information I have for the location in a dataframe.
Here is what I have tried, but when I try to st.write(result) in another function, I get error saying result is not defined. How do I resolve it, or is there a better way to do solve what I hope to achieve above in an easier or different way?
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)
I was expecting to be able to use result in the Python function custom_function, but it didn't work and I got an error.
How to copy a marker's location on folium Map by clicking on it?
This example copies it and is what my code is modeled on, but I am not sure how to get it into a Python variable which is my goal to access the coordinates.