using Ipywidgets interactive() on google colab displays the widget indented

Viewed 20

I was testing an option to have widgets appear only when a box is checked. I couldn't use Stack as it's not supported in Google Colab. Everything works as expected, just not displays as expected - as seen in the screenshot here, the widget is not as far left as it can be.

Does anyone know a fix?

class test():
  
  def __init__(self):
    self.option_1 = False
    self.option_2 = False

  def checks(self,a,b):
    if a:
      self.option_1 = True
    else:
      self.option_1 = False
    if b:
      self.option_2 = True
    else:
      self.option_2 = False
    
  def f(self, all_options):
    if all_options:
      w = widgets.interactive(self.checks,a=False,b=False)
      items = widgets.GridBox(w.children[:-1], layout = widgets.Layout(grid_template_columns="repeat(7, 100px)"))
      display(items)
    else:
      IPython.display.clear_output()

def main():
  test_subject=test()
  w = widgets.interactive(test_subject.f,all_options=True)
  display(w)

main()
1 Answers
from google.colab import widgets  # missing in your code
from ipywidgets import interact, interactive, fixed, interact_manual  # missing in your code
import ipywidgets as widgets  # missing in your code



class test():

def __init__(self):
  self.option_1 = False
  self.option_2 = False

def checks(self,a,b):
  if a:
    self.option_1 = True
  else:
    self.option_1 = False
  if b:
    self.option_2 = True
  else:
    self.option_2 = False

def f(self, all_options):
  if all_options:
    w = widgets.interactive(self.checks,a=False,b=False)
    items = widgets.GridBox(w.children[:-1], layout = widgets.Layout(grid_template_columns="repeat(7, 100px)"))
    display(items)
  else:
    IPython.display.clear_output()

def main():
  test_subject=test()
  w = widgets.interactive(test_subject.f,all_options=True)
  display(w)

main()
Related