Skip to content

Commit e720b60

Browse files
committed
ui.clipboard becomes class
1 parent 64ffe80 commit e720b60

File tree

2 files changed

+58
-54
lines changed

2 files changed

+58
-54
lines changed

nicegui/functions/clipboard.py

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,65 +12,69 @@
1212
pass
1313

1414

15-
async def read() -> Optional[str]:
16-
"""Read text from the clipboard.
15+
class Clipboard:
16+
"""Wrapper for clipboard helper functions."""
1717

18-
Note: This function only works in secure contexts (HTTPS or localhost).
19-
"""
20-
result = await run_javascript('''
21-
if (navigator.clipboard) {
22-
return navigator.clipboard.readText()
23-
}
24-
else {
25-
console.error('Clipboard API is only available in secure contexts (HTTPS or localhost).')
26-
}
27-
''')
28-
if result is None:
29-
log.warning('Clipboard API is only available in secure contexts (HTTPS or localhost).')
30-
return result
18+
async def read(self) -> Optional[str]:
19+
"""Read text from the clipboard.
3120
21+
Note: This function only works in secure contexts (HTTPS or localhost).
22+
"""
23+
result = await run_javascript('''
24+
if (navigator.clipboard) {
25+
return navigator.clipboard.readText()
26+
}
27+
else {
28+
console.error('Clipboard API is only available in secure contexts (HTTPS or localhost).')
29+
}
30+
''')
31+
if result is None:
32+
log.warning('Clipboard API is only available in secure contexts (HTTPS or localhost).')
33+
return result
3234

33-
def write(text: str) -> None:
34-
"""Write text to the clipboard.
35-
36-
Note: This function only works in secure contexts (HTTPS or localhost).
35+
def write(self, text: str) -> None:
36+
"""Write text to the clipboard.
3737
38-
:param text: text to write
39-
"""
40-
run_javascript(f'''
41-
if (navigator.clipboard) {{
42-
navigator.clipboard.writeText({json.dumps(text)})
43-
}}
44-
else {{
45-
console.error('Clipboard API is only available in secure contexts (HTTPS or localhost).')
46-
}}
47-
''')
38+
Note: This function only works in secure contexts (HTTPS or localhost).
4839
40+
:param text: text to write
41+
"""
42+
run_javascript(f'''
43+
if (navigator.clipboard) {{
44+
navigator.clipboard.writeText({json.dumps(text)})
45+
}}
46+
else {{
47+
console.error('Clipboard API is only available in secure contexts (HTTPS or localhost).')
48+
}}
49+
''')
4950

50-
async def read_image() -> Union['PIL_Image.Image', None]:
51-
"""Read PIL images from the clipboard.
51+
async def read_image(self) -> Union['PIL_Image.Image', None]:
52+
"""Read PIL images from the clipboard.
5253
53-
Note: This function only works in secure contexts (HTTPS or localhost) and requires Pillow to be installed.
54+
Note: This function only works in secure contexts (HTTPS or localhost) and requires Pillow to be installed.
5455
55-
*Added in version 2.10.0*
56-
"""
57-
if not optional_features.has('pillow'):
58-
log.warning('Pillow is not installed, so we cannot read images from the clipboard.')
59-
return None
60-
content = await run_javascript('''
61-
if (navigator.clipboard) {
62-
const items = await navigator.clipboard.read();
63-
for (const item of items) {
64-
if (item.types.length > 0 && /^image/.test(item.types[0])) {
65-
return await item.getType(item.types[0]);
56+
*Added in version 2.10.0*
57+
"""
58+
if not optional_features.has('pillow'):
59+
log.warning('Pillow is not installed, so we cannot read images from the clipboard.')
60+
return None
61+
content = await run_javascript('''
62+
if (navigator.clipboard) {
63+
const items = await navigator.clipboard.read();
64+
for (const item of items) {
65+
if (item.types.length > 0 && /^image/.test(item.types[0])) {
66+
return await item.getType(item.types[0]);
67+
}
6668
}
6769
}
68-
}
69-
else {
70-
console.error('Clipboard API is only available in secure contexts (HTTPS or localhost).');
71-
}
72-
''', timeout=5)
73-
if not content:
74-
return None
75-
buffer = io.BytesIO(content)
76-
return PIL_Image.open(buffer)
70+
else {
71+
console.error('Clipboard API is only available in secure contexts (HTTPS or localhost).');
72+
}
73+
''', timeout=5)
74+
if not content:
75+
return None
76+
buffer = io.BytesIO(content)
77+
return PIL_Image.open(buffer)
78+
79+
80+
clipboard = Clipboard()

nicegui/ui.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@
264264
from nicegui.elements.upload import Upload as upload
265265
from nicegui.elements.video import Video as video
266266
from nicegui.elements.xterm import Xterm as xterm
267-
from nicegui.functions import clipboard
267+
from nicegui.functions.clipboard import clipboard
268268
from nicegui.functions.download import download
269269
from nicegui.functions.html import add_body_html, add_head_html
270270
from nicegui.functions.javascript import run_javascript
@@ -402,7 +402,7 @@
402402
from .elements.upload import Upload as upload
403403
from .elements.video import Video as video
404404
from .elements.xterm import Xterm as xterm
405-
from .functions import clipboard
405+
from .functions.clipboard import clipboard
406406
from .functions.download import download
407407
from .functions.html import add_body_html, add_head_html
408408
from .functions.javascript import run_javascript

0 commit comments

Comments
 (0)