Show a notification message. :param str content: Notification content. :param float duration: The duration of the notification display, in seconds. `0` means not to close automatically (at this time, a close button will be displayed next to the message, and the user can close the me
(content: str, duration: float = 2, position: str = 'center', color: str = 'info',
onclick: Callable[[], None] = None)
| 2053 | |
| 2054 | |
| 2055 | def toast(content: str, duration: float = 2, position: str = 'center', color: str = 'info', |
| 2056 | onclick: Callable[[], None] = None): |
| 2057 | """Show a notification message. |
| 2058 | |
| 2059 | :param str content: Notification content. |
| 2060 | :param float duration: The duration of the notification display, in seconds. `0` means not to close automatically |
| 2061 | (at this time, a close button will be displayed next to the message, and the user can close the message manually) |
| 2062 | :param str position: Where to display the notification message. Available values are `'left'`, `'center'` and `'right'`. |
| 2063 | :param str color: Background color of the notification. |
| 2064 | Available values are `'info'`, `'error'`, `'warn'`, `'success'` or hexadecimal color value starting with `'#'` |
| 2065 | :param callable onclick: The callback function when the notification message is clicked. |
| 2066 | The callback function receives no parameters. |
| 2067 | |
| 2068 | Note: When in :ref:`Coroutine-based session <coroutine_based_session>`, the callback can be a coroutine function. |
| 2069 | |
| 2070 | Example: |
| 2071 | |
| 2072 | .. exportable-codeblock:: |
| 2073 | :name: toast |
| 2074 | :summary: `toast()` usage |
| 2075 | |
| 2076 | def show_msg(): |
| 2077 | put_text("You clicked the notification.") |
| 2078 | |
| 2079 | toast('New messages', position='right', color='#2188ff', duration=0, onclick=show_msg) |
| 2080 | |
| 2081 | """ |
| 2082 | |
| 2083 | colors = { |
| 2084 | 'info': '#1565c0', |
| 2085 | 'error': '#e53935', |
| 2086 | 'warn': '#ef6c00', |
| 2087 | 'success': '#2e7d32' |
| 2088 | } |
| 2089 | color = colors.get(color, color) |
| 2090 | callback_id = output_register_callback(lambda _: onclick()) if onclick is not None else None |
| 2091 | |
| 2092 | send_msg(cmd='toast', spec=dict(content=content, duration=int(duration * 1000), position=position, |
| 2093 | color=color, callback_id=callback_id)) |
| 2094 | |
| 2095 | |
| 2096 | clear_scope = clear |
no test coverage detected
searching dependent graphs…