-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from vlasenkoalexey/master
added pan and zoom support for jupyter
- Loading branch information
Showing
2 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
# coding: utf-8 | ||
from .graphviz_wrapper import board, add_digraph, add_digraph_node, add_digraph_edge | ||
from .jupyter_helper import jupyter_pan_and_zoom, jupyter_show_as_svg | ||
|
||
|
||
__author__ = "akimach" | ||
__version__ = "0.0.6" | ||
__version__ = "0.0.7" | ||
__license__ = "MIT" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import time | ||
from IPython.display import HTML | ||
|
||
def jupyter_show_as_svg(g): | ||
""" | ||
Shows object as SVG (by default it is rendered as image). | ||
@param g: digraph object | ||
""" | ||
return HTML(g.pipe(format='svg').decode("utf-8")) | ||
|
||
def jupyter_pan_and_zoom( | ||
g, | ||
element_styles="height:auto", | ||
container_styles="overflow:hidden", | ||
pan_zoom_json = "{controlIconsEnabled: true, zoomScaleSensitivity: 0.4, minZoom: 0.2}"): | ||
""" | ||
Embeds SVG object into Jupyter cell with ability to pan and zoom. | ||
@param g: digraph object | ||
@param element_styles: CSS styles for embedded SVG element. | ||
@param container_styles: CSS styles for container div element. | ||
@param pan_zoom_json: pan and zoom settings, see https://github.com/ariutta/svg-pan-zoom | ||
""" | ||
svg_txt = g.pipe(format='svg').decode("utf-8") | ||
html_container_class_name = F"svg_container_{int(time.time())}" | ||
html = F''' | ||
<div class="{html_container_class_name}"> | ||
<style> | ||
.{html_container_class_name} {{ | ||
{container_styles} | ||
}} | ||
.{html_container_class_name} SVG {{ | ||
{element_styles} | ||
}} | ||
</style> | ||
<script src="https://ariutta.github.io/svg-pan-zoom/dist/svg-pan-zoom.min.js"></script> | ||
<script type="text/javascript"> | ||
attempts = 5; | ||
var existCondition = setInterval(function() {{ | ||
console.log(attempts); | ||
svg_el = document.querySelector(".{html_container_class_name} svg"); | ||
if (svg_el != null) {{ | ||
console.log("Exists!"); | ||
clearInterval(existCondition); | ||
svgPanZoom(svg_el, {pan_zoom_json}); | ||
}} | ||
if (--attempts == 0) {{ | ||
console.warn("SVG element not found, zoom wont work"); | ||
clearInterval(existCondition); | ||
}} | ||
}}, 100); // check every 100ms | ||
</script> | ||
{svg_txt} | ||
</div> | ||
''' | ||
return HTML(html) |