【问题标题】:Folium issue with srdocsrdoc 的叶子问题
【发布时间】:2020-03-18 13:32:11
【问题描述】:

我正在尝试使用 Folium 基于数据框绘制地图。在这里你可以查看整个Jupyter Notebook。我在有问题的 sn-p 下方粘贴:

map_borough = folium.Map(location = [borough_lat, borough_lng], zoom_start = 10.7)

for lat, lng, neighborhood in zip(borough['Latitude'], borough['Longitude'], borough['Neighborhood']):
    label = folium.Popup(label, parse_html=True)
    folium.CircleMarker(
        [lat, lng],
        radius=5,
        popup=label,
        color='blue',
        fill=True,
        fill_color='#3186cc',
        fill_opacity=0.7).add_to(map_borough)  
map_borough

正如在笔记本上看到的,这段代码输出了一个奇怪的错误:

---------------------------------------------------------------------------
UndefinedError                            Traceback (most recent call last)
~/miniconda3/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

~/miniconda3/lib/python3.7/site-packages/folium/folium.py in _repr_html_(self, **kwargs)
    291             self._parent = None
    292         else:
--> 293             out = self._parent._repr_html_(**kwargs)
    294         return out
    295 

~/miniconda3/lib/python3.7/site-packages/branca/element.py in _repr_html_(self, **kwargs)
    328         # the HTML in the src attribute with a data URI. The alternative of using a srcdoc
    329         # attribute is not supported in Microsoft Internet Explorer and Edge.
--> 330         html = base64.b64encode(self.render(**kwargs).encode('utf8')).decode('utf8')
    331         onload = (
    332             'this.contentDocument.open();'

~/miniconda3/lib/python3.7/site-packages/branca/element.py in render(self, **kwargs)
    319         """Renders the HTML representation of the element."""
    320         for name, child in self._children.items():
--> 321             child.render(**kwargs)
    322         return self._template.render(this=self, kwargs=kwargs)
    323 

~/miniconda3/lib/python3.7/site-packages/folium/folium.py in render(self, **kwargs)
    368             '</style>'), name='map_style')
    369 
--> 370         super(Map, self).render(**kwargs)
    371 
    372     def fit_bounds(self, bounds, padding_top_left=None,

~/miniconda3/lib/python3.7/site-packages/branca/element.py in render(self, **kwargs)
    637 
    638         for name, element in self._children.items():
--> 639             element.render(**kwargs)

~/miniconda3/lib/python3.7/site-packages/branca/element.py in render(self, **kwargs)
    637 
    638         for name, element in self._children.items():
--> 639             element.render(**kwargs)

~/miniconda3/lib/python3.7/site-packages/folium/map.py in render(self, **kwargs)
    367 
    368         figure.script.add_child(Element(
--> 369             self._template.render(this=self, kwargs=kwargs)),
    370             name=self.get_name())
    371 

~/miniconda3/lib/python3.7/site-packages/jinja2/environment.py in render(self, *args, **kwargs)
   1088             return concat(self.root_render_func(self.new_context(vars)))
   1089         except Exception:
-> 1090             self.environment.handle_exception()
   1091 
   1092     def render_async(self, *args, **kwargs):

~/miniconda3/lib/python3.7/site-packages/jinja2/environment.py in handle_exception(self, source)
    830         from .debug import rewrite_traceback_stack
    831 
--> 832         reraise(*rewrite_traceback_stack(source=source))
    833 
    834     def join_path(self, template, parent):

~/miniconda3/lib/python3.7/site-packages/jinja2/_compat.py in reraise(tp, value, tb)
     26     def reraise(tp, value, tb=None):
     27         if value.__traceback__ is not tb:
---> 28             raise value.with_traceback(tb)
     29         raise value
     30 

<template> in top-level template code()

~/miniconda3/lib/python3.7/site-packages/folium/map.py in render(self, **kwargs)
    367 
    368         figure.script.add_child(Element(
--> 369             self._template.render(this=self, kwargs=kwargs)),
    370             name=self.get_name())
    371 

~/miniconda3/lib/python3.7/site-packages/jinja2/environment.py in render(self, *args, **kwargs)
   1088             return concat(self.root_render_func(self.new_context(vars)))
   1089         except Exception:
-> 1090             self.environment.handle_exception()
   1091 
   1092     def render_async(self, *args, **kwargs):

~/miniconda3/lib/python3.7/site-packages/jinja2/environment.py in handle_exception(self, source)
    830         from .debug import rewrite_traceback_stack
    831 
--> 832         reraise(*rewrite_traceback_stack(source=source))
    833 
    834     def join_path(self, template, parent):

~/miniconda3/lib/python3.7/site-packages/jinja2/_compat.py in reraise(tp, value, tb)
     26     def reraise(tp, value, tb=None):
     27         if value.__traceback__ is not tb:
---> 28             raise value.with_traceback(tb)
     29         raise value
     30 

<template> in top-level template code()

UndefinedError: 'None' has no attribute 'replace'

我在此处和 Google 上都进行了搜索,但无法弄清楚发生了什么。怎么了?

【问题讨论】:

    标签: python python-3.x jupyter-notebook folium


    【解决方案1】:

    刚刚发现错误!我的for 期望在弹出窗口中绘制一个label 变量。

    map_borough = folium.Map(location = [borough_lat, borough_lng], zoom_start = 10)
    
    for lat, lng, label in zip(borough['Latitude'], borough['Longitude'], borough['Neighborhood']):
        label = folium.Popup(label, parse_html=True)
        folium.CircleMarker(
            [lat, lng],
            radius=5,
            popup=label,
            color='blue',
            fill=True,
            fill_color='#3186cc',
            fill_opacity=0.7).add_to(map_borough)  
    map_borough
    
    

    现在可以了!

    【讨论】:

      猜你喜欢
      • 2023-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      • 2011-07-04
      • 2021-09-29
      • 1970-01-01
      • 2023-04-07
      相关资源
      最近更新 更多