|
@@ -4,20 +4,25 @@ import time
|
|
|
from pathlib import Path
|
|
|
from typing import Any, Callable, List, Optional, Union, cast
|
|
|
|
|
|
-from PIL.Image import Image as PIL_Image
|
|
|
-
|
|
|
+from .. import optional_features
|
|
|
from ..events import GenericEventArguments, MouseEventArguments, handle_event
|
|
|
from .image import pil_to_base64
|
|
|
from .mixins.content_element import ContentElement
|
|
|
from .mixins.source_element import SourceElement
|
|
|
|
|
|
+try:
|
|
|
+ from PIL.Image import Image as PIL_Image
|
|
|
+ optional_features.register('pillow')
|
|
|
+except ImportError:
|
|
|
+ pass
|
|
|
+
|
|
|
|
|
|
class InteractiveImage(SourceElement, ContentElement, component='interactive_image.js'):
|
|
|
CONTENT_PROP = 'content'
|
|
|
PIL_CONVERT_FORMAT = 'PNG'
|
|
|
|
|
|
def __init__(self,
|
|
|
- source: Union[str, Path] = '', *,
|
|
|
+ source: Union[str, Path, 'PIL_Image'] = '', *,
|
|
|
content: str = '',
|
|
|
on_mouse: Optional[Callable[..., Any]] = None,
|
|
|
events: List[str] = ['click'],
|
|
@@ -61,8 +66,8 @@ class InteractiveImage(SourceElement, ContentElement, component='interactive_ima
|
|
|
handle_event(on_mouse, arguments)
|
|
|
self.on('mouse', handle_mouse)
|
|
|
|
|
|
- def _set_props(self, source: Union[str, Path]) -> None:
|
|
|
- if isinstance(source, PIL_Image):
|
|
|
+ def _set_props(self, source: Union[str, Path, 'PIL_Image']) -> None:
|
|
|
+ if optional_features.has('pillow') and isinstance(source, PIL_Image):
|
|
|
source = pil_to_base64(source, self.PIL_CONVERT_FORMAT)
|
|
|
super()._set_props(source)
|
|
|
|