Erik Shilts
Erik Shilts

Reputation: 4509

Can values from IPython widgets propagate to other cells?

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.

  1. Markdown - Select each of the lines you want to plot from the checkboxes below.
  2. Code - interactive(some_plotting_fn, ckbox1=CheckboxWidget(), ckbox2=CheckboxWidget())
  3. Markdown - Description of first chart. Blah blah.
  4. Code - plot_first_chart_based_on_check_boxes()
  5. Markdown - Description of second chart. Blah blah blah.
  6. Code - 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

Answers (1)

Matt
Matt

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

Related Questions