Skip to content

Commit 3369bcf

Browse files
committed
fixes #19
1 parent bed0958 commit 3369bcf

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

dialoghelper/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
'syms': { 'dialoghelper.core': { 'dialoghelper.core._add_msg_unsafe': ('core.html#_add_msg_unsafe', 'dialoghelper/core.py'),
99
'dialoghelper.core._msg': ('core.html#_msg', 'dialoghelper/core.py'),
1010
'dialoghelper.core._umsg': ('core.html#_umsg', 'dialoghelper/core.py'),
11+
'dialoghelper.core.add_html': ('core.html#add_html', 'dialoghelper/core.py'),
1112
'dialoghelper.core.add_msg': ('core.html#add_msg', 'dialoghelper/core.py'),
1213
'dialoghelper.core.del_msg': ('core.html#del_msg', 'dialoghelper/core.py'),
1314
'dialoghelper.core.export_dialog': ('core.html#export_dialog', 'dialoghelper/core.py'),

dialoghelper/core.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
# %% auto 0
44
__all__ = ['Placements', 'empty', 'get_db', 'find_var', 'find_dialog_id', 'find_msgs', 'find_msg_id', 'read_msg_ids', 'msg_idx',
5-
'read_msg', 'del_msg', 'add_msg', 'update_msg', 'load_gist', 'gist_file', 'import_string', 'is_usable_tool',
6-
'mk_toollist', 'import_gist', 'export_dialog', 'import_dialog', 'tool_info', 'asdict']
5+
'read_msg', 'add_html', 'del_msg', 'add_msg', 'update_msg', 'load_gist', 'gist_file', 'import_string',
6+
'is_usable_tool', 'mk_toollist', 'import_gist', 'export_dialog', 'import_dialog', 'tool_info', 'asdict']
77

88
# %% ../nbs/00_core.ipynb
99
import json, importlib, linecache
1010
from typing import Dict
1111
from tempfile import TemporaryDirectory
1212
from ipykernel_helper import *
1313
from dataclasses import dataclass
14+
from fastcore.xml import to_xml
1415

1516
from fastcore.utils import *
1617
from fastcore.meta import delegates
@@ -98,6 +99,14 @@ def read_msg(n:int=-1, # Message index (if relative, +ve is downwards)
9899
db = get_db()
99100
return db.t.message.selectone('sid=?', [ids[idx]])
100101

102+
# %% ../nbs/00_core.ipynb
103+
def add_html(
104+
content:str, # The HTML to send to the client (generally should include hx-swap-oob)
105+
):
106+
"Send HTML to the browser to be swapped into the DOM"
107+
res = to_xml(content)
108+
xpost('http://localhost:5001/add_html_', data=dict(content=res))
109+
101110
# %% ../nbs/00_core.ipynb
102111
def del_msg(
103112
sid:str=None, # sid (stable id -- pk) of message that placement is relative to (if None, uses current message)
@@ -280,6 +289,7 @@ def import_dialog(fname, add_header=True):
280289
def tool_info():
281290
cts='''Tools available from `dialoghelper`:
282291
292+
- &`add_html`: Send HTML to the browser to be swapped into the DOM using hx-swap-oob.
283293
- &`find_dialog_id`: Get the current dialog id.
284294
- &`find_msg_id`: Get the current message id.
285295
- &`find_msgs`: Find messages in current specific dialog that contain the given information.

nbs/00_core.ipynb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"from tempfile import TemporaryDirectory\n",
2929
"from ipykernel_helper import *\n",
3030
"from dataclasses import dataclass\n",
31+
"from fastcore.xml import to_xml\n",
3132
"\n",
3233
"from fastcore.utils import *\n",
3334
"from fastcore.meta import delegates\n",
@@ -389,6 +390,21 @@
389390
"read_msg(-1, relative=False)"
390391
]
391392
},
393+
{
394+
"cell_type": "code",
395+
"execution_count": null,
396+
"metadata": {},
397+
"outputs": [],
398+
"source": [
399+
"#| export\n",
400+
"def add_html(\n",
401+
" content:str, # The HTML to send to the client (generally should include hx-swap-oob)\n",
402+
"):\n",
403+
" \"Send HTML to the browser to be swapped into the DOM\"\n",
404+
" res = to_xml(content)\n",
405+
" xpost('http://localhost:5001/add_html_', data=dict(content=res))"
406+
]
407+
},
392408
{
393409
"cell_type": "code",
394410
"execution_count": null,
@@ -1070,6 +1086,7 @@
10701086
"def tool_info():\n",
10711087
" cts='''Tools available from `dialoghelper`:\n",
10721088
"\n",
1089+
"- &`add_html`: Send HTML to the browser to be swapped into the DOM using hx-swap-oob.\n",
10731090
"- &`find_dialog_id`: Get the current dialog id.\n",
10741091
"- &`find_msg_id`: Get the current message id.\n",
10751092
"- &`find_msgs`: Find messages in current specific dialog that contain the given information.\n",

0 commit comments

Comments
 (0)