Skip to content

Commit 3dfe1fa

Browse files
committed
fixes #18
1 parent dde4396 commit 3dfe1fa

2 files changed

Lines changed: 37 additions & 14 deletions

File tree

dialoghelper/core.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ def del_msg(
107107

108108
# %% ../nbs/00_core.ipynb
109109
def _msg(
110+
msg_type: str='note', # Message type, can be 'code', 'note', or 'prompt'
111+
output:str='', # For prompts/code, initial output
110112
time_run: str | None = '', # When was message executed
111113
is_exported: int | None = 0, # Export message to a module?
112114
skipped: int | None = 0, # Hide message from prompt?
@@ -122,17 +124,23 @@ def _msg(
122124
@delegates(_msg)
123125
def add_msg(
124126
content:str, # Content of the message (i.e the message prompt, code, or note text)
125-
msg_type: str='note', # Message type, can be 'code', 'note', or 'prompt'
126-
output:str='', # For prompts/code, initial output
127127
placement:str='add_after', # Can be 'add_after', 'add_before', 'update', 'at_start', 'at_end'
128128
sid:str=None, # sid (stable id -- pk) of message that placement is relative to (if None, uses current message)
129129
**kwargs
130130
):
131-
"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)."
132-
if msg_type not in ('note', 'code', 'prompt'): return "msg_type must be 'code', 'note', or 'prompt'."
133-
if msg_type=='note' and output: return "note messages cannot have an output."
131+
"""Add/update a message to the queue to show after code execution completes.
132+
Be sure to pass a `sid` (stable id) not a `mid` (which is used only for sorting, and can change).
133+
Sets msg_type to 'note' by default if not update placement."""
134+
if 'msg_type' not in kwargs and placement!='update': kwargs['msg_type']='note'
135+
mt = kwargs.get('msg_type',None)
136+
ot = kwargs.get('output',None)
137+
if mt and mt not in ('note', 'code', 'prompt'): return "msg_type must be 'code', 'note', or 'prompt'."
138+
if mt=='note' and ot: return "note messages cannot have an output."
139+
if mt=='code':
140+
try: json.loads(ot or '[]')
141+
except: return "Code output must be valid json"
134142
if not sid: sid = find_msg_id()
135-
data = dict(content=content, msg_type=msg_type, output=output, placement=placement, sid=sid, **kwargs)
143+
data = dict(content=content, placement=placement, sid=sid, **kwargs)
136144
return xpost('http://localhost:5001/add_relative_', data=data).text
137145

138146
# %% ../nbs/00_core.ipynb
@@ -174,7 +182,7 @@ def update_msg(
174182
sid = kw.pop('sid', sid)
175183
if not sid: raise TypeError("update_msg needs either a dict message or `sid=...`")
176184
kw.pop('did', None)
177-
add_msg(content, placement='update', sid=sid, **kw)
185+
return add_msg(content, placement='update', sid=sid, **kw)
178186

179187
# %% ../nbs/00_core.ipynb
180188
def load_gist(gist_id:str):

nbs/00_core.ipynb

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@
411411
"source": [
412412
"#| export\n",
413413
"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",
414416
" time_run: str | None = '', # When was message executed\n",
415417
" is_exported: int | None = 0, # Export message to a module?\n",
416418
" skipped: int | None = 0, # Hide message from prompt?\n",
@@ -433,17 +435,23 @@
433435
"@delegates(_msg)\n",
434436
"def add_msg(\n",
435437
" 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",
438438
" placement:str='add_after', # Can be 'add_after', 'add_before', 'update', 'at_start', 'at_end'\n",
439439
" sid:str=None, # sid (stable id -- pk) of message that placement is relative to (if None, uses current message)\n",
440440
" **kwargs\n",
441441
"):\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",
445453
" 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",
447455
" return xpost('http://localhost:5001/add_relative_', data=data).text"
448456
]
449457
},
@@ -506,7 +514,14 @@
506514
" sid = kw.pop('sid', sid)\n",
507515
" if not sid: raise TypeError(\"update_msg needs either a dict message or `sid=...`\")\n",
508516
" 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"
510525
]
511526
},
512527
{

0 commit comments

Comments
 (0)