Skip to content

foliumap module

Map (Map)

Source code in pyrcgeos/foliumap.py
class Map(folium.Map):
    def __init__(self, center=[40,-100], zoom=2, **kwargs) -> None: 
        """Create a folium map object

        Args:
            center (list, optional): _description_. Defaults to [40,-100].
            zoom (int, optional): _description_. Defaults to 2.
        """        
        super().__init__(location=center, zoom_start=zoom, **kwargs)
    def add_tile_layer(self, url, name, attribution ="", **kwargs):
        """Add a tile layer

        Args:
            url (_type_): _description_
            name (_type_): _description_
            attribution (str, optional): _description_. Defaults to "".
        """        

        tile_layer = folium.TileLayer(
            tiles=url,
            name=name,
            attr=attribution,
            **kwargs
        )
        self.add_child(tile_layer)

    def to_streamlit(
        self,
        width=None,
        height=600,
        scrolling=False,
        add_layer_control=True,
        bidirectional=False,
        **kwargs,
    ):
        """Renders `folium.Figure` or `folium.Map` in a Streamlit app. This method is a static Streamlit Component, meaning, no information is passed back from Leaflet on browser interaction.

        Args:
            width (int, optional): Width of the map. Defaults to None.
            height (int, optional): Height of the map. Defaults to 600.
            scrolling (bool, optional): Whether to allow the map to scroll. Defaults to False.
            add_layer_control (bool, optional): Whether to add the layer control. Defaults to True.
            bidirectional (bool, optional): Whether to add bidirectional functionality to the map. The streamlit-folium package is required to use the bidirectional functionality. Defaults to False.

        Raises:
            ImportError: If streamlit is not installed.

        Returns:
            streamlit.components: components.html object.
        """

        try:
            import streamlit.components.v1 as components

            if add_layer_control:
                self.add_layer_control()

            if bidirectional:
                from streamlit_folium import st_folium

                output = st_folium(self, width=width, height=height)
                return output
            else:
                # if responsive:
                #     make_map_responsive = """
                #     <style>
                #     [title~="st.iframe"] { width: 100%}
                #     </style>
                #     """
                #     st.markdown(make_map_responsive, unsafe_allow_html=True)
                return components.html(
                    self.to_html(), width=width, height=height, scrolling=scrolling
                )

        except Exception as e:
            raise Exception(e)

__init__(self, center=[40, -100], zoom=2, **kwargs) special

Create a folium map object

Parameters:

Name Type Description Default
center list

description. Defaults to [40,-100].

[40, -100]
zoom int

description. Defaults to 2.

2
Source code in pyrcgeos/foliumap.py
def __init__(self, center=[40,-100], zoom=2, **kwargs) -> None: 
    """Create a folium map object

    Args:
        center (list, optional): _description_. Defaults to [40,-100].
        zoom (int, optional): _description_. Defaults to 2.
    """        
    super().__init__(location=center, zoom_start=zoom, **kwargs)

add_tile_layer(self, url, name, attribution='', **kwargs)

Add a tile layer

Parameters:

Name Type Description Default
url _type_

description

required
name _type_

description

required
attribution str

description. Defaults to "".

''
Source code in pyrcgeos/foliumap.py
def add_tile_layer(self, url, name, attribution ="", **kwargs):
    """Add a tile layer

    Args:
        url (_type_): _description_
        name (_type_): _description_
        attribution (str, optional): _description_. Defaults to "".
    """        

    tile_layer = folium.TileLayer(
        tiles=url,
        name=name,
        attr=attribution,
        **kwargs
    )
    self.add_child(tile_layer)

to_streamlit(self, width=None, height=600, scrolling=False, add_layer_control=True, bidirectional=False, **kwargs)

Renders folium.Figure or folium.Map in a Streamlit app. This method is a static Streamlit Component, meaning, no information is passed back from Leaflet on browser interaction.

Parameters:

Name Type Description Default
width int

Width of the map. Defaults to None.

None
height int

Height of the map. Defaults to 600.

600
scrolling bool

Whether to allow the map to scroll. Defaults to False.

False
add_layer_control bool

Whether to add the layer control. Defaults to True.

True
bidirectional bool

Whether to add bidirectional functionality to the map. The streamlit-folium package is required to use the bidirectional functionality. Defaults to False.

False

Exceptions:

Type Description
ImportError

If streamlit is not installed.

Returns:

Type Description
streamlit.components

components.html object.

Source code in pyrcgeos/foliumap.py
def to_streamlit(
    self,
    width=None,
    height=600,
    scrolling=False,
    add_layer_control=True,
    bidirectional=False,
    **kwargs,
):
    """Renders `folium.Figure` or `folium.Map` in a Streamlit app. This method is a static Streamlit Component, meaning, no information is passed back from Leaflet on browser interaction.

    Args:
        width (int, optional): Width of the map. Defaults to None.
        height (int, optional): Height of the map. Defaults to 600.
        scrolling (bool, optional): Whether to allow the map to scroll. Defaults to False.
        add_layer_control (bool, optional): Whether to add the layer control. Defaults to True.
        bidirectional (bool, optional): Whether to add bidirectional functionality to the map. The streamlit-folium package is required to use the bidirectional functionality. Defaults to False.

    Raises:
        ImportError: If streamlit is not installed.

    Returns:
        streamlit.components: components.html object.
    """

    try:
        import streamlit.components.v1 as components

        if add_layer_control:
            self.add_layer_control()

        if bidirectional:
            from streamlit_folium import st_folium

            output = st_folium(self, width=width, height=height)
            return output
        else:
            # if responsive:
            #     make_map_responsive = """
            #     <style>
            #     [title~="st.iframe"] { width: 100%}
            #     </style>
            #     """
            #     st.markdown(make_map_responsive, unsafe_allow_html=True)
            return components.html(
                self.to_html(), width=width, height=height, scrolling=scrolling
            )

    except Exception as e:
        raise Exception(e)

Last update: 2023-05-05