Issue with Displaying Different Colors in Streamlit-Folium

Summary

Iโ€™m currently applying a map visualization app using Streamlit and Folium. Iโ€™m trying to display multiple livestock (chicken, pig, cow) on the map, each with a different color. However, Iโ€™m facing an issue where only one color is displayed in the app.

Steps to reproduce

Code snippet:

def display_map(df, selected_states, selected_livestocks):
    if selected_states:
        df = df[df['SIG_KOR_NM'].isin(selected_states)]
    
    if 'ALL' not in selected_livestocks:
        df = df[df['property_type_name'].isin(selected_livestocks)]

    color_mapping = {
            'Unknown': 'black',
            'chiecken': 'green',
            'pig': 'pink',
            'cow': 'orange'
        }

    if not df.empty:
        df['geometry'] = df['geometry_coordinates'].apply(
            lambda x: Polygon(ast.literal_eval(x)[0]) if pd.notnull(x) else None
        )
        map = folium.Map(location=[35.4681593,128.4765802], zoom_start=1, scrollWheelZoom=False, tiles='CartoDB positron')

        for _, row in df.iterrows():
            if row['geometry'] is not None:
                if row['geometry'].is_valid:
                    # st.write(row)
                    property_type_name = row['property_type_name']
                    folium.GeoJson(row['geometry'],
                                   style_function = lambda x: {
                                    'color': color_mapping.get(property_type_name, 'gray'), 
                                    'fillColor': color_mapping.get(property_type_name, 'gray')}).add_to(map)
        folium_static(map)
    else :
        map = folium.Map(location=[ , ], zoom_start=7, scrollWheelZoom=False, tiles='CartoDB positron')

    return map

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

My goal is to successfully display a map where each livestock type is represented by a unique color.

Actual behavior:

Even though there are no error messages, the final output consistently displays in a single color.

Debug info

  • Streamlit version: streamlit-folium 0.14.0
  • Python version: python 3.9.18
  • Using Conda? Visual studio
  • OS version: Mac
  • Browser version: Chrome

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.