|
33 | 33 |
|
34 | 34 | from __future__ import annotations |
35 | 35 |
|
36 | | -from dash import MATCH, Input, Output, State, callback, dcc, html, no_update |
| 36 | +import logging |
| 37 | + |
| 38 | +from dash import MATCH, Input, Output, State, callback, ctx, dcc, html, no_update |
37 | 39 |
|
38 | 40 | from crystal_toolkit.core.mpcomponent import MPComponent |
39 | 41 |
|
| 42 | +logger = logging.getLogger(__name__) |
| 43 | + |
40 | 44 | # Bulma-inspired color scheme for notification types |
41 | 45 | _TYPE_COLORS = { |
42 | 46 | "info": { |
@@ -294,24 +298,39 @@ def layout(self) -> html.Div: |
294 | 298 | Output(ids.message(MATCH), "children"), |
295 | 299 | Output(ids.wrapper(MATCH), "style", allow_duplicate=True), |
296 | 300 | Output(ids.div(MATCH), "style"), |
297 | | - Output(ids.close_button(MATCH), "n_clicks"), |
298 | 301 | Input(ids.data(MATCH), "data"), |
299 | 302 | Input(ids.close_button(MATCH), "n_clicks"), |
300 | 303 | State(ids.div(MATCH), "style"), |
301 | 304 | prevent_initial_call=True, |
302 | 305 | ) |
303 | 306 | def update_messages(input_data, close_clicks, cur_style): |
304 | | - if close_clicks or not input_data: |
305 | | - return no_update, {"display": "none"}, cur_style, 0 |
| 307 | + if not isinstance(input_data, dict): |
| 308 | + raise ValueError("`input_data` must be a dictionary for MessageAIO") |
| 309 | + |
| 310 | + if ( |
| 311 | + isinstance(ctx.triggered_id, dict) |
| 312 | + and ctx.triggered_id.get("subcomponents") == "close_button" |
| 313 | + ): |
| 314 | + return no_update, {"display": "none"}, cur_style |
| 315 | + |
| 316 | + if not input_data: |
| 317 | + return no_update, {"display": "none"}, cur_style |
306 | 318 |
|
307 | 319 | message = input_data.get("message", None) |
308 | | - msg_type = input_data.get("msg_type", "info") |
| 320 | + msg_type = input_data.get("msg_type", None) |
| 321 | + |
| 322 | + if not message: |
| 323 | + raise ValueError("`message` field is required for MessageAIO") |
| 324 | + |
| 325 | + if not msg_type: |
| 326 | + logger.warning("No `msg_type`. Falling back to 'info'") |
| 327 | + msg_type = "info" |
309 | 328 |
|
310 | 329 | # Resolve type colors |
311 | | - type_style = _TYPE_COLORS.get(msg_type, _TYPE_COLORS["info"]) |
| 330 | + type_style = _TYPE_COLORS.get(msg_type, {}) |
312 | 331 | cur_style.update(type_style) |
313 | 332 |
|
314 | | - return message, {"display": "block"}, cur_style, 1 |
| 333 | + return message, {"display": "block"}, cur_style |
315 | 334 |
|
316 | 335 | """ |
317 | 336 | @callback( |
|
0 commit comments