0

I am working on a Jupyter Notebook in which I am creating a tab widget in python using the following code:

import ipywidgets as widgets

tab_contents = ['P0', 'P1']
children = [widgets.Text(description=name) for name in tab_contents]
tab = widgets.Tab()
tab.children = children
for i in range(len(children)):
    tab.set_title(i, str(i))

if tab.selected_index ==0:
    vartest = 0
else:
    vartest = 1

display(tab)

In a different cell, I check the value of vartest by calling it

vartest

The problem is that independently of the tab I have selected, vartest is always 0. What I want is that when I am in tab P0 (tab.selected_index ==0), vartest should be 0, when I am in P1 (tab.selected_index ==1) vartest should be 1.

What am I missing?

-- EDIT --

enter image description here

GCGM
  • 901
  • 1
  • 17
  • 38

2 Answers2

2

You need to set an observe to change the value of the variable dynamically:

import ipywidgets as widgets

tab_contents = ['P0', 'P1']
children = [widgets.Text(description=name) for name in tab_contents]
tab = widgets.Tab()
tab.children = children
for i in range(len(children)):
    tab.set_title(i, str(i))

def tab_toggle_var(*args):
    global vartest
    if tab.selected_index ==0:
        vartest = 0
    else:
        vartest = 1
tab.observe(tab_toggle_var)
tab_toggle_var()
display(tab)
ibarrond
  • 6,617
  • 4
  • 26
  • 45
  • When running your code in a jupyter cell and then calling `vartest` in another cell I get `NameError: vartest is not defined`. Did you have a different behaviour?. I have added a screenshot to demostrate the behaviour – GCGM Jan 10 '20 at 08:29
  • 1
    Oh true! on this case you actually need to define `vartest` as global. I had forgotten about it. Correcting now – ibarrond Jan 10 '20 at 08:34
  • This almost works. When running for the first time the code (in a new kernel), the first run of `vartest` in the second cell gives `NameError`. Then, I select the other tab and when running again the second cell calling `vartest` it works! It seems the very first time it is run `vartest` is not recognised. Any idea of why? Hope it is clear – GCGM Jan 10 '20 at 08:40
  • 1
    Yeap, I do: since `observe` doesn't run until there is some change in the widget `tab`, the first time you run it there is no `vartest`. Solved by running `tab_toggle_var()` once before display. – ibarrond Jan 10 '20 at 08:49
  • very nice trick thansk a lot for the help!! I will now adapt it to my script :) – GCGM Jan 10 '20 at 08:54
0

You set the values for vartest within the if statements, without having vartest declared "globally" (i.e. before the if statements) Hence, the vartest variables only live within the if block and are not available after that.

When you call it, it is zero, because that is the default value, assigned by python. Fix: declare vartest before the if statement

maik-s
  • 87
  • 5
  • Even if you declare it inside the `if` statement, `vartest` is a global variable because it is outside of the scope of any function/class/package. Declaring `vartest` before won't change the result. – ibarrond Jan 09 '20 at 16:40
  • The problem is that the `if` statement is executed only once, when the cell is run. This single execution enters in the `if` statement and assigns `vartest` to 0 because by default `tab.selected_index` is 0. – ibarrond Jan 09 '20 at 16:42