我正在运行Jupyter笔记本,该笔记本除其他外显示一个简单的字符串到字符串字典(在底部有一个空行,以便用户可以向字典中添加新条目)。我正在使用ipysheet来执行此操作,并且我注意到,由于用户多次将不同的字典加载到工作表中而不重新启动内核,因此Jupyter Notebook的UI线程会变慢并最终崩溃。
经过研究,我发现每个单独的ipysheet单元格都作为一个单独的小部件加载,并且我发现问题很可能是我创建了太多的小部件,并且在卸载字典后没有丢弃它们。我决定尝试更改我的代码,以便它只能创建与显示迄今为止已在该程序中加载到内存的最大的单个字典一样多的单元。
不幸的是,我发现一旦以编程方式创建了单元格,就无法更改其值。我尝试简单地设置cell_object.value = target_value
,因为这似乎是所有其他ipysheet修改的接口,但是发现它并没有更新单元格(即使我可以毫无问题地更改sheet_object.rows = new_row_number
。)-他们继续即使应该将新字典加载到工作表中,也可以保留原始字典的值。
有什么我想念的吗? UI是否更新不正确? ipysheet中的单元格是不可变的类型吗?
def set_sheet_from_dictionary(self,target_dict):
key_list = list(target_dict.keys())
key_list.sort()
key_length = len(target_dict.keys())
with ipysheet.hold_cells():
self.sheet1.rows = key_length+1
for i in range(len(key_list)):
if len(self.cell_list_a) <= i:
# These would probably be better as Column objects? But I'm trying to keep it simple for now
self.cell_list_a.append(ipysheet.cell(i,key_list[i],read_only=True))
self.cell_list_b.append(ipysheet.cell(i,1,target_dict[key_list[i]]))
else:
self.cell_list_a[i].value = key_list[i]
self.cell_list_b[i].value = target_dict[key_list[i]]
if len(self.cell_list_c) == 0:
self.cell_list_c.append(ipysheet.cell(key_length,""))
self.cell_list_c.append(ipysheet.cell(key_length,""))
else:
self.cell_list_c[0].value = ""
self.cell_list_c[0].row = key_length
self.cell_list_c[1].value = ""
self.cell_list_c[1].row = key_length