Ryan
Ryan

Reputation: 1

How to add min and max width for folium popup

I've seen a couple of posts trying to give answers for this but none have worked in my code. I want to set a min and max width for my folium popups so that everything is on one line until I specify that there should be a break. It currently looks like the image below.

Screenshot

My code is as follows:

# Create map
map = folium.Map(
    location = [41.8, -72.4],
    zoom_start = 8
)

# Plot job locations, create popup and tooltip, define colors for icons
for _, jobs in df_mod.iterrows():
    folium.Marker(
        location = [jobs['Latitude'], jobs['Longitude']],
        popup = '<b>Status: </b>' + jobs['Status'] + '<br>' + '<b>Product: </b>' + jobs['Type'] + '<br>' + '<b>Contact: </b>' + jobs['Contact'], min_width = 1000,
        tooltip = jobs['Job'],
        icon = folium.Icon(color = jobs['Color'], prefix = 'fa', icon='circle')
    ).add_to(map)

# Display map
map

Upvotes: 0

Views: 1890

Answers (1)

r-beginners
r-beginners

Reputation: 35265

The folium popup cannot be set to a minimum width, so only the maximum value is valid. Popups can be created by configuring the string representation in HTML and specifying the maximum width. max_width='100%' is the default value, so they will be created to fit the maximum value of a new lined string. (Example of your question) The first example is an example of setting max_width. The second example can be controlled using an IFrame if you want to set the height and width. See this example for more details.

import pandas as pd

df_mod = pd.DataFrame({'Latitude':[41.8],
                       'Longitude':[-72.4],
                       'Status':['In Progress'],
                       'Product':['Fire'],
                       'Job':['Machinr learning'],
                       'Type': ['Full Stack'],
                       'Contact': ['John Smith'],
                       'Color':'red'})

import folium

m = folium.Map(location=[41.8, -72.4], zoom_start=8)

for _, jobs in df_mod.iterrows():
    
    html = '''
    <b>Status: </b>{status}
    <b>Product: </b>{type}
    <b>Contact: </b>{contact}
    '''.format(status=jobs['Status'],type=jobs['Type'],contact=jobs['Contact'])
    
    folium.Marker(
        location = [jobs['Latitude'], jobs['Longitude']],
        popup = folium.Popup(html, parse_html=False, max_width=1000),
        tooltip = jobs['Job'],
        icon = folium.Icon(color = jobs['Color'], prefix = 'fa', icon='circle')
    ).add_to(m)

m

enter image description here

Example using IFrame:

import folium
import branca

m = folium.Map(location=[41.8, -72.4], zoom_start=8)

for _, jobs in df_mod.iterrows():
    
    html = '''
    <b>Status: </b>{status}
    <b>Product: </b>{type}
    <b>Contact: </b>{contact}
    '''.format(status=jobs['Status'],type=jobs['Type'],contact=jobs['Contact'])

    iframe = branca.element.IFrame(html=html, width=500, height=100)
    popup = folium.Popup(iframe, max_width=500)

    folium.Marker(
        location = [jobs['Latitude'], jobs['Longitude']],
        popup = popup,
        tooltip = jobs['Job'],
        icon = folium.Icon(color=jobs['Color'], prefix='fa', icon='circle')
    ).add_to(m)

m

enter image description here

Upvotes: 1

Related Questions