|
411 | 411 | "source": [ |
412 | 412 | "#| export\n", |
413 | 413 | "def _msg(\n", |
| 414 | + " msg_type: str='note', # Message type, can be 'code', 'note', or 'prompt'\n", |
| 415 | + " output:str='', # For prompts/code, initial output\n", |
414 | 416 | " time_run: str | None = '', # When was message executed\n", |
415 | 417 | " is_exported: int | None = 0, # Export message to a module?\n", |
416 | 418 | " skipped: int | None = 0, # Hide message from prompt?\n", |
|
433 | 435 | "@delegates(_msg)\n", |
434 | 436 | "def add_msg(\n", |
435 | 437 | " content:str, # Content of the message (i.e the message prompt, code, or note text)\n", |
436 | | - " msg_type: str='note', # Message type, can be 'code', 'note', or 'prompt'\n", |
437 | | - " output:str='', # For prompts/code, initial output\n", |
438 | 438 | " placement:str='add_after', # Can be 'add_after', 'add_before', 'update', 'at_start', 'at_end'\n", |
439 | 439 | " sid:str=None, # sid (stable id -- pk) of message that placement is relative to (if None, uses current message)\n", |
440 | 440 | " **kwargs\n", |
441 | 441 | "):\n", |
442 | | - " \"Add/update a message to the queue to show after code execution completes. Be sure to pass a `sid` (stable id) not a `mid` (which is used only for sorting, and can change).\"\n", |
443 | | - " if msg_type not in ('note', 'code', 'prompt'): return \"msg_type must be 'code', 'note', or 'prompt'.\"\n", |
444 | | - " if msg_type=='note' and output: return \"note messages cannot have an output.\"\n", |
| 442 | + " \"\"\"Add/update a message to the queue to show after code execution completes.\n", |
| 443 | + " Be sure to pass a `sid` (stable id) not a `mid` (which is used only for sorting, and can change).\n", |
| 444 | + " Sets msg_type to 'note' by default if not update placement.\"\"\"\n", |
| 445 | + " if 'msg_type' not in kwargs and placement!='update': kwargs['msg_type']='note'\n", |
| 446 | + " mt = kwargs.get('msg_type',None)\n", |
| 447 | + " ot = kwargs.get('output',None)\n", |
| 448 | + " if mt and mt not in ('note', 'code', 'prompt'): return \"msg_type must be 'code', 'note', or 'prompt'.\"\n", |
| 449 | + " if mt=='note' and ot: return \"note messages cannot have an output.\"\n", |
| 450 | + " if mt=='code':\n", |
| 451 | + " try: json.loads(ot or '[]')\n", |
| 452 | + " except: return \"Code output must be valid json\"\n", |
445 | 453 | " if not sid: sid = find_msg_id()\n", |
446 | | - " data = dict(content=content, msg_type=msg_type, output=output, placement=placement, sid=sid, **kwargs)\n", |
| 454 | + " data = dict(content=content, placement=placement, sid=sid, **kwargs)\n", |
447 | 455 | " return xpost('http://localhost:5001/add_relative_', data=data).text" |
448 | 456 | ] |
449 | 457 | }, |
|
506 | 514 | " sid = kw.pop('sid', sid)\n", |
507 | 515 | " if not sid: raise TypeError(\"update_msg needs either a dict message or `sid=...`\")\n", |
508 | 516 | " kw.pop('did', None)\n", |
509 | | - " add_msg(content, placement='update', sid=sid, **kw)" |
| 517 | + " return add_msg(content, placement='update', sid=sid, **kw)" |
| 518 | + ] |
| 519 | + }, |
| 520 | + { |
| 521 | + "cell_type": "markdown", |
| 522 | + "metadata": {}, |
| 523 | + "source": [ |
| 524 | + "## Gists" |
510 | 525 | ] |
511 | 526 | }, |
512 | 527 | { |
|
0 commit comments