1

I want to do HTML formatting into a folium map popup. When I try to render html by using def format_popup_content(row) function then the map does not display. How do I format popup?

This is what I have tried so far

def format_popup_content(row):
    # Format the popup content using HTML
    popup_content = f"""
    <div style="font-family: Arial, sans-serif;">
        <h2 style="margin-bottom: 5px; text-align: center;">District: {row['district']}</h2>
        <table style="width: 100%;">
            <tr>
                <td style="font-weight: bold;">Diagnose:</td>
                <td>{row['provisionaldiagnose']}</td>
            </tr>
            <tr>
                <td style="font-weight: bold;">Age:</td>
                <td>{row['age']}</td>
            </tr>
            <tr>
                <td style="font-weight: bold;">Gender:</td>
                <td>{row['pgender']}</td>
            </tr>
            <!-- Add more information here if needed -->
        </table>
    </div>
    """
    return popup_content

This is the code of popup functionality

popup_text = folium.Html(format_popup_content(row), script=True) 
iframe = branca.element.IFrame(html=popup_text, width=320, height=500)
popup = folium.Popup(iframe, parse_html=True)

# Update the color parameter to use the corresponding color from the color dictionary
folium.CircleMarker([lat, lon], radius=7, color=color, opacity=1.0, fill_color=color, popup=popup).add_to(district_layers[district])
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Ocean Vue
  • 19
  • 1

1 Answers1

0

I didn't have data to reproduce your question, so I used the appropriate sample data to map the popup. The layer information was unknown, so I changed it to markers on the map.

import folium
import branca
import pandas as pd

df = pd.DataFrame({'district':'San Antonio', 'provisionaldiagnose': True,'age':'45','pgender': 'female'}, index=[0])

def format_popup_content(row):
    # Format the popup content using HTML
    popup_content = f"""
    <div style="font-family: Arial, sans-serif;">
        <h2 style="margin-bottom: 5px; text-align: center;">District: {row['district']}</h2>
        <table style="width: 100%;">
            <tr>
                <td style="font-weight: bold;">Diagnose:</td>
                <td>{row['provisionaldiagnose']}</td>
            </tr>
            <tr>
                <td style="font-weight: bold;">Age:</td>
                <td>{row['age']}</td>
            </tr>
            <tr>
                <td style="font-weight: bold;">Gender:</td>
                <td>{row['pgender']}</td>
            </tr>
            <!-- Add more information here if needed -->
        </table>
    </div>
    """
    return popup_content

lat = 29.49639
lon = -98.52898
color='red'

m = folium.Map([43, -100], zoom_start=4)

for i,row in df.iterrows():
    #print(row)
    popup_text = folium.Html(format_popup_content(row), script=True)

iframe = branca.element.IFrame(html=popup_text, width=320, height=200)
popup = folium.Popup(iframe, parse_html=True)

# Update the color parameter to use the corresponding color from the color dictionary
folium.CircleMarker([lat, lon], radius=7, color=color, opacity=1.0, fill_color=color, popup=popup).add_to(m)#district_layers[district]
m

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32