Show More
@@ -1,38 +1,38 b'' | |||
|
1 | 1 | from .widget import Widget, DOMWidget, CallbackDispatcher, register |
|
2 | 2 | |
|
3 | 3 | from .widget_bool import Checkbox, ToggleButton |
|
4 | 4 | from .widget_button import Button |
|
5 | 5 | from .widget_box import Box, FlexBox, HBox, VBox |
|
6 | 6 | from .widget_float import FloatText, BoundedFloatText, FloatSlider, FloatProgress, FloatRangeSlider |
|
7 | 7 | from .widget_image import Image |
|
8 | 8 | from .widget_int import IntText, BoundedIntText, IntSlider, IntProgress, IntRangeSlider |
|
9 | 9 | from .widget_output import Output |
|
10 | 10 | from .widget_selection import RadioButtons, ToggleButtons, Dropdown, Select |
|
11 | 11 | from .widget_selectioncontainer import Tab, Accordion |
|
12 | 12 | from .widget_string import HTML, Latex, Text, Textarea |
|
13 | 13 | from .interaction import interact, interactive, fixed, interact_manual |
|
14 |
from .widget_link import Link, |
|
|
14 | from .widget_link import jslink, jsdlink, Link, DirectionalLink | |
|
15 | 15 | |
|
16 | 16 | # Deprecated classes |
|
17 | 17 | from .widget_bool import CheckboxWidget, ToggleButtonWidget |
|
18 | 18 | from .widget_button import ButtonWidget |
|
19 | 19 | from .widget_box import ContainerWidget |
|
20 | 20 | from .widget_float import FloatTextWidget, BoundedFloatTextWidget, FloatSliderWidget, FloatProgressWidget |
|
21 | 21 | from .widget_image import ImageWidget |
|
22 | 22 | from .widget_int import IntTextWidget, BoundedIntTextWidget, IntSliderWidget, IntProgressWidget |
|
23 | 23 | from .widget_selection import RadioButtonsWidget, ToggleButtonsWidget, DropdownWidget, SelectWidget |
|
24 | 24 | from .widget_selectioncontainer import TabWidget, AccordionWidget |
|
25 | 25 | from .widget_string import HTMLWidget, LatexWidget, TextWidget, TextareaWidget |
|
26 | 26 | |
|
27 | 27 | # We use warn_explicit so we have very brief messages without file or line numbers. |
|
28 | 28 | # The concern is that file or line numbers will confuse the interactive user. |
|
29 | 29 | # To ignore this warning, do: |
|
30 | 30 | # |
|
31 | 31 | # from warnings import filterwarnings |
|
32 | 32 | # filterwarnings('ignore', module='IPython.html.widgets') |
|
33 | 33 | |
|
34 | 34 | from warnings import warn_explicit |
|
35 | 35 | __warningregistry__ = {} |
|
36 | 36 | warn_explicit("IPython widgets are experimental and may change in the future.", |
|
37 | 37 | FutureWarning, '', 0, module = 'IPython.html.widgets', |
|
38 | 38 | registry = __warningregistry__, module_globals = globals) |
@@ -1,39 +1,39 b'' | |||
|
1 | 1 | # Copyright (c) IPython Development Team. |
|
2 | 2 | # Distributed under the terms of the Modified BSD License. |
|
3 | 3 | |
|
4 | 4 | import nose.tools as nt |
|
5 | 5 | |
|
6 | from .. import link, dlink, ToggleButton | |
|
6 | from .. import jslink, jsdlink, ToggleButton | |
|
7 | 7 | from .test_interaction import setup, teardown |
|
8 | 8 | |
|
9 | def test_link_args(): | |
|
9 | def test_jslink_args(): | |
|
10 | 10 | with nt.assert_raises(TypeError): |
|
11 | link() | |
|
11 | jslink() | |
|
12 | 12 | w1 = ToggleButton() |
|
13 | 13 | with nt.assert_raises(TypeError): |
|
14 | link((w1, 'value')) | |
|
14 | jslink((w1, 'value')) | |
|
15 | 15 | |
|
16 | 16 | w2 = ToggleButton() |
|
17 | link((w1, 'value'), (w2, 'value')) | |
|
17 | jslink((w1, 'value'), (w2, 'value')) | |
|
18 | 18 | |
|
19 | 19 | with nt.assert_raises(TypeError): |
|
20 | link((w1, 'value'), (w2, 'nosuchtrait')) | |
|
20 | jslink((w1, 'value'), (w2, 'nosuchtrait')) | |
|
21 | 21 | |
|
22 | 22 | with nt.assert_raises(TypeError): |
|
23 | link((w1, 'value'), (w2, 'traits')) | |
|
23 | jslink((w1, 'value'), (w2, 'traits')) | |
|
24 | 24 | |
|
25 | def test_dlink_args(): | |
|
25 | def test_jsdlink_args(): | |
|
26 | 26 | with nt.assert_raises(TypeError): |
|
27 | dlink() | |
|
27 | jsdlink() | |
|
28 | 28 | w1 = ToggleButton() |
|
29 | 29 | with nt.assert_raises(TypeError): |
|
30 | dlink((w1, 'value')) | |
|
30 | jsdlink((w1, 'value')) | |
|
31 | 31 | |
|
32 | 32 | w2 = ToggleButton() |
|
33 | dlink((w1, 'value'), (w2, 'value')) | |
|
33 | jsdlink((w1, 'value'), (w2, 'value')) | |
|
34 | 34 | |
|
35 | 35 | with nt.assert_raises(TypeError): |
|
36 | dlink((w1, 'value'), (w2, 'nosuchtrait')) | |
|
36 | jsdlink((w1, 'value'), (w2, 'nosuchtrait')) | |
|
37 | 37 | |
|
38 | 38 | with nt.assert_raises(TypeError): |
|
39 | dlink((w1, 'value'), (w2, 'traits')) | |
|
39 | jsdlink((w1, 'value'), (w2, 'traits')) |
@@ -1,112 +1,111 b'' | |||
|
1 | 1 | """Link and DirectionalLink classes. |
|
2 | 2 | |
|
3 | 3 | Propagate changes between widgets on the javascript side |
|
4 | 4 | """ |
|
5 | 5 | |
|
6 | 6 | # Copyright (c) IPython Development Team. |
|
7 | 7 | # Distributed under the terms of the Modified BSD License. |
|
8 | 8 | |
|
9 | 9 | from .widget import Widget |
|
10 | 10 | from IPython.testing.skipdoctest import skip_doctest |
|
11 | 11 | from IPython.utils.traitlets import Unicode, Tuple, List,Instance, TraitError |
|
12 | 12 | |
|
13 | 13 | class WidgetTraitTuple(Tuple): |
|
14 | 14 | """Traitlet for validating a single (Widget, 'trait_name') pair""" |
|
15 | 15 | |
|
16 | 16 | def __init__(self, **kwargs): |
|
17 | 17 | super(WidgetTraitTuple, self).__init__(Instance(Widget), Unicode, **kwargs) |
|
18 | 18 | |
|
19 | 19 | def validate_elements(self, obj, value): |
|
20 | 20 | value = super(WidgetTraitTuple, self).validate_elements(obj, value) |
|
21 | 21 | widget, trait_name = value |
|
22 | 22 | trait = widget.traits().get(trait_name) |
|
23 | 23 | trait_repr = "%s.%s" % (widget.__class__.__name__, trait_name) |
|
24 | 24 | # Can't raise TraitError because the parent will swallow the message |
|
25 | 25 | # and throw it away in a new, less informative TraitError |
|
26 | 26 | if trait is None: |
|
27 | 27 | raise TypeError("No such trait: %s" % trait_repr) |
|
28 | 28 | elif not trait.get_metadata('sync'): |
|
29 | 29 | raise TypeError("%s cannot be synced" % trait_repr) |
|
30 | 30 | |
|
31 | 31 | return value |
|
32 | 32 | |
|
33 | 33 | |
|
34 | 34 | class Link(Widget): |
|
35 | 35 | """Link Widget |
|
36 | 36 | |
|
37 | 37 | one trait: |
|
38 | 38 | widgets, a list of (widget, 'trait_name') tuples which should be linked in the frontend. |
|
39 | 39 | """ |
|
40 | 40 | _model_name = Unicode('LinkModel', sync=True) |
|
41 | 41 | widgets = List(WidgetTraitTuple, sync=True) |
|
42 | 42 | |
|
43 | 43 | def __init__(self, widgets, **kwargs): |
|
44 | 44 | if len(widgets) < 2: |
|
45 | 45 | raise TypeError("Require at least two widgets to link") |
|
46 | 46 | kwargs['widgets'] = widgets |
|
47 | 47 | super(Link, self).__init__(**kwargs) |
|
48 | 48 | |
|
49 | 49 | # for compatibility with traitlet links |
|
50 | 50 | def unlink(self): |
|
51 | 51 | self.close() |
|
52 | 52 | |
|
53 | 53 | |
|
54 | 54 | @skip_doctest |
|
55 | def link(*args): | |
|
55 | def jslink(*args): | |
|
56 | 56 | """Link traits from different widgets together on the frontend so they remain in sync. |
|
57 | 57 | |
|
58 | 58 | Parameters |
|
59 | 59 | ---------- |
|
60 | 60 | *args : two or more (Widget, 'trait_name') tuples that should be kept in sync. |
|
61 | 61 | |
|
62 | 62 | Examples |
|
63 | 63 | -------- |
|
64 | 64 | |
|
65 | 65 | >>> c = link((widget1, 'value'), (widget2, 'value'), (widget3, 'value')) |
|
66 | 66 | """ |
|
67 | 67 | return Link(widgets=args) |
|
68 | 68 | |
|
69 | 69 | |
|
70 | 70 | class DirectionalLink(Widget): |
|
71 | 71 | """A directional link |
|
72 | 72 | |
|
73 | 73 | source: a (Widget, 'trait_name') tuple for the source trait |
|
74 | 74 | targets: one or more (Widget, 'trait_name') tuples that should be updated |
|
75 | 75 | when the source trait changes. |
|
76 | 76 | """ |
|
77 | 77 | _model_name = Unicode('DirectionalLinkModel', sync=True) |
|
78 | 78 | targets = List(WidgetTraitTuple, sync=True) |
|
79 | 79 | source = WidgetTraitTuple(sync=True) |
|
80 | 80 | |
|
81 | 81 | # Does not quite behave like other widgets but reproduces |
|
82 | 82 | # the behavior of IPython.utils.traitlets.directional_link |
|
83 | 83 | def __init__(self, source, targets, **kwargs): |
|
84 | 84 | if len(targets) < 1: |
|
85 | 85 | raise TypeError("Require at least two widgets to link") |
|
86 | 86 | |
|
87 | 87 | kwargs['source'] = source |
|
88 | 88 | kwargs['targets'] = targets |
|
89 | 89 | super(DirectionalLink, self).__init__(**kwargs) |
|
90 | 90 | |
|
91 | 91 | # for compatibility with traitlet links |
|
92 | 92 | def unlink(self): |
|
93 | 93 | self.close() |
|
94 | 94 | |
|
95 | 95 | @skip_doctest |
|
96 |
def d |
|
|
96 | def jsdlink(source, *targets): | |
|
97 | 97 | """Link the trait of a source widget with traits of target widgets in the frontend. |
|
98 | 98 | |
|
99 | 99 | Parameters |
|
100 | 100 | ---------- |
|
101 | 101 | source : a (Widget, 'trait_name') tuple for the source trait |
|
102 | 102 | *targets : one or more (Widget, 'trait_name') tuples that should be updated |
|
103 | 103 | when the source trait changes. |
|
104 | 104 | |
|
105 | 105 | Examples |
|
106 | 106 | -------- |
|
107 | 107 | |
|
108 | 108 | >>> c = dlink((src_widget, 'value'), (tgt_widget1, 'value'), (tgt_widget2, 'value')) |
|
109 | 109 | """ |
|
110 | 110 | return DirectionalLink(source=source, targets=targets) |
|
111 | 111 | |
|
112 | dlink = directional_link |
General Comments 0
You need to be logged in to leave comments.
Login now