Skip to content

Commit

Permalink
Fix previous cell text data in vscode
Browse files Browse the repository at this point in the history
  • Loading branch information
xl0 committed Aug 18, 2024
1 parent 6fc5318 commit 8cd5f94
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 40 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
_docs/
_proc/


# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
1 change: 1 addition & 0 deletions friendlly/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
'friendlly.utils.cell_to_blocks': ('utils.html#cell_to_blocks', 'friendlly/utils.py'),
'friendlly.utils.display_data_to_blocks': ('utils.html#display_data_to_blocks', 'friendlly/utils.py'),
'friendlly.utils.make_cell_from_captured': ('utils.html#make_cell_from_captured', 'friendlly/utils.py'),
'friendlly.utils.maybe_array_to_string': ('utils.html#maybe_array_to_string', 'friendlly/utils.py'),
'friendlly.utils.merge_messages': ('utils.html#merge_messages', 'friendlly/utils.py'),
'friendlly.utils.nict': ('utils.html#nict', 'friendlly/utils.py'),
'friendlly.utils.nict.__delattr__': ('utils.html#nict.__delattr__', 'friendlly/utils.py'),
Expand Down
23 changes: 16 additions & 7 deletions friendlly/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/06_utils.ipynb.

# %% auto 0
__all__ = ['nict', 'GenericEncoder', 'safe_int', 'strip_traceback_junk', 'parse_cell_line', 'svg_to_png',
__all__ = ['nict', 'GenericEncoder', 'safe_int', 'strip_traceback_junk', 'parse_cell_line', 'svg_to_png', 'maybe_array_to_string',
'display_data_to_blocks', 'cell_to_blocks', 'make_cell_from_captured', 'merge_messages']

# %% ../nbs/06_utils.ipynb 1
Expand Down Expand Up @@ -129,6 +129,12 @@ def parse_dimension(value):
return png

# %% ../nbs/06_utils.ipynb 24
def maybe_array_to_string(a: str|list[str]):
if isinstance(a, list):
return "\n".join(a)
return a


def display_data_to_blocks(dsp):
"""
Converts a display_data (a dict of mime types to data) to a list of claudette messages (text, image, with xml tags)
Expand All @@ -147,15 +153,18 @@ def display_data_to_blocks(dsp):
# "text/markdown": "## Markdown text",
# }

plain_text = dsp.get("text/plain")
# Note, in the notebook, the text-like elements are saves as an array of strings, not a single string.


plain_text = maybe_array_to_string(dsp.get("text/plain"))

# Map mime types to their xml tags
text_types = list(filter(lambda x:x[1], [
("text-html", dsp.get("text/html")),
("latex", dsp.get("text/latex")),
("markdown", dsp.get("text/markdown")),
("json", dsp.get("application/json")),
("js", dsp.get("application/javascript"))
("text-html", maybe_array_to_string(dsp.get("text/html"))),
("latex", maybe_array_to_string(dsp.get("text/latex"))),
("markdown", maybe_array_to_string(dsp.get("text/markdown"))),
("json", maybe_array_to_string(dsp.get("application/json"))),
("js", maybe_array_to_string(dsp.get("application/javascript")))
]))
messages = [f"<{t[0]}>{t[1]}</{t[0]}>" for t in text_types]

Expand Down
50 changes: 17 additions & 33 deletions nbs/06_utils.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -386,33 +386,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def display_to_messages(dsp):\n",
" plain_text = dsp.get(\"text/plain\")\n",
" text = list(filter(lambda x:x[1], [\n",
" (\"text-html\", dsp.get(\"text/html\")),\n",
" (\"latex\", dsp.get(\"text/latex\")),\n",
" (\"markdown\", dsp.get(\"text/markdown\")),\n",
" (\"json\", dsp.get(\"application/json\")),\n",
" (\"js\", dsp.get(\"application/javascript\"))\n",
" ]))\n",
"\n",
" images = list(filter(lambda x:x, [ dsp.get(\"image/png\"), dsp.get(\"image/jpeg\") ]))\n",
" svg = dsp.get(\"image/svg+xml\")\n",
" if svg: images.append(svg_to_png(svg, 256))\n",
"\n",
" messages = [f\"<{t[0]}>{t[1]}</{t[0]}>\" for t in text]\n",
"\n",
" if not len(messages):\n",
" messages = [plain_text]\n",
"\n",
" claudette_msgs = [text_msg(m) for m in messages]\n",
" if images:\n",
" imagebytes = base64.b64decode(images[0]) if not isinstance(images[0], bytes) else images[0]\n",
" claudette_msgs.append(img_msg(imagebytes)) # Grab only one image\n",
"\n",
" return claudette_msgs"
]
"source": []
},
{
"cell_type": "code",
Expand All @@ -422,6 +396,13 @@
"source": [
"#| export\n",
"\n",
"\n",
"def maybe_array_to_string(a: str|list[str]):\n",
" if isinstance(a, list):\n",
" return \"\\n\".join(a)\n",
" return a\n",
"\n",
"\n",
"def display_data_to_blocks(dsp):\n",
" \"\"\"\n",
" Converts a display_data (a dict of mime types to data) to a list of claudette messages (text, image, with xml tags)\n",
Expand All @@ -440,15 +421,18 @@
" # \"text/markdown\": \"## Markdown text\",\n",
" # }\n",
"\n",
" plain_text = dsp.get(\"text/plain\")\n",
" # Note, in the notebook, the text-like elements are saves as an array of strings, not a single string.\n",
"\n",
"\n",
" plain_text = maybe_array_to_string(dsp.get(\"text/plain\"))\n",
"\n",
" # Map mime types to their xml tags\n",
" text_types = list(filter(lambda x:x[1], [\n",
" (\"text-html\", dsp.get(\"text/html\")),\n",
" (\"latex\", dsp.get(\"text/latex\")),\n",
" (\"markdown\", dsp.get(\"text/markdown\")),\n",
" (\"json\", dsp.get(\"application/json\")),\n",
" (\"js\", dsp.get(\"application/javascript\"))\n",
" (\"text-html\", maybe_array_to_string(dsp.get(\"text/html\"))),\n",
" (\"latex\", maybe_array_to_string(dsp.get(\"text/latex\"))),\n",
" (\"markdown\", maybe_array_to_string(dsp.get(\"text/markdown\"))),\n",
" (\"json\", maybe_array_to_string(dsp.get(\"application/json\"))),\n",
" (\"js\", maybe_array_to_string(dsp.get(\"application/javascript\")))\n",
" ]))\n",
" messages = [f\"<{t[0]}>{t[1]}</{t[0]}>\" for t in text_types]\n",
"\n",
Expand Down

0 comments on commit 8cd5f94

Please sign in to comment.