让 ipywidgets 更新 folium 热图?

Getting ipywidgets to update a folium heatmap?

初学者 python 用户,使用 Folium 在热图方面取得了一些成功。我正在尝试使用 Global Terrorism Database as a source for some visualizations but I really wanted to use ipywidgets 到 select 列表中的特定恐怖组织来更新我的热图。我已经为 ISIS 构建了一个热图,创建了一个包含我要比较的组的 ipyvuetify 按钮,但我无法弄清楚函数和 widgets.link 语法。我真的很感激一些帮助,因为似乎没有任何好的指南(对于我的技能水平)如何做我在这里想做的事情。

import ipyvuetify as v
import ipywidgets as widgets
import folium
from folium import plugins
from folium.plugins import HeatMap as hm

map = folium.Map(location = [25,15], tiles = "cartodbdark_matter", zoom_start = 2)
selected = gtd.loc[gtd['Group'] == 'Islamic State of Iraq and the Levant (ISIL)'] 
coords = selected[['Latitude','Longitude']]
hm(coords).add_to(map)

terrorists = [
    'Taliban', 
    'Shining Path (SL)', 
    'Islamic State of Iraq and the Levant (ISIL)', 
    'Farabundo Marti National Liberation Front (FMLN)', 
    'Al-Shabaab', 
    'Irish Republican Army (IRA)', 
    'Revolutionary Armed Forces of Colombia (FARC)', 
    'New Peoples Army (NPA)', 
    'Kurdistan Workers Party (PKK)', 
    'Boko Haram']

# This is where things go off the rails for me, not exactly sure how to arrange this function
def update(x):
    if widget_terrorist_selector.selected is not None:
        xmin, xmax = widget_terrorist_selector.selected
widget_terrorist_selector.observe(update,'widget_terrorist_selector')

# The selector here works but I can't figure out how to link it to my map so that it pushes a new set of heatmap coordinates
widget_terrorist_selector = v.Select(label='Choose Option', items=terrorists, v_model=terrorists[0])
widget_terrorist_selector

# This bit keeps throwing a "TypeError: Each object must be HasTraits, not <class 'pandas.core.frame.DataFrame'>"
widgets.link((widget_terrorist_selector,'v_model'),(selected, 'Group'))

提前致谢!

widgets.link 用于保持两个 ipywidgets 同步。我不确定这就是您要在这里实现的目标,它可能可以通过普通的旧 observe.

实现

我认为在您的 update 函数中您需要对地图进行必要的更改,例如

def update(x):
    new_choice = widget_terrorist_selector.selected
    selected = gtd.loc[gtd['Group'] == new_choice] 
    coords = selected[['Latitude','Longitude']]
    hm(coords).add_to(map)