pyglove 0.4.5.dev20240318__py3-none-any.whl → 0.4.5.dev202501132210__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyglove/core/__init__.py +54 -20
- pyglove/core/coding/__init__.py +42 -0
- pyglove/core/coding/errors.py +111 -0
- pyglove/core/coding/errors_test.py +98 -0
- pyglove/core/coding/execution.py +309 -0
- pyglove/core/coding/execution_test.py +333 -0
- pyglove/core/{object_utils/codegen.py → coding/function_generation.py} +10 -4
- pyglove/core/{object_utils/codegen_test.py → coding/function_generation_test.py} +5 -7
- pyglove/core/coding/parsing.py +153 -0
- pyglove/core/coding/parsing_test.py +150 -0
- pyglove/core/coding/permissions.py +100 -0
- pyglove/core/coding/permissions_test.py +93 -0
- pyglove/core/geno/base.py +54 -41
- pyglove/core/geno/base_test.py +2 -4
- pyglove/core/geno/categorical.py +37 -28
- pyglove/core/geno/custom.py +19 -16
- pyglove/core/geno/numerical.py +20 -17
- pyglove/core/geno/space.py +4 -5
- pyglove/core/hyper/base.py +6 -6
- pyglove/core/hyper/categorical.py +94 -55
- pyglove/core/hyper/custom.py +7 -7
- pyglove/core/hyper/custom_test.py +9 -10
- pyglove/core/hyper/derived.py +30 -22
- pyglove/core/hyper/derived_test.py +2 -4
- pyglove/core/hyper/dynamic_evaluation.py +5 -6
- pyglove/core/hyper/evolvable.py +57 -46
- pyglove/core/hyper/numerical.py +48 -24
- pyglove/core/hyper/numerical_test.py +9 -9
- pyglove/core/hyper/object_template.py +58 -46
- pyglove/core/io/__init__.py +1 -0
- pyglove/core/io/file_system.py +17 -7
- pyglove/core/io/file_system_test.py +2 -0
- pyglove/core/io/sequence.py +299 -0
- pyglove/core/io/sequence_test.py +124 -0
- pyglove/core/logging_test.py +0 -2
- pyglove/core/patching/object_factory.py +4 -4
- pyglove/core/patching/pattern_based.py +4 -4
- pyglove/core/patching/rule_based.py +17 -5
- pyglove/core/patching/rule_based_test.py +27 -4
- pyglove/core/symbolic/__init__.py +2 -7
- pyglove/core/symbolic/base.py +320 -183
- pyglove/core/symbolic/base_test.py +123 -19
- pyglove/core/symbolic/boilerplate.py +7 -13
- pyglove/core/symbolic/boilerplate_test.py +25 -23
- pyglove/core/symbolic/class_wrapper.py +48 -45
- pyglove/core/symbolic/class_wrapper_test.py +2 -2
- pyglove/core/symbolic/compounding.py +9 -15
- pyglove/core/symbolic/compounding_test.py +2 -4
- pyglove/core/symbolic/dict.py +154 -110
- pyglove/core/symbolic/dict_test.py +238 -130
- pyglove/core/symbolic/diff.py +199 -10
- pyglove/core/symbolic/diff_test.py +226 -0
- pyglove/core/symbolic/flags.py +1 -1
- pyglove/core/symbolic/functor.py +29 -26
- pyglove/core/symbolic/functor_test.py +102 -50
- pyglove/core/symbolic/inferred.py +2 -2
- pyglove/core/symbolic/list.py +81 -50
- pyglove/core/symbolic/list_test.py +119 -97
- pyglove/core/symbolic/object.py +225 -113
- pyglove/core/symbolic/object_test.py +320 -108
- pyglove/core/symbolic/origin.py +17 -14
- pyglove/core/symbolic/origin_test.py +4 -2
- pyglove/core/symbolic/pure_symbolic.py +4 -3
- pyglove/core/symbolic/ref.py +108 -21
- pyglove/core/symbolic/ref_test.py +93 -0
- pyglove/core/symbolic/symbolize_test.py +10 -2
- pyglove/core/tuning/local_backend.py +2 -2
- pyglove/core/tuning/protocols.py +3 -3
- pyglove/core/tuning/sample_test.py +3 -3
- pyglove/core/typing/__init__.py +14 -5
- pyglove/core/typing/annotation_conversion.py +43 -27
- pyglove/core/typing/annotation_conversion_test.py +23 -0
- pyglove/core/typing/callable_ext.py +241 -3
- pyglove/core/typing/callable_ext_test.py +255 -0
- pyglove/core/typing/callable_signature.py +510 -66
- pyglove/core/typing/callable_signature_test.py +619 -99
- pyglove/core/typing/class_schema.py +229 -154
- pyglove/core/typing/class_schema_test.py +149 -95
- pyglove/core/typing/custom_typing.py +5 -4
- pyglove/core/typing/inspect.py +63 -0
- pyglove/core/typing/inspect_test.py +39 -0
- pyglove/core/typing/key_specs.py +10 -11
- pyglove/core/typing/key_specs_test.py +7 -4
- pyglove/core/typing/type_conversion.py +4 -5
- pyglove/core/typing/type_conversion_test.py +12 -12
- pyglove/core/typing/typed_missing.py +6 -7
- pyglove/core/typing/typed_missing_test.py +7 -8
- pyglove/core/typing/value_specs.py +604 -362
- pyglove/core/typing/value_specs_test.py +328 -90
- pyglove/core/utils/__init__.py +164 -0
- pyglove/core/{object_utils → utils}/common_traits.py +3 -67
- pyglove/core/utils/common_traits_test.py +36 -0
- pyglove/core/{object_utils → utils}/docstr_utils.py +23 -0
- pyglove/core/{object_utils → utils}/docstr_utils_test.py +36 -4
- pyglove/core/{object_utils → utils}/error_utils.py +78 -9
- pyglove/core/{object_utils → utils}/error_utils_test.py +61 -5
- pyglove/core/utils/formatting.py +464 -0
- pyglove/core/utils/formatting_test.py +453 -0
- pyglove/core/{object_utils → utils}/hierarchical.py +23 -25
- pyglove/core/{object_utils → utils}/hierarchical_test.py +3 -5
- pyglove/core/{object_utils → utils}/json_conversion.py +177 -52
- pyglove/core/{object_utils → utils}/json_conversion_test.py +97 -16
- pyglove/core/{object_utils → utils}/missing.py +3 -3
- pyglove/core/{object_utils → utils}/missing_test.py +2 -4
- pyglove/core/utils/text_color.py +128 -0
- pyglove/core/utils/text_color_test.py +94 -0
- pyglove/core/{object_utils → utils}/thread_local_test.py +1 -3
- pyglove/core/utils/timing.py +236 -0
- pyglove/core/utils/timing_test.py +154 -0
- pyglove/core/{object_utils → utils}/value_location.py +275 -6
- pyglove/core/utils/value_location_test.py +707 -0
- pyglove/core/views/__init__.py +32 -0
- pyglove/core/views/base.py +804 -0
- pyglove/core/views/base_test.py +580 -0
- pyglove/core/views/html/__init__.py +27 -0
- pyglove/core/views/html/base.py +547 -0
- pyglove/core/views/html/base_test.py +830 -0
- pyglove/core/views/html/controls/__init__.py +35 -0
- pyglove/core/views/html/controls/base.py +275 -0
- pyglove/core/views/html/controls/label.py +207 -0
- pyglove/core/views/html/controls/label_test.py +157 -0
- pyglove/core/views/html/controls/progress_bar.py +183 -0
- pyglove/core/views/html/controls/progress_bar_test.py +97 -0
- pyglove/core/views/html/controls/tab.py +320 -0
- pyglove/core/views/html/controls/tab_test.py +87 -0
- pyglove/core/views/html/controls/tooltip.py +99 -0
- pyglove/core/views/html/controls/tooltip_test.py +99 -0
- pyglove/core/views/html/tree_view.py +1517 -0
- pyglove/core/views/html/tree_view_test.py +1461 -0
- {pyglove-0.4.5.dev20240318.dist-info → pyglove-0.4.5.dev202501132210.dist-info}/METADATA +18 -4
- pyglove-0.4.5.dev202501132210.dist-info/RECORD +214 -0
- {pyglove-0.4.5.dev20240318.dist-info → pyglove-0.4.5.dev202501132210.dist-info}/WHEEL +1 -1
- pyglove/core/object_utils/__init__.py +0 -154
- pyglove/core/object_utils/common_traits_test.py +0 -82
- pyglove/core/object_utils/formatting.py +0 -234
- pyglove/core/object_utils/formatting_test.py +0 -223
- pyglove/core/object_utils/value_location_test.py +0 -385
- pyglove/core/symbolic/schema_utils.py +0 -327
- pyglove/core/symbolic/schema_utils_test.py +0 -57
- pyglove/core/typing/class_schema_utils.py +0 -202
- pyglove/core/typing/class_schema_utils_test.py +0 -194
- pyglove-0.4.5.dev20240318.dist-info/RECORD +0 -185
- /pyglove/core/{object_utils → utils}/thread_local.py +0 -0
- {pyglove-0.4.5.dev20240318.dist-info → pyglove-0.4.5.dev202501132210.dist-info}/LICENSE +0 -0
- {pyglove-0.4.5.dev20240318.dist-info → pyglove-0.4.5.dev202501132210.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,183 @@
|
|
1
|
+
# Copyright 2024 The PyGlove Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
"""Progress bar control."""
|
15
|
+
|
16
|
+
import functools
|
17
|
+
from typing import Annotated, List, Optional, Union
|
18
|
+
|
19
|
+
from pyglove.core import utils
|
20
|
+
from pyglove.core.symbolic import object as pg_object
|
21
|
+
# pylint: disable=g-importing-member
|
22
|
+
from pyglove.core.views.html.base import Html
|
23
|
+
from pyglove.core.views.html.controls.base import HtmlControl
|
24
|
+
from pyglove.core.views.html.controls.label import Label
|
25
|
+
# pylint: enable=g-importing-member
|
26
|
+
|
27
|
+
|
28
|
+
@pg_object.use_init_args(
|
29
|
+
['name', 'value', 'id', 'css_classes', 'styles']
|
30
|
+
)
|
31
|
+
class SubProgress(HtmlControl):
|
32
|
+
"""A sub progress bar control."""
|
33
|
+
|
34
|
+
name: Annotated[
|
35
|
+
str, 'The name of the sub progress bar.'
|
36
|
+
]
|
37
|
+
|
38
|
+
value: Annotated[
|
39
|
+
int, 'The value of the sub progress bar.'
|
40
|
+
] = 0
|
41
|
+
|
42
|
+
interactive = True
|
43
|
+
|
44
|
+
def _on_parent_change(self, *args, **kwargs):
|
45
|
+
super()._on_parent_change(*args, **kwargs) # pytype: disable=attribute-error
|
46
|
+
self.__dict__.pop('parent', None)
|
47
|
+
|
48
|
+
@functools.cached_property
|
49
|
+
def parent(self) -> Optional['ProgressBar']:
|
50
|
+
"""Returns the parent progress bar."""
|
51
|
+
return self.sym_ancestor(
|
52
|
+
lambda x: isinstance(x, ProgressBar)
|
53
|
+
)
|
54
|
+
|
55
|
+
@property
|
56
|
+
def total(self) -> Optional[int]:
|
57
|
+
"""Returns the total number of the sub progress bar."""
|
58
|
+
assert self.parent is not None
|
59
|
+
return self.parent.total
|
60
|
+
|
61
|
+
@property
|
62
|
+
def width(self) -> Optional[str]:
|
63
|
+
"""Returns the width of the sub progress bar."""
|
64
|
+
if self.total is None:
|
65
|
+
return None
|
66
|
+
return f'{self.value / self.total:.0%}'
|
67
|
+
|
68
|
+
def _to_html(self, **kwargs) -> Html:
|
69
|
+
styles = self.styles.copy()
|
70
|
+
styles.update(width=self.width)
|
71
|
+
return Html.element(
|
72
|
+
'div',
|
73
|
+
[],
|
74
|
+
id=self.element_id(),
|
75
|
+
styles=styles,
|
76
|
+
css_classes=['sub-progress', utils.camel_to_snake(self.name, '-')]
|
77
|
+
+ self.css_classes,
|
78
|
+
)
|
79
|
+
|
80
|
+
def increment(self, delta: int = 1):
|
81
|
+
"""Increments the value of the sub progress bar."""
|
82
|
+
self.update(self.value + delta)
|
83
|
+
|
84
|
+
def update(self, value: Optional[int] = None):
|
85
|
+
if value is not None:
|
86
|
+
self.rebind(
|
87
|
+
value=value, skip_notification=True, raise_on_no_change=False
|
88
|
+
)
|
89
|
+
self._update_style(dict(width=self.width))
|
90
|
+
self.parent.update()
|
91
|
+
|
92
|
+
|
93
|
+
@pg_object.use_init_args(
|
94
|
+
['subprogresses', 'total', 'id', 'css_classes', 'styles']
|
95
|
+
)
|
96
|
+
class ProgressBar(HtmlControl):
|
97
|
+
"""A progress bar control."""
|
98
|
+
|
99
|
+
subprogresses: Annotated[
|
100
|
+
List[SubProgress],
|
101
|
+
'The sub progress bars of the progress bar.'
|
102
|
+
]
|
103
|
+
|
104
|
+
total: Annotated[
|
105
|
+
Optional[int],
|
106
|
+
(
|
107
|
+
'The total number of steps represented by the progress bar.'
|
108
|
+
'If None, the progress steps are not determined.'
|
109
|
+
)
|
110
|
+
] = None
|
111
|
+
|
112
|
+
interactive = True
|
113
|
+
|
114
|
+
def _on_bound(self):
|
115
|
+
super()._on_bound()
|
116
|
+
self._progress_label = Label(
|
117
|
+
text=self._progress_text(),
|
118
|
+
tooltip=self._progress_tooltip(),
|
119
|
+
css_classes=['progress-label'],
|
120
|
+
interactive=True,
|
121
|
+
)
|
122
|
+
|
123
|
+
def _progress_text(self) -> Union[str, Html]:
|
124
|
+
completed = sum(s.value for s in self.subprogresses)
|
125
|
+
if self.total is None:
|
126
|
+
return 'n/a'
|
127
|
+
complete_rate = completed / self.total
|
128
|
+
return f'{complete_rate: .1%} ({completed}/{self.total})'
|
129
|
+
|
130
|
+
def _progress_tooltip(self) -> str:
|
131
|
+
if self.total is None:
|
132
|
+
return 'Not started'
|
133
|
+
assert self.total > 0
|
134
|
+
return '\n'.join([
|
135
|
+
f'{s.name}: {s.value / self.total:.1%} ({s.value}/{self.total})'
|
136
|
+
for s in self.subprogresses
|
137
|
+
])
|
138
|
+
|
139
|
+
def _to_html(self, **kwargs) -> Html:
|
140
|
+
return Html.element(
|
141
|
+
'div',
|
142
|
+
[
|
143
|
+
Html.element('div', self.subprogresses, css_classes=['shade']),
|
144
|
+
self._progress_label,
|
145
|
+
],
|
146
|
+
css_classes=['progress-bar']
|
147
|
+
).add_style(
|
148
|
+
"""
|
149
|
+
.progress-bar {
|
150
|
+
display: inline-block;
|
151
|
+
}
|
152
|
+
.progress-bar .shade {
|
153
|
+
position: relative;
|
154
|
+
background-color: #EEE;
|
155
|
+
display: inline-flex;
|
156
|
+
margin: 10px;
|
157
|
+
width: 100px;
|
158
|
+
height: 5px;
|
159
|
+
border-radius: 5px;
|
160
|
+
overflow: hidden;
|
161
|
+
}
|
162
|
+
.progress-bar .sub-progress {
|
163
|
+
background-color: dodgerblue;
|
164
|
+
height: 10px;
|
165
|
+
width: 0%;
|
166
|
+
border-radius: 0px;
|
167
|
+
}
|
168
|
+
"""
|
169
|
+
)
|
170
|
+
|
171
|
+
def update(self, total: Optional[int] = None):
|
172
|
+
if total is not None:
|
173
|
+
assert self.total is None
|
174
|
+
self._sync_members(total=total)
|
175
|
+
self._progress_label.update(
|
176
|
+
self._progress_text(), tooltip=self._progress_tooltip()
|
177
|
+
)
|
178
|
+
|
179
|
+
def __getitem__(self, name: str) -> SubProgress:
|
180
|
+
for sub in self.subprogresses:
|
181
|
+
if sub.name == name:
|
182
|
+
return sub
|
183
|
+
raise KeyError(f'Sub progress bar {name!r} not found.')
|
@@ -0,0 +1,97 @@
|
|
1
|
+
# Copyright 2024 The PyGlove Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
import inspect
|
15
|
+
import unittest
|
16
|
+
|
17
|
+
from pyglove.core import symbolic # pylint: disable=unused-import
|
18
|
+
from pyglove.core.views.html.controls import progress_bar
|
19
|
+
|
20
|
+
|
21
|
+
class ProgressBarTest(unittest.TestCase):
|
22
|
+
|
23
|
+
def assert_html_content(self, control, expected):
|
24
|
+
expected = inspect.cleandoc(expected).strip()
|
25
|
+
actual = control.to_html().content.strip()
|
26
|
+
if actual != expected:
|
27
|
+
print(actual)
|
28
|
+
self.assertEqual(actual, expected)
|
29
|
+
|
30
|
+
def test_basic(self):
|
31
|
+
bar = progress_bar.ProgressBar(
|
32
|
+
subprogresses=[
|
33
|
+
progress_bar.SubProgress('foo'),
|
34
|
+
progress_bar.SubProgress('bar', 20),
|
35
|
+
],
|
36
|
+
total=None,
|
37
|
+
)
|
38
|
+
self.assert_html_content(
|
39
|
+
bar,
|
40
|
+
(
|
41
|
+
'<div class="progress-bar"><div class="shade">'
|
42
|
+
f'<div class="sub-progress foo" id="{bar["foo"].element_id()}">'
|
43
|
+
'</div><div class="sub-progress bar" '
|
44
|
+
f'id="{bar["bar"].element_id()}"></div></div>'
|
45
|
+
'<div class="label-container"><span class="label progress-label"'
|
46
|
+
f' id="{bar._progress_label.element_id()}">n/a</span><span class='
|
47
|
+
f'"tooltip" id="{bar._progress_label.tooltip.element_id()}">'
|
48
|
+
'Not started</span></div></div>'
|
49
|
+
)
|
50
|
+
)
|
51
|
+
self.assertEqual(bar['foo'], progress_bar.SubProgress('foo'))
|
52
|
+
self.assertEqual(bar['bar'], progress_bar.SubProgress('bar', 20))
|
53
|
+
with self.assertRaisesRegex(KeyError, 'Sub progress bar .* not found'):
|
54
|
+
_ = bar['baz']
|
55
|
+
self.assertIsNone(bar['foo'].total)
|
56
|
+
self.assertIsNone(bar['foo'].width)
|
57
|
+
|
58
|
+
bar.update(total=100)
|
59
|
+
self.assertEqual(bar.total, 100)
|
60
|
+
self.assertEqual(bar['foo'].total, 100)
|
61
|
+
self.assertEqual(bar['foo'].width, '0%')
|
62
|
+
self.assertEqual(bar['bar'].width, '20%')
|
63
|
+
with bar.track_scripts() as scripts:
|
64
|
+
bar['foo'].increment()
|
65
|
+
self.assertEqual(
|
66
|
+
scripts,
|
67
|
+
[
|
68
|
+
inspect.cleandoc(
|
69
|
+
f"""
|
70
|
+
elem = document.getElementById("{bar['foo'].element_id()}");
|
71
|
+
elem.style = "width:1%;";
|
72
|
+
"""
|
73
|
+
),
|
74
|
+
inspect.cleandoc(
|
75
|
+
f"""
|
76
|
+
elem = document.getElementById("{bar._progress_label.element_id()}");
|
77
|
+
elem.textContent = " 21.0% (21/100)";
|
78
|
+
"""
|
79
|
+
),
|
80
|
+
inspect.cleandoc(
|
81
|
+
f"""
|
82
|
+
elem = document.getElementById("{bar._progress_label.tooltip.element_id()}");
|
83
|
+
elem.textContent = "foo: 1.0% (1/100)\\nbar: 20.0% (20/100)";
|
84
|
+
"""
|
85
|
+
),
|
86
|
+
inspect.cleandoc(
|
87
|
+
f"""
|
88
|
+
elem = document.getElementById("{bar._progress_label.tooltip.element_id()}");
|
89
|
+
elem.classList.remove("html-content");
|
90
|
+
"""
|
91
|
+
),
|
92
|
+
]
|
93
|
+
)
|
94
|
+
|
95
|
+
|
96
|
+
if __name__ == '__main__':
|
97
|
+
unittest.main()
|
@@ -0,0 +1,320 @@
|
|
1
|
+
# Copyright 2024 The PyGlove Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
"""Tab control."""
|
15
|
+
|
16
|
+
from typing import Annotated, List, Literal, Optional, Union
|
17
|
+
|
18
|
+
from pyglove.core.symbolic import flags as pg_flags
|
19
|
+
from pyglove.core.symbolic import object as pg_object
|
20
|
+
|
21
|
+
# pylint: disable=g-importing-member
|
22
|
+
from pyglove.core.views.html.base import Html
|
23
|
+
from pyglove.core.views.html.base import HtmlConvertible
|
24
|
+
from pyglove.core.views.html.controls.base import HtmlControl
|
25
|
+
from pyglove.core.views.html.controls.label import Label
|
26
|
+
# pylint: enable=g-importing-member
|
27
|
+
|
28
|
+
|
29
|
+
class Tab(pg_object.Object):
|
30
|
+
"""A tab."""
|
31
|
+
|
32
|
+
label: Annotated[
|
33
|
+
Label,
|
34
|
+
'The label of the tab.'
|
35
|
+
]
|
36
|
+
|
37
|
+
content: Annotated[
|
38
|
+
Union[Html, HtmlConvertible],
|
39
|
+
'The content of the tab.'
|
40
|
+
]
|
41
|
+
|
42
|
+
css_classes: Annotated[
|
43
|
+
List[str],
|
44
|
+
'The CSS classes of the tab.'
|
45
|
+
] = []
|
46
|
+
|
47
|
+
name: Annotated[
|
48
|
+
Optional[str],
|
49
|
+
'An optional name that can be used to identify a tab under a tab control'
|
50
|
+
] = None
|
51
|
+
|
52
|
+
|
53
|
+
@pg_object.use_init_args(
|
54
|
+
['tabs', 'selected', 'tab_position', 'id', 'css_classes', 'styles']
|
55
|
+
)
|
56
|
+
class TabControl(HtmlControl):
|
57
|
+
"""A tab control."""
|
58
|
+
|
59
|
+
tabs: Annotated[
|
60
|
+
List[Tab],
|
61
|
+
'The tabs of the tab control.'
|
62
|
+
]
|
63
|
+
|
64
|
+
selected: Annotated[
|
65
|
+
int,
|
66
|
+
'The index of the selected tab.'
|
67
|
+
] = 0
|
68
|
+
|
69
|
+
tab_position: Annotated[
|
70
|
+
Literal['top', 'left'],
|
71
|
+
'The direction of the tab control.'
|
72
|
+
] = 'top'
|
73
|
+
|
74
|
+
interactive = True
|
75
|
+
|
76
|
+
def append(self, tab: Tab) -> None:
|
77
|
+
with pg_flags.notify_on_change(False):
|
78
|
+
self.tabs.append(tab)
|
79
|
+
|
80
|
+
self._insert_adjacent_html(
|
81
|
+
f"""
|
82
|
+
const elem = document.getElementById('{self.element_id()}-button-group');
|
83
|
+
""",
|
84
|
+
self._tab_button(tab, len(self.tabs) - 1),
|
85
|
+
position='beforeend',
|
86
|
+
)
|
87
|
+
self._insert_adjacent_html(
|
88
|
+
f"""
|
89
|
+
const elem = document.getElementById('{self.element_id()}-content-group');
|
90
|
+
""",
|
91
|
+
self._tab_content(tab, len(self.tabs) - 1),
|
92
|
+
position='beforeend',
|
93
|
+
)
|
94
|
+
|
95
|
+
def insert(self, index_or_name: Union[int, str], tab: Tab) -> None:
|
96
|
+
"""Inserts a tab before a tab identified by index or name."""
|
97
|
+
index = self.indexof(index_or_name)
|
98
|
+
if index == -1:
|
99
|
+
raise ValueError(f'Tab not found: {index_or_name!r}')
|
100
|
+
with pg_flags.notify_on_change(False):
|
101
|
+
self.tabs.insert(index, tab)
|
102
|
+
|
103
|
+
self._insert_adjacent_html(
|
104
|
+
f"""
|
105
|
+
const elem = document.querySelectorAll('#{self.element_id()}-button-group > .tab-button')[{index}];
|
106
|
+
""",
|
107
|
+
self._tab_button(tab, len(self.tabs) - 1),
|
108
|
+
position='beforebegin',
|
109
|
+
)
|
110
|
+
self._insert_adjacent_html(
|
111
|
+
f"""
|
112
|
+
const elem = document.querySelectorAll('#{self.element_id()}-content-group > .tab-content')[{index}];
|
113
|
+
""",
|
114
|
+
self._tab_content(tab, len(self.tabs) - 1),
|
115
|
+
position='beforebegin',
|
116
|
+
)
|
117
|
+
|
118
|
+
def indexof(self, index_or_name: Union[int, str]) -> int:
|
119
|
+
if isinstance(index_or_name, int):
|
120
|
+
index = index_or_name
|
121
|
+
if index >= len(self.tabs):
|
122
|
+
return len(self.tabs) - 1
|
123
|
+
elif index < -len(self.tabs):
|
124
|
+
return -1
|
125
|
+
elif index < 0:
|
126
|
+
index = index + len(self.tabs)
|
127
|
+
return index
|
128
|
+
else:
|
129
|
+
name = index_or_name
|
130
|
+
assert isinstance(name, str), name
|
131
|
+
for i, tab in enumerate(self.tabs):
|
132
|
+
if tab.name == name:
|
133
|
+
return i
|
134
|
+
return -1
|
135
|
+
|
136
|
+
def extend(self, tabs: List[Tab]) -> None:
|
137
|
+
for tab in tabs:
|
138
|
+
self.append(tab)
|
139
|
+
|
140
|
+
def select(
|
141
|
+
self,
|
142
|
+
index_or_name: Union[int, str, List[str]]) -> Union[int, str]:
|
143
|
+
"""Selects a tab identified by an index or name.
|
144
|
+
|
145
|
+
Args:
|
146
|
+
index_or_name: The index or name of the tab to select. If a list of names
|
147
|
+
is provided, the first name in the list that is found will be selected.
|
148
|
+
|
149
|
+
Returns:
|
150
|
+
The index (if the index was provided) or name of the selected tab.
|
151
|
+
"""
|
152
|
+
selected_name = index_or_name if isinstance(index_or_name, str) else None
|
153
|
+
index = -1
|
154
|
+
if isinstance(index_or_name, list):
|
155
|
+
for name in index_or_name:
|
156
|
+
index = self.indexof(name)
|
157
|
+
if index != -1:
|
158
|
+
selected_name = name
|
159
|
+
break
|
160
|
+
else:
|
161
|
+
index = self.indexof(index_or_name)
|
162
|
+
if index == -1:
|
163
|
+
raise ValueError(f'Tab not found: {index_or_name!r}')
|
164
|
+
self._sync_members(selected=index)
|
165
|
+
self._run_javascript(
|
166
|
+
f"""
|
167
|
+
const tabButtons = document.querySelectorAll('#{self.element_id()}-button-group > .tab-button');
|
168
|
+
tabButtons[{index}].click();
|
169
|
+
"""
|
170
|
+
)
|
171
|
+
return selected_name or index
|
172
|
+
|
173
|
+
def _to_html(self, **kwargs):
|
174
|
+
return Html.element(
|
175
|
+
'table',
|
176
|
+
[
|
177
|
+
'<tr><td>',
|
178
|
+
Html.element(
|
179
|
+
'div',
|
180
|
+
[self._tab_button(tab, i) for i, tab in enumerate(self.tabs)],
|
181
|
+
css_classes=[
|
182
|
+
'tab-button-group',
|
183
|
+
self.tab_position
|
184
|
+
] + self.css_classes,
|
185
|
+
id=self.element_id('button-group'),
|
186
|
+
),
|
187
|
+
('</td><td>' if self.tab_position == 'left'
|
188
|
+
else '</td></tr><tr><td>'),
|
189
|
+
Html.element(
|
190
|
+
'div',
|
191
|
+
[self._tab_content(tab, i) for i, tab in enumerate(self.tabs)],
|
192
|
+
css_classes=[
|
193
|
+
'tab-content-group',
|
194
|
+
self.tab_position
|
195
|
+
] + self.css_classes,
|
196
|
+
id=self.element_id('content-group'),
|
197
|
+
),
|
198
|
+
'</td></tr>'
|
199
|
+
],
|
200
|
+
css_classes=['tab-control'],
|
201
|
+
styles=self.styles,
|
202
|
+
).add_script(
|
203
|
+
"""
|
204
|
+
function openTab(event, controlId, tabId) {
|
205
|
+
const tabButtons = document.querySelectorAll('#' + controlId + '-button-group > .tab-button');
|
206
|
+
for (let i = 0; i < tabButtons.length; i++) {
|
207
|
+
tabButtons[i].classList.remove('selected');
|
208
|
+
}
|
209
|
+
const tabContents = document.querySelectorAll('#' + controlId + '-content-group > .tab-content');
|
210
|
+
for (let i = 0; i < tabContents.length; i++) {
|
211
|
+
tabContents[i].classList.remove('selected')
|
212
|
+
}
|
213
|
+
const tabButton = event.currentTarget;
|
214
|
+
tabButton.classList.add('selected');
|
215
|
+
document.getElementById(tabId).classList.add('selected');
|
216
|
+
}
|
217
|
+
"""
|
218
|
+
).add_style(
|
219
|
+
"""
|
220
|
+
.tab-control {
|
221
|
+
border-spacing: 0px;
|
222
|
+
border-collapse: collapse;
|
223
|
+
margin-top: 10px;
|
224
|
+
}
|
225
|
+
.tab-control td {
|
226
|
+
padding: 0px;
|
227
|
+
margin: 0px;
|
228
|
+
vertical-align: top;
|
229
|
+
}
|
230
|
+
.top.tab-button-group {
|
231
|
+
border-left: 1px solid #DDD;
|
232
|
+
border-top: 1px solid #DDD;
|
233
|
+
border-right: 1px solid #DDD;
|
234
|
+
border-radius: 5px 5px 0px 0px;
|
235
|
+
padding: 0px 5px 0px 0px;
|
236
|
+
margin-bottom: -2px;
|
237
|
+
}
|
238
|
+
.tab-button {
|
239
|
+
background-color: transparent;
|
240
|
+
border-radius: 5px;
|
241
|
+
border: 0px;
|
242
|
+
font-weight: bold;
|
243
|
+
color: gray;
|
244
|
+
outline: none;
|
245
|
+
cursor: pointer;
|
246
|
+
transition: 0.3s;
|
247
|
+
}
|
248
|
+
.tab-button:hover {
|
249
|
+
background-color: #fff1dd;
|
250
|
+
}
|
251
|
+
.tab-button.selected {
|
252
|
+
background-color: #f0ecf9;
|
253
|
+
color: black;
|
254
|
+
}
|
255
|
+
.top.tab-content-group {
|
256
|
+
border-left: 1px solid #DDD;
|
257
|
+
border-right: 1px solid #DDD;
|
258
|
+
border-bottom: 1px solid #DDD;
|
259
|
+
border-radius: 0px 0px 5px 5px;
|
260
|
+
margin: 0px;
|
261
|
+
padding: 5px;
|
262
|
+
height: 100%;
|
263
|
+
}
|
264
|
+
.top > .tab-button {
|
265
|
+
margin: 5px 0px 5px 5px;
|
266
|
+
}
|
267
|
+
.tab-content {
|
268
|
+
display: none;
|
269
|
+
}
|
270
|
+
.tab-content.selected {
|
271
|
+
display: block;
|
272
|
+
}
|
273
|
+
.left.tab-button-group {
|
274
|
+
display: inline-flex;
|
275
|
+
flex-direction: column;
|
276
|
+
border: 1px solid #DDD;
|
277
|
+
border-radius: 5px;
|
278
|
+
margin-right: 5px;
|
279
|
+
padding: 0px 0px 5px 0px;
|
280
|
+
}
|
281
|
+
.left.tab-content-group {
|
282
|
+
border: 0px
|
283
|
+
margin: 0px;
|
284
|
+
padding: 0px;
|
285
|
+
height: 100%;
|
286
|
+
}
|
287
|
+
.left > .tab-button {
|
288
|
+
text-align: left;
|
289
|
+
margin: 5px 5px 0px 5px;
|
290
|
+
}
|
291
|
+
"""
|
292
|
+
)
|
293
|
+
|
294
|
+
def _tab_button(self, tab: Tab, i: int) -> Html:
|
295
|
+
return Html.element(
|
296
|
+
'button',
|
297
|
+
[
|
298
|
+
tab.label
|
299
|
+
],
|
300
|
+
css_classes=[
|
301
|
+
'tab-button',
|
302
|
+
'selected' if i == self.selected else None
|
303
|
+
] + tab.css_classes,
|
304
|
+
onclick=(
|
305
|
+
f"""openTab(event, '{self.element_id()}', '{self.element_id(str(i))}')"""
|
306
|
+
)
|
307
|
+
)
|
308
|
+
|
309
|
+
def _tab_content(self, tab: Tab, i: int) -> Html:
|
310
|
+
return Html.element(
|
311
|
+
'div',
|
312
|
+
[
|
313
|
+
tab.content
|
314
|
+
],
|
315
|
+
css_classes=[
|
316
|
+
'tab-content',
|
317
|
+
'selected' if i == self.selected else None
|
318
|
+
] + tab.css_classes,
|
319
|
+
id=self.element_id(str(i))
|
320
|
+
)
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# Copyright 2024 The PyGlove Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
import inspect
|
15
|
+
import unittest
|
16
|
+
|
17
|
+
from pyglove.core import symbolic # pylint: disable=unused-import
|
18
|
+
from pyglove.core.views.html import base
|
19
|
+
from pyglove.core.views.html.controls import tab as tab_lib
|
20
|
+
|
21
|
+
|
22
|
+
class TabControlTest(unittest.TestCase):
|
23
|
+
|
24
|
+
def assert_html_content(self, control, expected):
|
25
|
+
expected = inspect.cleandoc(expected).strip()
|
26
|
+
actual = control.to_html().content.strip()
|
27
|
+
if actual != expected:
|
28
|
+
print(actual)
|
29
|
+
self.assertEqual(actual, expected)
|
30
|
+
|
31
|
+
def test_basic(self):
|
32
|
+
tab = tab_lib.TabControl([
|
33
|
+
tab_lib.Tab('foo', base.Html('<h1>foo</h1>'), css_classes=['foo']),
|
34
|
+
tab_lib.Tab('bar', base.Html('<h1>bar</h1>')),
|
35
|
+
])
|
36
|
+
elem_id = tab.element_id()
|
37
|
+
self.assert_html_content(
|
38
|
+
tab,
|
39
|
+
f"""<table class="tab-control"><tr><td><div class="tab-button-group top" id="{elem_id}-button-group"><button class="tab-button selected foo" onclick="openTab(event, '{elem_id}', '{elem_id}-0')"><span class="label">foo</span></button><button class="tab-button" onclick="openTab(event, '{elem_id}', '{elem_id}-1')"><span class="label">bar</span></button></div></td></tr><tr><td><div class="tab-content-group top" id="{elem_id}-content-group"><div class="tab-content selected foo" id="{elem_id}-0"><h1>foo</h1></div><div class="tab-content" id="{elem_id}-1"><h1>bar</h1></div></div></td></tr></table>"""
|
40
|
+
)
|
41
|
+
with tab.track_scripts() as scripts:
|
42
|
+
tab.extend([
|
43
|
+
tab_lib.Tab(
|
44
|
+
'baz',
|
45
|
+
base.Html(
|
46
|
+
'<h1 class="a">baz</h1>').add_style('.a { color: red; }')
|
47
|
+
),
|
48
|
+
tab_lib.Tab('qux', base.Html('<h1>qux</h1>')),
|
49
|
+
])
|
50
|
+
self.assertEqual(len(scripts), 6)
|
51
|
+
with tab.track_scripts() as scripts:
|
52
|
+
tab.insert(0, tab_lib.Tab('x', 'foo', name='x'))
|
53
|
+
self.assertEqual(len(scripts), 2)
|
54
|
+
self.assertEqual(len(tab.tabs), 5)
|
55
|
+
self.assertEqual(tab.indexof(-1), 4)
|
56
|
+
self.assertEqual(tab.indexof(3), 3)
|
57
|
+
self.assertEqual(tab.indexof(10), 4)
|
58
|
+
self.assertEqual(tab.indexof(-10), -1)
|
59
|
+
self.assertEqual(tab.indexof('x'), 0)
|
60
|
+
self.assertEqual(tab.indexof('y'), -1)
|
61
|
+
self.assertEqual(tab.select(0), 0)
|
62
|
+
self.assertEqual(tab.select('x'), 'x')
|
63
|
+
self.assertEqual(tab.select(['y', 'x']), 'x')
|
64
|
+
|
65
|
+
with self.assertRaisesRegex(ValueError, 'Tab not found'):
|
66
|
+
tab.select('y')
|
67
|
+
with self.assertRaisesRegex(ValueError, 'Tab not found'):
|
68
|
+
tab.insert('y', tab_lib.Tab('z', 'bar'))
|
69
|
+
|
70
|
+
with tab.track_scripts() as scripts:
|
71
|
+
tab.insert('x', tab_lib.Tab('y', 'bar'))
|
72
|
+
self.assertEqual(len(scripts), 2)
|
73
|
+
self.assertEqual(len(tab.tabs), 6)
|
74
|
+
|
75
|
+
with tab.track_scripts() as scripts:
|
76
|
+
tab.select(3)
|
77
|
+
self.assertEqual(len(scripts), 1)
|
78
|
+
self.assertEqual(tab.selected, 3)
|
79
|
+
|
80
|
+
with tab.track_scripts() as scripts:
|
81
|
+
tab.select('x')
|
82
|
+
self.assertEqual(len(scripts), 1)
|
83
|
+
self.assertEqual(tab.selected, 1)
|
84
|
+
|
85
|
+
|
86
|
+
if __name__ == '__main__':
|
87
|
+
unittest.main()
|