import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import ipywidgets as widgets
from ipywidgets import interact, Layout, IntSlider, FloatSlider
import numpy as np
from numpy import arange,sin,pi
def printf(str, *args):
print(str % args, end='')
# Define the square function
#
def square(x):
result = x * x
# print short notice above the result value
print(f'Result of square({x}) is:', end='')
return result
# Simple interact widget from ipywidgets
#
interact = interact(
square,
x=IntSlider(
value=5,
min=0,
max=10,
step=1,
description='Slider value:',
layout=Layout(width='99%')
)
);
# the semicolon ';' suppress the output
# of the code execution
#
interact;
# Define the plot function
#
def myplot(xv):
# calculate x as evenly spaced values within a interval 0 .. xv
x = arange(0, xv, 0.01)
# the plot function
y = sin(pi*x)
# NOTE: Matplotlib doesn't work with pixels directly, but rather
# physical sizes (inch) and DPI. To use pixels for the figures
# figsize, the DPI of the monitor must be used for re-calculation
#
# See: https://www.infobyip.com/detectmonitordpi.php
# DPI of common monitors
my_dpi=96
# figsize: width|height recalculated from inch to pixels
plt.figure(figsize=(800/my_dpi, 400/my_dpi), dpi=my_dpi)
# set the grid lines (light-gray)
plt.grid(axis='x', color='#E0E0E0', linestyle='--', linewidth=2),
plt.grid(axis='y', color='#E0E0E0', linestyle='-', linewidth=2),
# set the axis labels
plt.xlabel('x-values'),
plt.ylabel('y-values'),
# set the plot title
plt.title('Simple plot function based on sin(x)'),
# define the plot
plt.plot(x, y)
# Create an interactive widget for the plot using interact
#
interact = widgets.interact(
myplot,
xv = widgets.FloatSlider(
value=pi,
min=(pi),
max=(4*pi),
step=0.01,
description='Value (x):',
layout=Layout(width='99%')
)
)
# the semicolon ';' suppress the output
# of the code execution
#
interact;
def say_my_name(name):
"""
Print the current widget value in short sentence
"""
print(f'My name is {name}')
widgets.interact(
say_my_name,
name=["Jim", "Emma", "Bond"]
);
def widget_says(x):
"""
Print the current widget value in short sentence
"""
print(f'Widget says: {x}')
widgets.interact(widget_says, x=[0, 1, 2, 3])
widgets.interact(widget_says, x=(0, 10, 1))
widgets.interact(widget_says, x=(0, 10, .5))
# the variable underscore ('_')) returns the output
# of the latest code execution
#
_ = widgets.interact(widget_says, x=True)
int_slider = widgets.IntSlider(
value=5,
min=0, max=10, step=1,
description='Slider',
layout=Layout(width='99%')
)
int_range_slider = widgets.IntRangeSlider(
value=(20, 40),
min=0,
max=100,
step=2,
description='Range Slider',
layout=Layout(width='99%')
)
dropdown = widgets.Dropdown(
value='Feb',
options=['Jan', 'Feb', 'Mar', 'Apr'],
description='Dropdown'
)
radiobuttons = widgets.RadioButtons(
value='Feb',
options=['Jan', 'Feb', 'Mar', 'Apr'],
description='Radio Buttons'
)
combobox = widgets.Combobox(
placeholder='start typing... (e.g. L or o)',
options=['Amsterdam', 'Athens', 'Lisbon', 'London', 'Ljubljana'],
description='Combo Box'
)
checkbox = widgets.Checkbox(
description='Checkbox',
value=True
)
# VBox container to pack all widgets vertically
#
widgets.VBox([
int_slider,
int_range_slider,
dropdown,
radiobuttons,
checkbox,
combobox,
])
sl1 = widgets.IntSlider(
description='Slider 1',
min=0,
max=10,
layout=Layout(width='99%')
)
sl2 = widgets.IntSlider(
description='Slider 2',
min=0,
max=10,
layout=Layout(width='99%')
)
link = widgets.link(
(sl1, 'value'),
(sl2, 'min')
)
sl1.value = 5
widgets.VBox([
sl1, sl2
])