-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFE: Rendet Dot results in Jupyter cells #220
Comments
Note that |
(continuing my last comment) For the web, i would try also the all client-JS solution https://visjs.github.io/vis-network/docs/network |
For future reference, this the code for zoomable SVGs in Jupyter: def _repr_html_monkeypatches(dot):
"""
Monkey-patching for ``pydot.Dot._repr_html_()` for rendering in jupyter cells.
:param dot:
a `pydot.Dot()` instance
.. Note::
Had to use ``_repr_html_()`` and not simply ``_repr_svg_()`` because
(due to https://github.com/jupyterlab/jupyterlab/issues/7497)
"""
pan_zoom_json" = "{controlIconsEnabled: true, zoomScaleSensitivity: 0.4, fit: true}",
element_styles = "width: 100%; height: 300px;",
container_styles = ""
svg_txt = dot.create_svg().decode()
html = f"""
<div class="svg_container">
<style>
.svg_container {{
{container_styles}
}}
.svg_container SVG {{
{element_styles}
}}
</style>
<script src="http://ariutta.github.io/svg-pan-zoom/dist/svg-pan-zoom.min.js"></script>
<script type="text/javascript">
var scriptTag = document.scripts[document.scripts.length - 1];
var parentTag = scriptTag.parentNode;
svg_el = parentTag.querySelector(".svg_container svg");
svgPanZoom(svg_el, {pan_zoom_json});
</script>
{svg_txt}
</</>
"""
return html |
Not really, as the encoding is... well,
So if one wanted to be really pedantic, they could use: import re
import pydot
dot = pydot.Dot()
svg = dot.create_svg()
encoding = 'utf-8'
if b'encoding=' in svg:
match = re.search(b'encoding="([^"]*)"', svg)
encoding = match.groups()[0].decode(encoding="ascii")
svg_txt = svg.decode(encoding=encoding) Heh! In fact, interestingly, my point 1 turns out to be more correct. You wouldn't actually want to use that code, as it probably lies: >>> import pydot
>>> dot = pydot.Dot()
>>> dot.create_svg(encoding="iso-8859-15")
b'<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"\n "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n<!-- Generated by graphviz version 8.1.0 (20230707.0739)\n -->\n<!-- Title: G Pages: 1 -->\n<svg width="8pt" height="8pt"\n viewBox="0.00 0.00 8.00 8.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">\n<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 4)">\n<title>G</title>\n<polygon fill="white" stroke="none" points="-4,4 -4,-4 4,-4 4,4 -4,4"/>\n</g>\n</svg>\n' Note the The question of whether an SVG created with |
But to finish my thought, the safest option is probably to always explicitly specify "utf-8" both coming and going: import pydot
def _repr_svg(self):
return self.create_svg(encoding="utf-8").decode(
encoding="utf-8")
pydot.Dot._repr_svg = _repr_svg Ditto for a |
Simply adding this method in
pydot.Dot
class makes it renderable in jupyter notebooks & lab:There is an issue of which encoding to use, and how.
But it would be a great facility to data people who love notebooks.
The text was updated successfully, but these errors were encountered: