188 lines
4.1 KiB
Text
188 lines
4.1 KiB
Text
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"id": "0ad1fbe7-107b-4668-ae4d-8ce4ae9a4400",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"from __future__ import annotations\n",
|
||
|
"\n",
|
||
|
"from dataclasses import dataclass"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"id": "c2d3a9f4-dfdb-4ced-bbcd-3dfd1780af80",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"7.29.0\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"import IPython\n",
|
||
|
"\n",
|
||
|
"print(IPython.__version__)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "21e7a4a1-0cf8-48cc-823c-dca698ae6853",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Supported IPython display formatters:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"id": "053cdbc4-b157-4e3e-9c86-8f374770d006",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"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)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "d79b063d-ce81-497b-a0ea-5b2e2972e845",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Let's write a simple class that will output different mime:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"id": "c847636c-1c45-432e-9d8d-7310dd7f5637",
|
||
|
"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"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"id": "4fa54f22-0c3a-4809-91f7-ea7101ff1907",
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"mime = Mime(\"E = mc^2\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"id": "c419e6a6-240c-4af0-a244-5f1526705c30",
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"\\[E = mc^2\\]"
|
||
|
],
|
||
|
"text/latex": [
|
||
|
"\\[E = mc^2\\]"
|
||
|
],
|
||
|
"text/markdown": [
|
||
|
"$$E = mc^2$$"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"E = mc^2"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 6,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"mime"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"id": "bf140b8e-16ac-4670-9778-f1c1d9486f9d",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Note that #7561 made ipynb reader aware of this, and #7563 made ipynb writer aware of this."
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3 (ipykernel)",
|
||
|
"language": "python",
|
||
|
"name": "python3"
|
||
|
},
|
||
|
"language_info": {
|
||
|
"codemirror_mode": {
|
||
|
"name": "ipython",
|
||
|
"version": 3
|
||
|
},
|
||
|
"file_extension": ".py",
|
||
|
"mimetype": "text/x-python",
|
||
|
"name": "python",
|
||
|
"nbconvert_exporter": "python",
|
||
|
"pygments_lexer": "ipython3",
|
||
|
"version": "3.9.6"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 5
|
||
|
}
|