Reputation: 4509
Is it possible to have values from IPython widgets propagate to other cells without reloading those cells further downstream?
Specifically, I have some check boxes which indicate whether to plot certain lines in each chart. I have several charts and I'd like to have the charts in separate cells so that I can provide text descriptions of what's in each plot. The rough structure of what I'm envisioning is shown below, with each numbered item in the list is a different cell.
interactive(some_plotting_fn, ckbox1=CheckboxWidget(), ckbox2=CheckboxWidget())
plot_first_chart_based_on_check_boxes()
plot_second_chart_based_on_check_boxes()
Is that feasible? I could do all the plotting in a single cell, but then the plots end up bunched together and I'd prefer to have text describing the charts to introduce them.
Upvotes: 3
Views: 1430
Reputation: 27863
Create the checkbox
as separate objects :
from IPython.html.widgets import interactive, Checkbox
c= Checkbox()
interactive(lambda x,y: print(x,y), x=c, y=Checkbox())
----
display X and Y checkbox
Here some markdown....
interactive(lambda x,y: print(x,z), x=c, z=Checkbox())
----
display X and Z checkbox
Here more markdown, I won't use interactive, so you will need to rerun the cell to see changes :
if x is True:
title('is is True')
plot(...)
The X checkbox is synced across cells (on IPython master at least)
replace the print by your plotting functions. You can also access the value of 'x' by c.value
.
[edit] To respond to people proposind edits: Note that on IPython master and future versions the Widget
suffix has been dropped. The API with suffixes everywhere was provisional and never ment to be definitive. So the correct usage is indeed Checkbox
and not CheckboxWidget
.
Upvotes: 1