mirror of
https://github.com/wassname/baukit.git
synced 2026-07-20 12:10:33 +08:00
15 KiB
15 KiB
In [ ]:
%%bash
!(stat -t /usr/local/lib/*/dist-packages/google/colab > /dev/null 2>&1) && exit
pip install git+https://github.com/davidbau/baukit > /dev/nullIn [ ]:
from baukit import show
show('hello', 'world')In [ ]:
show([[1,2,3,4,5]])In [ ]:
show([
[ 1, 2, 3, 4, 5],
['a', 'b', 'c', 'd', 'e']
])In [ ]:
show(['first row ' * 30,
['second row ' * 10, 'second row second item ' * 5],
['third row ' * 10, ['third row second item ' * 5, ['some more', 'data', 'here']]]])In [ ]:
show([[['hello', [i, j]] for j in range(1, 8)] for i in range(1, 6)])In [ ]:
from PIL import Image
from numpy.random import randint
show([[[f'image {i}',
Image.fromarray(randint(0,255,(128,128,3),dtype='uint8'))]
for i in range(10)]])
In [ ]:
show([show.style(background='pink', font='italic 24px serif'), 'A demonstration of CSS control',
[show.style(background='skyblue'), 2, show.style(background='yellow', flex=2), 3],
show.style(background='lightgreen'), 4])In [ ]:
italic = show.style(background='pink', font='italic 24px serif', border='1px solid black')
show(['hello', [italic, 'world', italic, 'look at me!']])In [ ]:
show(show.TIGHT,
[[[show.style(font='italic 24px serif'), 'lots of data',
show.style(background='gainsboro'),
show.WRAP, [f'({a},{b})' for a in range(i) for b in range(j)]]
for j in range(1, 5)] for i in range(1, 3)])In [ ]:
from baukit import Range, Numberbox
nb = Numberbox()
ra = Range()
show([[nb, show.style(flex=5), ra]])In [ ]:
import random
ra.value = random.randrange(100)In [ ]:
print(nb.value)
print(ra.value)In [ ]:
nb2 = Numberbox()
ra2 = Range(max=1, step=0.001, value=0.5)
nb2.value = ra2.prop('value')
show([[nb2, show.style(flex=8), ra2]])
show(ra2)
In [ ]:
from baukit import Button
btn = Button('Click Me')
def dosomething():
ra2.value = random.randrange(100) / 100
btn.on('click', dosomething)
show(show.style(fontSize=20, height=28), btn)In [ ]:
from baukit import Div
div = Div('change the slider')
ra3 = Range()
def print_value():
div.innerHTML = f'the value is {ra3.value}'
ra3.on('value', print_value)
show([[div, show.style(flex=7), ra3]])In [ ]:
from baukit import Menu
menu = Menu(choices=list(range(0,101,10)))
ra4 = Range(step=10)
menu.value = ra4.prop('value')
show([[menu, show.style(flex=7), ra4]])In [ ]:
from baukit import Choice
choice = Choice(choices=list(range(0,101,50)))
ra6 = Range(step=50)
choice.value = ra6.prop('value')
show([[choice, show.style(flex=6), ra6]])
show([[[choice], show.style(flex=6), ra6]])In [ ]:
from baukit import Datalist
dl = Datalist(choices=[0, 40, 60, 100])
ra5 = Range(step=1)
dl.value = ra5.prop('value')
show([[dl, show.style(flex=7), ra5]])In [ ]:
import torch
from baukit import PlotWidget
def myplot(plt, frequency=1.0, amplitude=1.0):
[ax] = plt.axes
ax.clear()
ax.set_title(f'Sine wave of frequency {frequency:.2f}, amplitude {amplitude:.2f}')
x = torch.linspace(0, 20, 300)
y = (frequency * x).sin()
ax.plot(x, amplitude * y)
ax.set_ylim(-5, 5)
show([[PlotWidget(myplot),
PlotWidget(myplot, frequency=2, amplitude=3)]])In [ ]:
freq_ra = Range(min=0.1, max=5.0, step=0.01, value=1.0)
freq_nb = Numberbox(freq_ra.prop('value'))
amp_ra = Range(min=0.1, max=5.0, step=0.01, value=1.0)
amp_nb = Numberbox(amp_ra.prop('value'))
pw = PlotWidget(myplot, frequency=freq_ra.prop('value'), amplitude=amp_ra.prop('value'), format='svg')
show(show.TIGHT, [[pw],
['frequency', show.style(flex=10), freq_ra, freq_nb],
['amplitude', show.style(flex=10), amp_ra, amp_nb]])In [ ]:
from baukit import Textbox
coord_b = Textbox()
def handle_click(e):
loc = e.location
coord_b.value = f'x,y=({loc.x:.2f}, {loc.y:.2f}) inside={loc.inside}'
pw.on('click', handle_click)
show(show.TIGHT, [['Click the previous plot to see coordinates here:', coord_b]])