Skip to content
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

Add "save_to_disk" flag in the Diagram class to control automatic diagram file saving #1017

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions diagrams/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def __init__(
graph_attr: Optional[dict] = None,
node_attr: Optional[dict] = None,
edge_attr: Optional[dict] = None,
save_to_disk: bool = True, # Add flag to control saving behavior
):
"""Diagram represents a global diagrams context.

Expand Down Expand Up @@ -118,6 +119,9 @@ def __init__(
filename = "_".join(self.name.split()).lower()
self.filename = filename
self.dot = Digraph(self.name, filename=self.filename, strict=strict)
if save_to_disk is None:
save_to_disk = True


# Set attributes.
for k, v in self._default_graph_attrs.items():
Expand Down Expand Up @@ -152,6 +156,7 @@ def __init__(

self.show = show
self.autolabel = autolabel
self.save_to_disk = save_to_disk

def __str__(self) -> str:
return str(self.dot)
Expand All @@ -161,11 +166,13 @@ def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
self.render()
# Remove the graphviz file leaving only the image.
os.remove(self.filename)
if self.save_to_disk: # Only render if save_to_disk is True
self.render()
# Remove the graphviz file leaving only the image.
os.remove(self.filename)
setdiagram(None)


def _repr_png_(self):
return self.dot.pipe(format="png")

Expand All @@ -191,11 +198,12 @@ def subgraph(self, dot: Digraph) -> None:
self.dot.subgraph(dot)

def render(self) -> None:
if isinstance(self.outformat, list):
for one_format in self.outformat:
self.dot.render(format=one_format, view=self.show, quiet=True)
else:
self.dot.render(format=self.outformat, view=self.show, quiet=True)
if self.save_to_disk: # Only save if save_to_disk is True

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like for me better to do

    if not self.save_to_disk:
        return

to avoid unnecessary nesting

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hiya

Updated it to remove save_to_disk check in the render function all together. Now only checked in the exit() function before render() is even called. Saves doubling up on flag verification and mixing logic for one flag.

if isinstance(self.outformat, list):
for one_format in self.outformat:
self.dot.render(format=one_format, view=self.show, quiet=True)
else:
self.dot.render(format=self.outformat, view=self.show, quiet=True)


class Cluster:
Expand Down