20eb8ac7fd
Write RawBlock of markdown in code-cell output. #7561 makes the ipynb reader reads code-cell output with mime "text/markdown" to a RawBlock of markdown This commit makes the ipynb writer writes this RawBlock of markdown back inside a code-cell output with the same mime, preserving this information in round-trip Add tests of ipynb reader (#7561) and ipynb writer (#7563)'s ability to handle a "text/markdown" mime type in a code-cell output
169 lines
3.8 KiB
Text
169 lines
3.8 KiB
Text
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from __future__ import annotations\n",
|
|
"\n",
|
|
"from dataclasses import dataclass"
|
|
],
|
|
"id": "0ad1fbe7-107b-4668-ae4d-8ce4ae9a4400"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"output_type": "stream",
|
|
"name": "stdout",
|
|
"text": [
|
|
"7.29.0\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import IPython\n",
|
|
"\n",
|
|
"print(IPython.__version__)"
|
|
],
|
|
"id": "c2d3a9f4-dfdb-4ced-bbcd-3dfd1780af80"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Supported IPython display formatters:"
|
|
],
|
|
"id": "21e7a4a1-0cf8-48cc-823c-dca698ae6853"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"output_type": "stream",
|
|
"name": "stdout",
|
|
"text": [
|
|
"text/plain\n",
|
|
"text/html\n",
|
|
"text/markdown\n",
|
|
"image/svg+xml\n",
|
|
"image/png\n",
|
|
"application/pdf\n",
|
|
"image/jpeg\n",
|
|
"text/latex\n",
|
|
"application/json\n",
|
|
"application/javascript\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"ip = get_ipython()\n",
|
|
"for mime in ip.display_formatter.formatters:\n",
|
|
" print(mime)"
|
|
],
|
|
"id": "053cdbc4-b157-4e3e-9c86-8f374770d006"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Let's write a simple class that will output different mime:"
|
|
],
|
|
"id": "d79b063d-ce81-497b-a0ea-5b2e2972e845"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"@dataclass\n",
|
|
"class Mime:\n",
|
|
" math: str\n",
|
|
"\n",
|
|
" def _repr_mimebundle_(\n",
|
|
" self,\n",
|
|
" include: Container[str] | None = None,\n",
|
|
" exclude: Container[str] | None = None,\n",
|
|
" **kwargs,\n",
|
|
" ) -> dict[str, str]:\n",
|
|
" string = self.math\n",
|
|
" data = {\n",
|
|
" \"text/plain\": string,\n",
|
|
" \"text/html\": (latex := f\"\\\\[{string}\\\\]\"),\n",
|
|
" \"text/markdown\": f\"$${string}$$\",\n",
|
|
" # \"image/svg+xml\":,\n",
|
|
" # \"image/png\":,\n",
|
|
" # \"application/pdf\":,\n",
|
|
" # \"image/jpeg\":,\n",
|
|
" \"text/latex\": latex,\n",
|
|
" # \"application/json\":,\n",
|
|
" # \"application/javascript\":,\n",
|
|
" }\n",
|
|
" if include:\n",
|
|
" data = {k: v for k, v in data.items() if k in include}\n",
|
|
" if exclude:\n",
|
|
" data = {k: v for k, v in data.items() if k not in exclude}\n",
|
|
" return data"
|
|
],
|
|
"id": "c847636c-1c45-432e-9d8d-7310dd7f5637"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"mime = Mime(\"E = mc^2\")"
|
|
],
|
|
"id": "4fa54f22-0c3a-4809-91f7-ea7101ff1907"
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"output_type": "execute_result",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"data": {
|
|
"text/html": [
|
|
"\\[E = mc^2\\]"
|
|
],
|
|
"text/latex": [
|
|
"\\[E = mc^2\\]"
|
|
],
|
|
"text/markdown": [
|
|
"$$E = mc^2$$"
|
|
],
|
|
"text/plain": [
|
|
"E = mc^2"
|
|
]
|
|
}
|
|
}
|
|
],
|
|
"source": [
|
|
"mime"
|
|
],
|
|
"id": "c419e6a6-240c-4af0-a244-5f1526705c30"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Note that #7561 made ipynb reader aware of this, and #7563 made ipynb writer aware of this."
|
|
],
|
|
"id": "bf140b8e-16ac-4670-9778-f1c1d9486f9d"
|
|
}
|
|
],
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5,
|
|
"metadata": {}
|
|
}
|