连接jupyter笔记本中的两张iPysheet,以可视化

2024-05-29 03:20:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用ipysheets和ipywidgets在jupyter笔记本中使用voila生成一个类似web的用户界面

这是一个超级强大的工具,可以非常快速地创建用户界面,实现快速用户交互,而无需借助网页,即服务器、django、web框架等

我正在尝试链接两个iPysheet的单元格,一个用于输入数据,另一个用于输出数据。 每次输入数据发生变化时,输出数据表的内容都应该动态更新(在本例中,通过单击复选框进行示例)

from ipysheet import *
import ipywidgets as widgets
import ipysheet
import pandas as pd

# example dataframe with check boxes, (including booleans)
DF_input = pd.DataFrame(data={'a':[False,False,False],'b':[False,False,False]})
sheet_input = ipysheet.sheet(from_dataframe(DF_input))

# Output dataframe has as many columns as input dataframe but only one row
DF_m = pd.DataFrame(data={'m1':['non'],'m2':['non']})               
sheet_m = ipysheet.sheet(from_dataframe(DF_m))

# I try here to pass the cells of the current ipysheet to cell_m1 and cell_m2
cell_m1 = ipysheet.cell(0, 1)
cell_m2 = ipysheet.cell(0, 2)

I would like that this function runs every time there is a change in the Input sheet.
def calculate(change):
    mydf = to_dataframe(sheet_input)
    cell_m1.value = mydf.loc[:,'a'].any()
    cell_m2.value = mydf.loc[:,'b'].any()

# I want that the cells of the second sheet observe the changes. 
# I want to call the function calculate everytime there is a change
cell_m1.observe(calculate, 'value')
cell_m2.observe(calculate, 'value')

widgets.VBox([sheet_input,sheet_m])

结果是:

enter image description here

尝试单击复选框时,会发生奇怪的情况:

enter image description here

我最终想做的是将第二张图纸的单元格链接到第一张图纸的输入值。函数calculate必须获取所有工作表输入,进行一些计算并在工作表m中填写值

我做了什么: 我多次阅读ipysheets文档,尤其是observe函数。 https://readthedocs.org/projects/ipysheet/downloads/pdf/latest/

来点帮助就好了


Tags: thetoimportfalsedataframedfinputvalue

热门问题