Quellcode durchsuchen

#439 allow copying demo code to clipboard

Falko Schindler vor 2 Jahren
Ursprung
Commit
72c13f6e3e
2 geänderte Dateien mit 11 neuen und 3 gelöschten Zeilen
  1. 5 3
      nicegui/functions/notify.py
  2. 6 0
      website/demo.py

+ 5 - 3
nicegui/functions/notify.py

@@ -1,12 +1,14 @@
 from typing import Any, Optional, Union
 from typing import Any, Optional, Union
 
 
+from typing_extensions import Literal
+
 from .. import globals, outbox
 from .. import globals, outbox
 
 
 
 
 def notify(message: Any, *,
 def notify(message: Any, *,
-           position: str = 'bottom',
+           position: Literal['top-left', 'top-right', 'bottom-left', 'bottom-right', 'top', 'bottom', 'left', 'right', 'center'] = 'bottom',
            closeBtn: Union[bool, str] = False,
            closeBtn: Union[bool, str] = False,
-           type: Optional[str] = None,
+           type: Optional[Literal['positive', 'negative', 'warning', 'info', 'ongoing']] = None,
            color: Optional[str] = None,
            color: Optional[str] = None,
            **kwargs,
            **kwargs,
            ) -> None:
            ) -> None:
@@ -15,7 +17,7 @@ def notify(message: Any, *,
     Displays a notification on the screen.
     Displays a notification on the screen.
 
 
     :param message: content of the notification
     :param message: content of the notification
-    :param position: position on the screen ("top-left", "top-right", "bottom-left", "bottom-right, "top", "bottom", "left", "right" or "center", default: "bottom")
+    :param position: position on the screen ("top-left", "top-right", "bottom-left", "bottom-right", "top", "bottom", "left", "right" or "center", default: "bottom")
     :param closeBtn: optional label of a button to dismiss the notification (default: `False`)
     :param closeBtn: optional label of a button to dismiss the notification (default: `False`)
     :param type: optional type ("positive", "negative", "warning", "info" or "ongoing")
     :param type: optional type ("positive", "negative", "warning", "info" or "ongoing")
     :param color: optional color name
     :param color: optional color name

+ 6 - 0
website/demo.py

@@ -39,7 +39,13 @@ class demo:
                 code.append('ui.run()')
                 code.append('ui.run()')
             code = isort.code('\n'.join(code), no_sections=True, lines_after_imports=1)
             code = isort.code('\n'.join(code), no_sections=True, lines_after_imports=1)
             with python_window(classes='w-full max-w-[44rem]'):
             with python_window(classes='w-full max-w-[44rem]'):
+                async def copy_code():
+                    await ui.run_javascript('navigator.clipboard.writeText(`' + code + '`)', respond=False)
+                    ui.notify('Copied to clipboard', type='positive', color='primary')
                 ui.markdown(f'```python\n{code}\n```')
                 ui.markdown(f'```python\n{code}\n```')
+                ui.icon('content_copy', size='xs') \
+                    .classes('absolute right-2 top-10 opacity-10 hover:opacity-80 cursor-pointer') \
+                    .on('click', copy_code)
             with browser_window(self.browser_title,
             with browser_window(self.browser_title,
                                 classes='w-full max-w-[44rem] min-[1500px]:max-w-[20rem] min-h-[10rem] browser-window'):
                                 classes='w-full max-w-[44rem] min-[1500px]:max-w-[20rem] min-h-[10rem] browser-window'):
                 intersection_observer(on_intersection=f)
                 intersection_observer(on_intersection=f)