You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Folium.draw插件编辑报错:NameError: self未定义

Folium Draw插件自定义后出现NameError: name 'self' is not defined的问题修复

问题背景

从Folium源码中提取draw.py集成到自有项目后,运行时触发错误:if self.export: NameError: name 'self' is not defined,但代码中已在__init__方法里定义了self.export属性,需排查原因并解决。

相关代码片段:

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")

错误原因

  1. 缩进错误:render方法前有多余空格,导致该方法未正确绑定到类上,成为独立函数。非类实例方法无法访问self变量。
  2. 类定义缺失:这段代码原本属于DrawControl类,但你提取时可能遗漏了类定义部分。如果没有类包裹,__init__和render只是普通函数,self自然未被定义。

修复方案

方案1:修正方法缩进

确保render方法的缩进与__init__一致,使其成为类的实例方法:

from folium.elements import Element
from folium.figure import Figure

class DrawControl(Element):
    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")

方案2:补全类结构

确认代码包含完整的DrawControl类定义,且正确继承自Folium的Element类,同时导入必要的依赖(Element和Figure)。

验证步骤

  1. 检查类结构完整性,确保所有方法缩进正确。
  2. 实例化DrawControl并传入export=True,测试导出按钮是否正常渲染。
  3. 确认项目中Folium依赖版本与自定义代码兼容。

内容的提问来源于stack exchange,提问作者Geographos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 11:35:59