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.all import *
>>> from sage.repl.ipython_kernel.interact import interact
>>> @interact
... def f(x=(Integer(0), Integer(10))):
... pass
...Interactive function <function f at ...> with 1 widget
x: IntSlider(value=5, description='x', max=10)
>>> 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 f.widget.children
- class sage.repl.ipython_kernel.interact.sage_interactive(**kwargs: Any)[source]¶
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.all import * >>> from sage.repl.ipython_kernel.interact import sage_interactive >>> def myfunc(x=Integer(10), y='hello', z=None): pass >>> sage_interactive(myfunc, x=(Integer(0),Integer(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()[source]¶
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]">})
>>> from sage.all import * >>> from sage.repl.ipython_kernel.interact import sage_interactive >>> def myfunc(x=[Integer(1),Integer(2),Integer(3)], auto_update=False): pass >>> sage_interactive(myfunc).signature().parameters ...mappingproxy({'x': <Parameter "x=[1, 2, 3]">})
from sage.repl.ipython_kernel.interact import sage_interactive def myfunc(x=[1,2,3], auto_update=False): pass sage_interactive(myfunc).signature().parameters
- classmethod widget_from_iterable(abbrev, *args, **kwds)[source]¶
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)
>>> from sage.all import * >>> from sage.repl.ipython_kernel.interact import sage_interactive >>> sage_interactive.widget_from_iterable((ellipsis_range(Integer(1),Ellipsis,Integer(5)))) ...Dropdown(options=(1, 2, 3, 4, 5), value=1) >>> sage_interactive.widget_from_iterable(iter((ellipsis_range(Integer(1),Ellipsis,Integer(5))))) ...SelectionSlider(options=(1, 2, 3, 4, 5), value=1) >>> sage_interactive.widget_from_iterable((ellipsis_iter(Integer(1),Ellipsis,Integer(5)))) ...SelectionSlider(options=(1, 2, 3, 4, 5), value=1) >>> sage_interactive.widget_from_iterable(x for x in (ellipsis_range(Integer(1),Ellipsis,Integer(5)))) ...SelectionSlider(options=(1, 2, 3, 4, 5), value=1) >>> def gen(): ... yield Integer(1); yield Integer(2); yield Integer(3); yield Integer(4); yield Integer(5) >>> sage_interactive.widget_from_iterable(gen()) ...SelectionSlider(options=(1, 2, 3, 4, 5), value=1)
from sage.repl.ipython_kernel.interact import sage_interactive sage_interactive.widget_from_iterable([1..5]) sage_interactive.widget_from_iterable(iter([1..5])) sage_interactive.widget_from_iterable((1..5)) sage_interactive.widget_from_iterable(x for x in [1..5]) def gen(): yield 1; yield 2; yield 3; yield 4; yield 5 sage_interactive.widget_from_iterable(gen())
- classmethod widget_from_single_value(abbrev, *args, **kwds)[source]¶
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)) # needs sage.symbolic ...EvalText(value='sin(x)') sage: sage_interactive.widget_from_single_value(matrix([[1, 2], [3, 4]])) # needs sage.modules ...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: from sage.plot.colors import Color # needs sage.plot sage: sage_interactive.widget_from_single_value(Color('cornflowerblue')) # needs sage.plot ...SageColorPicker(value='#6495ed')
>>> from sage.all import * >>> from sage.repl.ipython_kernel.interact import sage_interactive >>> sage_interactive.widget_from_single_value("sin(x)") ...Text(value='sin(x)') >>> sage_interactive.widget_from_single_value(sin(x)) # needs sage.symbolic ...EvalText(value='sin(x)') >>> sage_interactive.widget_from_single_value(matrix([[Integer(1), Integer(2)], [Integer(3), Integer(4)]])) # needs sage.modules ...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')))))) >>> from sage.plot.colors import Color # needs sage.plot >>> sage_interactive.widget_from_single_value(Color('cornflowerblue')) # needs sage.plot ...SageColorPicker(value='#6495ed')
from sage.repl.ipython_kernel.interact import sage_interactive sage_interactive.widget_from_single_value("sin(x)") sage_interactive.widget_from_single_value(sin(x)) # needs sage.symbolic sage_interactive.widget_from_single_value(matrix([[1, 2], [3, 4]])) # needs sage.modules from sage.plot.colors import Color # needs sage.plot sage_interactive.widget_from_single_value(Color('cornflowerblue')) # needs sage.plot
- classmethod widget_from_tuple(abbrev, *args, **kwds)[source]¶
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) ) # needs sage.symbolic ...FloatSlider(value=2.277903107981444, max=3.141592653589793, min=1.4142135623730951)
>>> from sage.all import * >>> from sage.repl.ipython_kernel.interact import sage_interactive >>> sage_interactive.widget_from_tuple( (Integer(0), Integer(10)) ) ...IntSlider(value=5, max=10) >>> sage_interactive.widget_from_tuple( ("number", (Integer(0), Integer(10))) ) ...IntSlider(value=5, description='number', max=10) >>> sage_interactive.widget_from_tuple( (Integer(3), (Integer(0), Integer(10))) ) ...IntSlider(value=3, max=10) >>> sage_interactive.widget_from_tuple((Integer(2), [('one', Integer(1)), ('two', Integer(2)), ('three', Integer(3))])) ...Dropdown(index=1, options=(('one', 1), ('two', 2), ('three', 3)), value=2) >>> sage_interactive.widget_from_tuple( (sqrt(Integer(2)), pi) ) # needs sage.symbolic ...FloatSlider(value=2.277903107981444, max=3.141592653589793, min=1.4142135623730951)
from sage.repl.ipython_kernel.interact import sage_interactive sage_interactive.widget_from_tuple( (0, 10) ) sage_interactive.widget_from_tuple( ("number", (0, 10)) ) sage_interactive.widget_from_tuple( (3, (0, 10)) ) sage_interactive.widget_from_tuple((2, [('one', 1), ('two', 2), ('three', 3)])) sage_interactive.widget_from_tuple( (sqrt(2), pi) ) # needs sage.symbolic