Interacts for the Sage Jupyter notebook#
This is mostly the same as the stock ipywidgets.interact
, but with
some customizations for Sage.
EXAMPLES:
sage: from sage.repl.ipython_kernel.interact import interact
sage: @interact
....: def f(x=(0, 10)):
....: pass
...Interactive function <function f at ...> with 1 widget
x: IntSlider(value=5, description='x', max=10)
sage: f.widget.children
(IntSlider(value=5, description='x', max=10), Output())
from sage.repl.ipython_kernel.interact import interact
@interact
def f(x=(0, 10)):
pass
- class sage.repl.ipython_kernel.interact.sage_interactive(**kwargs: Any)#
Bases:
interactive
Wrapper around the ipywidgets interactive which handles some SageNB specifics.
EXAMPLES:
sage: from sage.repl.ipython_kernel.interact import sage_interactive sage: def myfunc(x=10, y="hello", z=None): pass sage: sage_interactive(myfunc, x=(0,100), z=["one", "two", "three"]) ...Interactive function <function myfunc at ...> with 3 widgets x: IntSlider(value=10, description='x') y: Text(value='hello', description='y') z: Dropdown(description='z', options=('one', 'two', 'three'), value=None)
from sage.repl.ipython_kernel.interact import sage_interactive def myfunc(x=10, y="hello", z=None): pass sage_interactive(myfunc, x=(0,100), z=["one", "two", "three"])
- signature()#
Return the fixed signature of the interactive function (after a possible
auto_update
parameter was removed).EXAMPLES:
sage: from sage.repl.ipython_kernel.interact import sage_interactive sage: def myfunc(x=[1,2,3], auto_update=False): pass sage: sage_interactive(myfunc).signature().parameters ...mappingproxy({'x': <Parameter "x=[1, 2, 3]">})
- classmethod widget_from_iterable(abbrev, *args, **kwds)#
Convert an unspecified iterable to a widget.
This behaves like in ipywidgets, except that an iterator (like a generator object) becomes a
SelectionSlider
.EXAMPLES:
sage: from sage.repl.ipython_kernel.interact import sage_interactive sage: sage_interactive.widget_from_iterable([1..5]) ...Dropdown(options=(1, 2, 3, 4, 5), value=1) sage: sage_interactive.widget_from_iterable(iter([1..5])) ...SelectionSlider(options=(1, 2, 3, 4, 5), value=1) sage: sage_interactive.widget_from_iterable((1..5)) ...SelectionSlider(options=(1, 2, 3, 4, 5), value=1) sage: sage_interactive.widget_from_iterable(x for x in [1..5]) ...SelectionSlider(options=(1, 2, 3, 4, 5), value=1) sage: def gen(): ....: yield 1; yield 2; yield 3; yield 4; yield 5 sage: sage_interactive.widget_from_iterable(gen()) ...SelectionSlider(options=(1, 2, 3, 4, 5), value=1)
- classmethod widget_from_single_value(abbrev, *args, **kwds)#
Convert a single value (i.e. a non-iterable) to a widget.
This supports the Sage
Color
andMatrix
classes. Any unknown type is changed to a string for evaluating. This is meant to support symbolic expressions likesin(x)
.EXAMPLES:
sage: from sage.repl.ipython_kernel.interact import sage_interactive sage: sage_interactive.widget_from_single_value("sin(x)") ...Text(value='sin(x)') sage: sage_interactive.widget_from_single_value(sin(x)) ...EvalText(value='sin(x)') sage: from sage.plot.colors import Color sage: sage_interactive.widget_from_single_value(matrix([[1, 2], [3, 4]])) ...Grid(value=[[1, 2], [3, 4]], children=(Label(value=''), VBox(children=(EvalText(value='1', layout=Layout(max_width='5em')), EvalText(value='3', layout=Layout(max_width='5em')))), VBox(children=(EvalText(value='2', layout=Layout(max_width='5em')), EvalText(value='4', layout=Layout(max_width='5em')))))) sage: sage_interactive.widget_from_single_value(Color('cornflowerblue')) ...SageColorPicker(value='#6495ed')
- classmethod widget_from_tuple(abbrev, *args, **kwds)#
Convert a tuple to a widget.
This supports two SageNB extensions:
(description, abbrev)
ifdescription
is a string and(default, abbrev)
ifabbrev
is not a single value.Symbolic expressions are changed to a floating-point number.
EXAMPLES:
sage: from sage.repl.ipython_kernel.interact import sage_interactive sage: sage_interactive.widget_from_tuple( (0, 10) ) ...IntSlider(value=5, max=10) sage: sage_interactive.widget_from_tuple( ("number", (0, 10)) ) ...IntSlider(value=5, description='number', max=10) sage: sage_interactive.widget_from_tuple( (3, (0, 10)) ) ...IntSlider(value=3, max=10) sage: sage_interactive.widget_from_tuple((2, [('one', 1), ('two', 2), ('three', 3)])) ...Dropdown(index=1, options=(('one', 1), ('two', 2), ('three', 3)), value=2) sage: sage_interactive.widget_from_tuple( (sqrt(2), pi) ) ...FloatSlider(value=2.277903107981444, max=3.141592653589793, min=1.4142135623730951)