pyglove 0.4.5.dev202411050809__py3-none-any.whl → 0.4.5.dev202411060809__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.
Potentially problematic release.
This version of pyglove might be problematic. Click here for more details.
- pyglove/core/__init__.py +8 -0
- pyglove/core/views/base_test.py +1 -1
- pyglove/core/views/html/__init__.py +0 -1
- pyglove/core/views/html/base.py +1 -1
- pyglove/core/views/html/base_test.py +1 -1
- pyglove/core/views/html/controls/__init__.py +35 -0
- pyglove/core/views/html/controls/base.py +238 -0
- pyglove/core/views/html/controls/label.py +190 -0
- pyglove/core/views/html/controls/label_test.py +138 -0
- pyglove/core/views/html/controls/progress_bar.py +185 -0
- pyglove/core/views/html/controls/progress_bar_test.py +97 -0
- pyglove/core/views/html/controls/tab.py +169 -0
- pyglove/core/views/html/controls/tab_test.py +54 -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 +1 -1
- pyglove/core/views/html/tree_view_test.py +1 -2
- {pyglove-0.4.5.dev202411050809.dist-info → pyglove-0.4.5.dev202411060809.dist-info}/METADATA +1 -1
- {pyglove-0.4.5.dev202411050809.dist-info → pyglove-0.4.5.dev202411060809.dist-info}/RECORD +22 -12
- {pyglove-0.4.5.dev202411050809.dist-info → pyglove-0.4.5.dev202411060809.dist-info}/LICENSE +0 -0
- {pyglove-0.4.5.dev202411050809.dist-info → pyglove-0.4.5.dev202411060809.dist-info}/WHEEL +0 -0
- {pyglove-0.4.5.dev202411050809.dist-info → pyglove-0.4.5.dev202411060809.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,185 @@
|
|
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 object_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=[
|
77
|
+
'sub-progress',
|
78
|
+
object_utils.camel_to_snake(self.name, '-')
|
79
|
+
] + self.css_classes,
|
80
|
+
)
|
81
|
+
|
82
|
+
def increment(self, delta: int = 1):
|
83
|
+
"""Increments the value of the sub progress bar."""
|
84
|
+
self.update(self.value + delta)
|
85
|
+
|
86
|
+
def update(self, value: Optional[int] = None):
|
87
|
+
if value is not None:
|
88
|
+
self.rebind(
|
89
|
+
value=value, skip_notification=True, raise_on_no_change=False
|
90
|
+
)
|
91
|
+
self._update_style(dict(width=self.width))
|
92
|
+
self.parent.update()
|
93
|
+
|
94
|
+
|
95
|
+
@pg_object.use_init_args(
|
96
|
+
['subprogresses', 'total', 'id', 'css_classes', 'styles']
|
97
|
+
)
|
98
|
+
class ProgressBar(HtmlControl):
|
99
|
+
"""A progress bar control."""
|
100
|
+
|
101
|
+
subprogresses: Annotated[
|
102
|
+
List[SubProgress],
|
103
|
+
'The sub progress bars of the progress bar.'
|
104
|
+
]
|
105
|
+
|
106
|
+
total: Annotated[
|
107
|
+
Optional[int],
|
108
|
+
(
|
109
|
+
'The total number of steps represented by the progress bar.'
|
110
|
+
'If None, the progress steps are not determined.'
|
111
|
+
)
|
112
|
+
] = None
|
113
|
+
|
114
|
+
interactive = True
|
115
|
+
|
116
|
+
def _on_bound(self):
|
117
|
+
super()._on_bound()
|
118
|
+
self._progress_label = Label(
|
119
|
+
text=self._progress_text(),
|
120
|
+
tooltip=self._progress_tooltip(),
|
121
|
+
css_classes=['progress-label'],
|
122
|
+
interactive=True,
|
123
|
+
)
|
124
|
+
|
125
|
+
def _progress_text(self) -> Union[str, Html]:
|
126
|
+
completed = sum(s.value for s in self.subprogresses)
|
127
|
+
if self.total is None:
|
128
|
+
return 'n/a'
|
129
|
+
complete_rate = completed / self.total
|
130
|
+
return f'{complete_rate: .1%} ({completed}/{self.total})'
|
131
|
+
|
132
|
+
def _progress_tooltip(self) -> str:
|
133
|
+
if self.total is None:
|
134
|
+
return 'Not started'
|
135
|
+
assert self.total > 0
|
136
|
+
return '\n'.join([
|
137
|
+
f'{s.name}: {s.value / self.total:.1%} ({s.value}/{self.total})'
|
138
|
+
for s in self.subprogresses
|
139
|
+
])
|
140
|
+
|
141
|
+
def _to_html(self, **kwargs) -> Html:
|
142
|
+
return Html.element(
|
143
|
+
'div',
|
144
|
+
[
|
145
|
+
Html.element('div', self.subprogresses, css_classes=['shade']),
|
146
|
+
self._progress_label,
|
147
|
+
],
|
148
|
+
css_classes=['progress-bar']
|
149
|
+
).add_style(
|
150
|
+
"""
|
151
|
+
.progress-bar {
|
152
|
+
display: inline-block;
|
153
|
+
}
|
154
|
+
.progress-bar .shade {
|
155
|
+
position: relative;
|
156
|
+
background-color: #EEE;
|
157
|
+
display: inline-flex;
|
158
|
+
margin: 10px;
|
159
|
+
width: 100px;
|
160
|
+
height: 5px;
|
161
|
+
border-radius: 5px;
|
162
|
+
overflow: hidden;
|
163
|
+
}
|
164
|
+
.progress-bar .sub-progress {
|
165
|
+
background-color: dodgerblue;
|
166
|
+
height: 10px;
|
167
|
+
width: 0%;
|
168
|
+
border-radius: 0px;
|
169
|
+
}
|
170
|
+
"""
|
171
|
+
)
|
172
|
+
|
173
|
+
def update(self, total: Optional[int] = None):
|
174
|
+
if total is not None:
|
175
|
+
assert self.total is None
|
176
|
+
self._sync_members(total=total)
|
177
|
+
self._progress_label.update(
|
178
|
+
self._progress_text(), tooltip=self._progress_tooltip()
|
179
|
+
)
|
180
|
+
|
181
|
+
def __getitem__(self, name: str) -> SubProgress:
|
182
|
+
for sub in self.subprogresses:
|
183
|
+
if sub.name == name:
|
184
|
+
return sub
|
185
|
+
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"><a class="label progress-label"'
|
46
|
+
f' id="{bar._progress_label.element_id()}">n/a</a><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,169 @@
|
|
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, Union
|
17
|
+
|
18
|
+
from pyglove.core.symbolic import object as pg_object
|
19
|
+
# pylint: disable=g-importing-member
|
20
|
+
from pyglove.core.views.html.base import Html
|
21
|
+
from pyglove.core.views.html.base import HtmlConvertible
|
22
|
+
from pyglove.core.views.html.controls.base import HtmlControl
|
23
|
+
from pyglove.core.views.html.controls.label import Label
|
24
|
+
# pylint: enable=g-importing-member
|
25
|
+
|
26
|
+
|
27
|
+
class Tab(pg_object.Object):
|
28
|
+
"""A tab."""
|
29
|
+
|
30
|
+
label: Annotated[
|
31
|
+
Label,
|
32
|
+
'The label of the tab.'
|
33
|
+
]
|
34
|
+
|
35
|
+
content: Annotated[
|
36
|
+
Union[Html, HtmlConvertible],
|
37
|
+
'The content of the tab.'
|
38
|
+
]
|
39
|
+
|
40
|
+
|
41
|
+
@pg_object.use_init_args(
|
42
|
+
['tabs', 'selected', 'tab_position', 'id', 'css_classes', 'styles']
|
43
|
+
)
|
44
|
+
class TabControl(HtmlControl):
|
45
|
+
"""A tab control."""
|
46
|
+
|
47
|
+
tabs: Annotated[
|
48
|
+
List[Tab],
|
49
|
+
'The tabs of the tab control.'
|
50
|
+
]
|
51
|
+
|
52
|
+
selected: Annotated[
|
53
|
+
int,
|
54
|
+
'The index of the selected tab.'
|
55
|
+
] = 0
|
56
|
+
|
57
|
+
tab_position: Annotated[
|
58
|
+
Literal['top', 'left'],
|
59
|
+
'The direction of the tab control.'
|
60
|
+
] = 'top'
|
61
|
+
|
62
|
+
interactive = True
|
63
|
+
|
64
|
+
def _to_html(self, **kwargs):
|
65
|
+
return Html.element(
|
66
|
+
'div',
|
67
|
+
[
|
68
|
+
Html.element(
|
69
|
+
'div',
|
70
|
+
[
|
71
|
+
Html.element(
|
72
|
+
'button',
|
73
|
+
[
|
74
|
+
tab.label
|
75
|
+
],
|
76
|
+
css_classes=[
|
77
|
+
'tab-button',
|
78
|
+
'selected' if i == self.selected else None
|
79
|
+
],
|
80
|
+
onclick=(
|
81
|
+
f"openTab(event, '{self.element_id(str(i))}')"
|
82
|
+
)
|
83
|
+
) for i, tab in enumerate(self.tabs)
|
84
|
+
],
|
85
|
+
css_classes=['tab-button-group'],
|
86
|
+
),
|
87
|
+
] + [
|
88
|
+
Html.element(
|
89
|
+
'div',
|
90
|
+
[
|
91
|
+
tab.content
|
92
|
+
],
|
93
|
+
css_classes=['tab-content'],
|
94
|
+
styles=dict(
|
95
|
+
display='block' if i == self.selected else 'none'
|
96
|
+
),
|
97
|
+
id=self.element_id(str(i))
|
98
|
+
) for i, tab in enumerate(self.tabs)
|
99
|
+
],
|
100
|
+
css_classes=['tab-control', self.tab_position] + self.css_classes,
|
101
|
+
id=self.id,
|
102
|
+
styles=self.styles,
|
103
|
+
).add_script(
|
104
|
+
"""
|
105
|
+
function openTab(event, tabId) {
|
106
|
+
const tabButtons = document.getElementsByClassName('tab-button');
|
107
|
+
for (let i = 0; i < tabButtons.length; i++) {
|
108
|
+
tabButtons[i].classList.remove('selected');
|
109
|
+
}
|
110
|
+
const tabContents = document.getElementsByClassName('tab-content');
|
111
|
+
for (let i = 0; i < tabContents.length; i++) {
|
112
|
+
tabContents[i].style.display = 'none';
|
113
|
+
}
|
114
|
+
const tabButton = event.currentTarget;
|
115
|
+
tabButton.classList.add('selected');
|
116
|
+
document.getElementById(tabId).style.display = "block";
|
117
|
+
}
|
118
|
+
"""
|
119
|
+
).add_style(
|
120
|
+
"""
|
121
|
+
.top .tab-button-group {
|
122
|
+
overflow-x: hidden;
|
123
|
+
border-bottom: 1px solid #DDD;
|
124
|
+
}
|
125
|
+
.left .tab-button-group {
|
126
|
+
float: left;
|
127
|
+
top: 0;
|
128
|
+
}
|
129
|
+
.tab-button {
|
130
|
+
background-color: #DDD;
|
131
|
+
border: 1px solid #DDD;
|
132
|
+
outline: none;
|
133
|
+
cursor: pointer;
|
134
|
+
transition: 0.3s;
|
135
|
+
padding: 10px 15px 10px 15px;
|
136
|
+
}
|
137
|
+
.top .tab-button {
|
138
|
+
border-top-width: 2px;
|
139
|
+
}
|
140
|
+
.left .tab-button {
|
141
|
+
display: block;
|
142
|
+
width: 100%;
|
143
|
+
border-left-width: 2px;
|
144
|
+
}
|
145
|
+
.top .tab-button.selected {
|
146
|
+
border-bottom-color: #fff;
|
147
|
+
border-top-color: #B721FF;
|
148
|
+
background: #fff;
|
149
|
+
}
|
150
|
+
.left .tab-button.selected {
|
151
|
+
border-right-color: #fff;
|
152
|
+
border-left-color: #B721FF;
|
153
|
+
background: #fff;
|
154
|
+
}
|
155
|
+
.top .tab-button:hover {
|
156
|
+
border-top-color: orange;
|
157
|
+
}
|
158
|
+
.left .tab-button:hover {
|
159
|
+
border-left-color: orange;
|
160
|
+
}
|
161
|
+
.tab-content {
|
162
|
+
display: none;
|
163
|
+
padding: 10px;
|
164
|
+
}
|
165
|
+
.left .tab-content {
|
166
|
+
float: left;
|
167
|
+
}
|
168
|
+
"""
|
169
|
+
)
|
@@ -0,0 +1,54 @@
|
|
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>')),
|
34
|
+
tab_lib.Tab('bar', base.Html('<h1>bar</h1>')),
|
35
|
+
])
|
36
|
+
self.assert_html_content(
|
37
|
+
tab,
|
38
|
+
(
|
39
|
+
'<div class="tab-control top"><div class="tab-button-group">'
|
40
|
+
'<button class="tab-button selected" '
|
41
|
+
f'''onclick="openTab(event, '{tab.element_id(str(0))}')">'''
|
42
|
+
'<a class="label">foo</a></button><button class="tab-button" '
|
43
|
+
f'''onclick="openTab(event, '{tab.element_id(str(1))}')">'''
|
44
|
+
'<a class="label">bar</a></button></div>'
|
45
|
+
'<div class="tab-content" style="display:block;" '
|
46
|
+
f'id="{tab.element_id(str(0))}"><h1>foo</h1></div>'
|
47
|
+
'<div class="tab-content" style="display:none;" '
|
48
|
+
f'id="{tab.element_id(str(1))}"><h1>bar</h1></div></div>'
|
49
|
+
)
|
50
|
+
)
|
51
|
+
|
52
|
+
|
53
|
+
if __name__ == '__main__':
|
54
|
+
unittest.main()
|
@@ -0,0 +1,99 @@
|
|
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
|
+
"""Html tooltip."""
|
15
|
+
|
16
|
+
from typing import Optional, Union
|
17
|
+
from pyglove.core import typing as pg_typing
|
18
|
+
from pyglove.core.symbolic import object as pg_object
|
19
|
+
# pylint: disable=g-importing-member
|
20
|
+
from pyglove.core.views.html.base import Html
|
21
|
+
from pyglove.core.views.html.controls.base import HtmlControl
|
22
|
+
# pylint: disable=g-importing-member
|
23
|
+
|
24
|
+
|
25
|
+
@pg_object.use_init_args(
|
26
|
+
['content', 'for_element', 'id', 'css_classes', 'styles']
|
27
|
+
)
|
28
|
+
class Tooltip(HtmlControl):
|
29
|
+
"""A tooltip control.
|
30
|
+
|
31
|
+
Attributes:
|
32
|
+
content: The content of the tooltip. It could be a string or a HTML object.
|
33
|
+
id: The id of the tooltip.
|
34
|
+
css_classes: The CSS classes for the tooltip.
|
35
|
+
for_element: The CSS selector for the element to attach the tooltip to.
|
36
|
+
e.g. '.my-element' or '#my-element'.
|
37
|
+
"""
|
38
|
+
|
39
|
+
content: Union[str, Html]
|
40
|
+
for_element: Optional[str] = None
|
41
|
+
|
42
|
+
def _to_html(self, **kwargs):
|
43
|
+
if self.for_element is None:
|
44
|
+
raise ValueError(
|
45
|
+
'CSS selector `for_element` is required for tooltip to display.'
|
46
|
+
)
|
47
|
+
content = self.content
|
48
|
+
if isinstance(self.content, str):
|
49
|
+
content = Html.escape(self.content)
|
50
|
+
return Html.element(
|
51
|
+
'span',
|
52
|
+
[content],
|
53
|
+
id=self.element_id(),
|
54
|
+
css_classes=[
|
55
|
+
'tooltip',
|
56
|
+
'html-content' if isinstance(content, Html) else None,
|
57
|
+
] + self.css_classes,
|
58
|
+
styles=self.styles,
|
59
|
+
).add_style(
|
60
|
+
"""
|
61
|
+
span.tooltip {
|
62
|
+
visibility: hidden;
|
63
|
+
white-space: pre-wrap;
|
64
|
+
font-weight: normal;
|
65
|
+
background-color: #484848;
|
66
|
+
color: #fff;
|
67
|
+
padding: 10px;
|
68
|
+
border-radius: 6px;
|
69
|
+
position: absolute;
|
70
|
+
z-index: 1;
|
71
|
+
}
|
72
|
+
span.tooltip:hover {
|
73
|
+
visibility: visible;
|
74
|
+
}
|
75
|
+
.tooltip.html-content {
|
76
|
+
white-space: inherit;
|
77
|
+
background-color: white;
|
78
|
+
color: inherit;
|
79
|
+
box-shadow: rgba(0, 0, 0, 0.16) 0px 1px 4px;
|
80
|
+
}
|
81
|
+
""",
|
82
|
+
f"""
|
83
|
+
{self.for_element}:hover + .tooltip {{
|
84
|
+
visibility: visible;
|
85
|
+
}}
|
86
|
+
"""
|
87
|
+
)
|
88
|
+
|
89
|
+
def update(self, content: Union[str, Html]) -> None:
|
90
|
+
self._sync_members(content=self._update_content(content))
|
91
|
+
if isinstance(content, Html):
|
92
|
+
self._add_css_class('html-content')
|
93
|
+
else:
|
94
|
+
self._remove_css_class('html-content')
|
95
|
+
|
96
|
+
|
97
|
+
# Register converter for automatic conversion.
|
98
|
+
pg_typing.register_converter(str, Tooltip, Tooltip)
|
99
|
+
pg_typing.register_converter(Html, Tooltip, Tooltip)
|
@@ -0,0 +1,99 @@
|
|
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 tooltip as tooltip_lib
|
20
|
+
|
21
|
+
|
22
|
+
class TooltipTest(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
|
+
tooltip = tooltip_lib.Tooltip('foo')
|
33
|
+
with self.assertRaisesRegex(
|
34
|
+
ValueError, 'CSS selector `for_element` is required'
|
35
|
+
):
|
36
|
+
tooltip.to_html()
|
37
|
+
|
38
|
+
tooltip = tooltip_lib.Tooltip('foo', for_element='.bar')
|
39
|
+
self.assertEqual(tooltip.for_element, '.bar')
|
40
|
+
self.assert_html_content(
|
41
|
+
tooltip,
|
42
|
+
'<span class="tooltip">foo</span>'
|
43
|
+
)
|
44
|
+
self.assertIn(
|
45
|
+
inspect.cleandoc(
|
46
|
+
"""
|
47
|
+
.bar:hover + .tooltip {
|
48
|
+
visibility: visible;
|
49
|
+
}
|
50
|
+
"""
|
51
|
+
),
|
52
|
+
tooltip.to_html().style_section,
|
53
|
+
)
|
54
|
+
|
55
|
+
def test_update(self):
|
56
|
+
tooltip = tooltip_lib.Tooltip('foo', for_element='.bar', interactive=True)
|
57
|
+
self.assertIn('id="control-', tooltip.to_html_str(content_only=True))
|
58
|
+
with tooltip.track_scripts() as scripts:
|
59
|
+
tooltip.update('normal text')
|
60
|
+
self.assertEqual(tooltip.content, 'normal text')
|
61
|
+
self.assertEqual(
|
62
|
+
scripts,
|
63
|
+
[
|
64
|
+
inspect.cleandoc(
|
65
|
+
f"""
|
66
|
+
elem = document.getElementById("{tooltip.element_id()}");
|
67
|
+
elem.textContent = "normal text";
|
68
|
+
"""
|
69
|
+
),
|
70
|
+
inspect.cleandoc(
|
71
|
+
f"""
|
72
|
+
elem = document.getElementById("{tooltip.element_id()}");
|
73
|
+
elem.classList.remove("html-content");
|
74
|
+
"""
|
75
|
+
),
|
76
|
+
]
|
77
|
+
)
|
78
|
+
with tooltip.track_scripts() as scripts:
|
79
|
+
tooltip.update(base.Html('<b>bold text</b>'))
|
80
|
+
self.assertEqual(
|
81
|
+
scripts,
|
82
|
+
[
|
83
|
+
inspect.cleandoc(
|
84
|
+
f"""
|
85
|
+
elem = document.getElementById("{tooltip.element_id()}");
|
86
|
+
elem.innerHTML = "<b>bold text</b>";
|
87
|
+
"""
|
88
|
+
),
|
89
|
+
inspect.cleandoc(
|
90
|
+
f"""
|
91
|
+
elem = document.getElementById("{tooltip.element_id()}");
|
92
|
+
elem.classList.add("html-content");
|
93
|
+
"""
|
94
|
+
),
|
95
|
+
]
|
96
|
+
)
|
97
|
+
|
98
|
+
if __name__ == '__main__':
|
99
|
+
unittest.main()
|