Search code examples
pythonfolium

Python folium problem with editing draw plugin


I would like to make some edits to the Python Folium.draw plugin.

I have the code from the following link:

https://github.com/python-visualization/folium/blob/main/folium/plugins/draw.py

After applying it to my code I have an error:

if self.export: NameError: name 'self' is not defined

 def __init__(
    self,
    export=False,
    filename="data.geojson",
    position="topleft",
    show_geometry_on_click=True,
    draw_options=None,
    edit_options=None,
   ):
    super().__init__()
    self._name = "DrawControl"
    self.export = export
    self.filename = filename
    self.position = position
    self.show_geometry_on_click = show_geometry_on_click
    self.draw_options = draw_options or {}
    self.edit_options = edit_options or {}

  def render(self, **kwargs):
    super().render(**kwargs)

    figure = self.get_root()
    assert isinstance(
        figure, Figure
     ), "You cannot render this Element if it is not in a Figure."

    export_style = """
        <style>
            #export {
                position: absolute;
                top: 5px;
                right: 10px;
                z-index: 999;
                background: white;
                color: black;
                padding: 6px;
                border-radius: 4px;
                font-family: 'Helvetica Neue';
                cursor: pointer;
                font-size: 12px;
                text-decoration: none;
                top: 90px;
            }
         </style>
      """
    export_button = """<a href='#' id='export'>Export</a>"""

    if self.export:
        figure.header.add_child(Element(export_style), name="export")
        figure.html.add_child(Element(export_button), name="export_button")

I see that self has been defined already.

What exactly causes this error and how to fix it?


Solution

  • Minimal working example:

    import folium
    from branca.element import Element, Figure, MacroElement
    from jinja2 import Template
    from folium.elements import JSCSSMixin
    
    
    class Draw(JSCSSMixin, MacroElement):
        def __init__(
            self,
            export=False,
            filename="data.geojson",
            position="topleft",
            show_geometry_on_click=True,
            draw_options=None,
            edit_options=None,
        ):
            super().__init__()
            self._name = "DrawControl"
            self.export = export
            self.filename = filename
            self.position = position
            self.show_geometry_on_click = show_geometry_on_click
            self.draw_options = draw_options or {}
            self.edit_options = edit_options or {}
    
        def render(self, **kwargs):
            super().render(**kwargs)
    
            figure = self.get_root()
            assert isinstance(
                figure, Figure
            ), "You cannot render this Element if it is not in a Figure."
    
            export_style = """
                <style>
                    #export {
                        position: absolute;
                        top: 5px;
                        right: 10px;
                        z-index: 999;
                        background: white;
                        color: black;
                        padding: 6px;
                        border-radius: 4px;
                        font-family: 'Helvetica Neue';
                        cursor: pointer;
                        font-size: 12px;
                        text-decoration: none;
                        top: 90px;
                    }
                </style>
            """
            export_button = """<a href='#' id='export'>Export</a>"""
            if self.export:
                figure.header.add_child(Element(export_style), name="export")
                figure.html.add_child(Element(export_button), name="export_button")
    
                
    m = folium.Map(location=[38,35], zoom_start=10)
    draw = Draw(export=True)
    draw.add_to(m)
    
    m
    

    Then, you can edit class Draw as you desire.