Skip to content

Instantly share code, notes, and snippets.

@jainxy
Created April 10, 2019 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jainxy/de425611d95d472177493c4733bf0f37 to your computer and use it in GitHub Desktop.
Save jainxy/de425611d95d472177493c4733bf0f37 to your computer and use it in GitHub Desktop.
Colab Layout Widget
# Ref - https://colab.research.google.com/notebooks/widgets.ipynb
# Build and control layout dynamically through code
from google.colab import widgets
# ----GRIDS-----
grid = widgets.Grid(2, 2, header_row=True, header_column=True) # Grid object
# Grid.output_to(indices) to direct output stream to that place in the grid.
with grid.output_to(1, 1):
print("Bye grid")
# Note we can output to arbitrary cell, not necessarily in order
with grid.output_to(0, 0):
print("Hello grid")
print("Now we are outside")
with grid.output_to(1, 0):
print("Back inside!")
grid.clear_cell() # Grid.clear_cell() to wipe off the output from current place. mention index for clearing from particular index
# ----TABS----
# Display multiple outputs in Grid cells in tabular form
tb = widgets.TabBar(['a', 'b'], location=location)
# ['a','b'] -> List of tab names
# location is string among ['start', 'bottom', 'end', 'top']
with tb.output_to('a'):
pylab.figure(figsize=(3, 3))
pylab.plot([1, 2, 3])
# Note you can access tab by its name (if they are unique), or
# by its index.
with tb.output_to('b'):
pylab.figure(figsize=(3, 3))
pylab.plot([3, 2, 3])
pylab.show()
# tb.output_to(tab-name OR index, select=True/False)
# TabBar.clear_tab() # to wipe off the output from current place. mention index for clearing from particular index
# ---------- ITERATE OVER WIDGETS LIKE GRID, TABBAR ----------------
grid = widgets.Grid(3, 3)
for (row, col) in grid:
print('data at cell (%d, %d)\n' % (row, col))
# -----------DELETE WIDGET -----------------------------
t = widgets.TabBar(["hi", "bye"])
g = widgets.Grid(3, 3)
# Do something #
t.remove()
g.remove()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment