在ipywidgets中显示图例的颜色框

问题描述

关于我在ipyvolume中制作的模型的图例,我想制作一个widget.HTML,该HTML除了在widgets.HTML()中的相应标签之外还显示一个小颜色框。

如我所见,您只能在HTML代码中放置单个字符串行。有人有个好主意吗?

解决方法

我决定只用所需的颜色构建一个网格和颜色按钮:

def create_expanded_button(description):
    return Button(description=description,style={'font_weight': 'bold'},layout=Layout( width='auto'))

def color_box (description,color):
    b1 = Button(description=description)
    b1.style.button_color = color
    return b1


# names and colors are two lists of length 11 and 22

cgrid = GridspecLayout(12,2)
tgrid = GridspecLayout(12,1)

c = 0
for i in range(12):
        for j in range(2):
                cgrid[i,j] = color_box(''.format(i,j),colors[c])
                c+=1 
     
            
for i in range(12):
        for j in range(1):
            tgrid[i,j] = create_expanded_button(names[i-1].format(i,j))
 
        
grid = GridspecLayout(2,2,height='auto',width='auto')
grid[:,:1] = cgrid
grid[:,1:] = tgrid
grid