iPython小部件:与html结果的interact()

时间:2016-07-01 13:27:19

标签: python-3.x ipython-notebook jupyter-notebook ipywidgets

我最近发现了iPython小部件,我想用它们为用户提供更多的心电图。特别是,我有一个函数,它根据用户的选择返回一个HTML表。

当我使用小部件并进行交互时,HTML表格不会更新。相反,每次用户进行选择时,它都会在下面打印一个新表,由于表格可能非常大,所以它不是很有用。

另一方面,如果我只返回HTML表格的字符串,它会被更新,但对可视化再次没有帮助!

是否有任何解决方案可以更新HTML表而不是每次用户进行选择时打印?

编辑:没关系,我发现如何将html输出链接到另一个小部件!

以下是一个使用简单表格的工作代码示例:

from IPython.display import display,HTML
#ipywidgets imports
from __future__ import print_function
from ipywidgets import *
import ipywidgets as widgets

#EDIT
#added an html widget for output
w3 = widgets.HTML(value="")

def my_function(a, b):
    html_table = '<table><thead>'
    for col in a:
        html_table += '<th>'+col+'</th>'
    html_table += '</thead><tbody>'
    for row in b:
        html_table += '<tr>'
        for col in a:
            html_table += '<td>'+row+'</td>'
        html_table += '</tr>'
    html_table += '</tbody></table>'

    #EDIT
    w3.value = html_table
    #return HTML(html_table)
    #return html_table

widget1 = widgets.SelectMultiple(
    description="columns:",
    options=['A', 'B', 'C', 'D', 'E', 'F', 'G']
)

widget2 = widgets.SelectMultiple(
    description="rows:",
    options=['A', 'B', 'C', 'D', 'E', 'F', 'G']
)

#EDIT
interact(my_function, a=widget1, b=widget2)
display(w3)

0 个答案:

没有答案
相关问题