kopia lustrzana https://github.com/deathbeds/ipydrawio
134 wiersze
3.5 KiB
Plaintext
134 wiersze
3.5 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Jupyter Rich Display\n",
|
|
"\n",
|
|
"At present, the mimetype (also called a _media type_) claimed by this extension is `application/x-drawio`."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"MIME_TYPE = \"application/x-drawio\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import lxml.etree as ET, pathlib as P, IPython.display as D, ipywidgets as W"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"a_xml = P.Path(\"A.dio\").read_text()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"class RichDiagram:\n",
|
|
" _STRIP_KEYS = [\"host\", \"modified\", \"agent\"]\n",
|
|
" \n",
|
|
" def __init__(self, data: str=a_xml, **metadata):\n",
|
|
" if \"height\" not in metadata:\n",
|
|
" metadata[\"height\"] = \"600px\"\n",
|
|
" self._metadata = metadata\n",
|
|
" self._data = data\n",
|
|
" \n",
|
|
" def __repr__(self):\n",
|
|
" data, metadata = self._clean()\n",
|
|
" return data\n",
|
|
"\n",
|
|
" def _repr_mimebundle_(self, **kwargs):\n",
|
|
" data, metadata = self._clean()\n",
|
|
" return {\n",
|
|
" MIME_TYPE: data,\n",
|
|
" \"text/markdown\": f\"```md\\n{data}\\n```\",\n",
|
|
" \"text/plain\": data\n",
|
|
" }, {MIME_TYPE: metadata}\n",
|
|
" \n",
|
|
" def _clean(self):\n",
|
|
" node = ET.fromstring(self._data)\n",
|
|
" node.attrib #.getchildren()[0]\n",
|
|
" [node.attrib.pop(key, None) for key in self._STRIP_KEYS]\n",
|
|
" data = ET.tostring(node, pretty_print=True).decode(\"utf-8\")\n",
|
|
" return data, self._metadata"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"d = RichDiagram(drawioUrlParams=dict(ui=\"dark\"))\n",
|
|
"did = D.display(d, display_id=True)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"@W.interact\n",
|
|
"def foo(\n",
|
|
" label=\"B\", \n",
|
|
" fontSize=(16, 128),\n",
|
|
" x=(-400, 400),\n",
|
|
" y=(-400, 400),\n",
|
|
" width=(0, 400),\n",
|
|
" height=(0, 400)\n",
|
|
"):\n",
|
|
" new = RichDiagram(\n",
|
|
" str(d)\n",
|
|
" .replace('''value=\"A\"''', f'''value=\"{label}\"''')\n",
|
|
" .replace('''fontSize=48''', f'''fontSize={fontSize}pt''')\n",
|
|
" .replace('''x=\"20\"''', f'''x=\"{x}\"''')\n",
|
|
" .replace('''y=\"20\"''', f'''y=\"{y}\"''')\n",
|
|
" .replace('''width=\"285\"''', f'''width=\"{width}\"''')\n",
|
|
" .replace('''height=\"280\"''', f'''height=\"{height}\"''')\n",
|
|
" )\n",
|
|
" did.update({MIME_TYPE: str(new)}, raw=True)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"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.8.5"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|