Skip to content

Commit d0013d6

Browse files
code review
1 parent 4814b50 commit d0013d6

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

nicegui/functions/clipboard.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import io
2-
from PIL import Image
2+
from typing import Union
33

4-
from .. import json
4+
from .. import json, optional_features
55
from ..logging import log
66
from .javascript import run_javascript
77

8+
try:
9+
from PIL import Image as PIL_Image
10+
optional_features.register('pillow')
11+
except ImportError:
12+
pass
13+
814

915
async def read() -> str:
1016
"""Read text from the clipboard.
@@ -41,30 +47,28 @@ def write(text: str) -> None:
4147
''')
4248

4349

44-
async def read_image() -> Image.Image | None:
45-
"""
46-
Read images from the clipboard.
47-
Note: This function only works in secure contexts (HTTPS or localhost).
50+
async def read_image() -> Union['PIL_Image.Image', None]:
51+
"""Read PIL images from the clipboard.
52+
53+
Note: This function only works in secure contexts (HTTPS or localhost) and requires Pillow to be installed.
4854
"""
55+
if not optional_features.has('pillow'):
56+
log.warning('Pillow is not installed, so we cannot read images from the clipboard.')
57+
return None
4958
content = await run_javascript('''
5059
if (navigator.clipboard) {
51-
var items = await navigator.clipboard.read()
52-
var images = []
53-
for(var item of items){
54-
if(item.types.length>0 && /^image/.test(item.types[0])){
55-
var blob = await item.getType(item.types[0])
56-
images.push(blob)
57-
break
60+
const items = await navigator.clipboard.read();
61+
for (const item of items) {
62+
if (item.types.length > 0 && /^image/.test(item.types[0])) {
63+
return await item.getType(item.types[0]);
5864
}
5965
}
60-
//console.log(images)
61-
return images
6266
}
6367
else {
64-
console.error('Clipboard API is only available in secure contexts (HTTPS or localhost).')
65-
return []
68+
console.error('Clipboard API is only available in secure contexts (HTTPS or localhost).');
6669
}
6770
''', timeout=5)
68-
if content:
69-
buffer = io.BytesIO(content[0])
70-
return Image.open(buffer)
71+
if not content:
72+
return None
73+
buffer = io.BytesIO(content)
74+
return PIL_Image.open(buffer)

website/documentation/content/clipboard_documentation.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@ async def read() -> None:
2121
ui.notify(await ui.clipboard.read())
2222
ui.button('Read Text', on_click=read)
2323

24-
image = ui.image()
25-
2624
async def read_image() -> None:
2725
img = await ui.clipboard.read_image()
2826
if not img:
29-
ui.notify("you must copy an image to clipboard first.")
27+
ui.notify('You must copy an image to clipboard first.')
3028
else:
3129
image.set_source(img)
3230
ui.button('Read Image', on_click=read_image)
31+
image = ui.image().classes('w-72')
3332

3433

3534
@doc.demo('Client-side clipboard', '''

0 commit comments

Comments
 (0)