Issue
I try to create an interactive dashboard using ipywidgets. (ipywidgets version 7.5.0, python version 3.7.2)
In a single batch of code, the widgets.VBox()
shows automatically. But once I outsource the statement to a function, the widgets.VBox()
does not show unless I explicitly wrap it in a display()
statement. Here is a simple piece of code that works. It shows the slider on top and the number chosen times 5 at the bottom.
import ipywidgets as widgets
from IPython.display import display
def fa(a,b):
display(a*b)
a = widgets.IntSlider(10)
out = widgets.interactive_output(fa,{'a':a,'b':widgets.fixed(5)})
widgets.VBox([a,out])
Once I put the VBox into an output function, nothing is displayed, not even an error message:
def fa(a,b):
display(a*b)
def db(a,b):
widgets.VBox([a,b])
a = widgets.IntSlider(10)
out = widgets.interactive_output(fa,{'a':a,'b':widgets.fixed(5)})
db(a,out)
But once I replace widgets.VBox([a,b])
in the db() function with display(widgets.VBox([a,b]))
everything is back to normal.
Sorry, may be a basic question, but what is the system behind this behaviour?
Solution
If you're working in Notebook, the default behaviour is to print/display the last line of any cell to the output space below. Hence your widgets.VBox([a,out])
on the last line of your first snippet is actually display(widgets.VBox([a,out]))
in disguise.
Answered By - ac24
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.