Skip to content

Commit

Permalink
Improve plotting capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
pablormier committed May 9, 2024
1 parent 1753a62 commit c51ebc4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
20 changes: 19 additions & 1 deletion corneto/_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,25 @@ def prune(
return self.subgraph(reachable)

def plot(self, **kwargs):
return self.to_graphviz(**kwargs)
Gv = self.to_graphviz(**kwargs)
try:
return Gv
except Exception as e:
from corneto._settings import LOGGER
from corneto._util import supports_html

LOGGER.warning(f"SVG+XML rendering failed: {e}.")
# Detect if HTML support
if supports_html():
from corneto.contrib._util import dot_vizjs_html

class _VizJS:
def _repr_html_(self):
return dot_vizjs_html(Gv)

return _VizJS()
else:
raise e

def to_graphviz(self, **kwargs):
from corneto._plotting import to_graphviz
Expand Down
46 changes: 33 additions & 13 deletions corneto/contrib/_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def dot_vizjs(
def dot_vizjs_html(
dot_input,
container_id=None,
viz_js_url=None,
Expand All @@ -8,15 +8,6 @@ def dot_vizjs(
import base64
import uuid

try:
from IPython.display import HTML, display
except ImportError as e:
raise ImportError(
"IPython is not installed but is required for displaying the output "
"directly in Jupyter notebooks. Please install IPython with "
"`pip install ipython`."
) from e

# Generate a random container ID if none is provided
if container_id is None:
container_id = f"container-{uuid.uuid4()}"
Expand All @@ -40,15 +31,17 @@ def dot_vizjs(

# Base64 encode the DOT content to safely embed it in HTML/JavaScript
dot_string_base64 = base64.b64encode(dot_string.encode()).decode("utf-8")
if vizjs_version:
if vizjs_version is not None:
vizjs_version = f"@{vizjs_version}"
else:
vizjs_version = ""
# Setting default URLs if custom URLs are not provided
if not viz_js_url:
viz_js_url = f"https://unpkg.com/viz.js{vizjs_version}/viz.js"
if not full_render_js_url:
full_render_js_url = f"https://unpkg.com/viz.js{vizjs_version}/full.render.js"

html_code = f"""
return f"""
<div id='{container_id}'></div> <!-- Container to display the graph -->
<script>
function loadScript(url, callback) {{
Expand Down Expand Up @@ -81,4 +74,31 @@ def dot_vizjs(
renderGraph("{dot_string_base64}");
</script>
"""
display(HTML(html_code))


def dot_vizjs(
dot_input,
container_id=None,
viz_js_url=None,
full_render_js_url=None,
vizjs_version=None,
):
html_code = dot_vizjs_html(
dot_input,
container_id=container_id,
viz_js_url=viz_js_url,
full_render_js_url=full_render_js_url,
vizjs_version=vizjs_version,
)

try:
from IPython.display import HTML, display

display(HTML(html_code))

except ImportError as e:
raise ImportError(
"IPython is not installed but is required for displaying the output "
"directly in Jupyter notebooks. Please install IPython with "
"`pip install ipython`."
) from e

0 comments on commit c51ebc4

Please sign in to comment.