1

I've recently discovered iPython widgets, and I would like to use them to provide more intearction for users. In particular, I have a function that returns an HTML table, based on choices made by user.

When I use the widgets and interact, the HTML table does not get updated. Instead, each time the user makes a selection, it prints a new table below, and it's not very helpful since the tables can be quite large.

On the other hand, if I return the string only of the HTML table, it is updated, but again not very helpful for visualization!

Is there any solution for the HTML table to be updated instead of printed each time the user makes a selection?

EDIT: Nevermind, I found out how to link the html output to another widget!

Here is an example of working code, with a simple table:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.