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

autolabel flag in diagrams that modifies label #482

Merged
merged 2 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 15 additions & 5 deletions diagrams/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def __init__(
direction: str = "LR",
curvestyle: str = "ortho",
outformat: str = "png",
autolabel: bool = False,
show: bool = True,
graph_attr: dict = {},
node_attr: dict = {},
Expand Down Expand Up @@ -137,6 +138,7 @@ def __init__(
self.dot.edge_attr.update(edge_attr)

self.show = show
self.autolabel = autolabel

def __str__(self) -> str:
return str(self.dot)
Expand Down Expand Up @@ -296,11 +298,23 @@ def __init__(self, label: str = "", **attrs: Dict):
self._id = self._rand_id()
self.label = label

# Node must be belong to a diagrams.
self._diagram = getdiagram()
if self._diagram is None:
raise EnvironmentError("Global diagrams context not set up")

if self._diagram.autolabel:
prefix = self.__class__.__name__
if self.label:
self.label = prefix + "\n" + self.label
else:
self.label = prefix

# fmt: off
# If a node has an icon, increase the height slightly to avoid
# that label being spanned between icon image and white space.
# Increase the height by the number of new lines included in the label.
padding = 0.4 * (label.count('\n'))
padding = 0.4 * (self.label.count('\n'))
self._attrs = {
"shape": "none",
"height": str(self._height + padding),
Expand All @@ -310,10 +324,6 @@ def __init__(self, label: str = "", **attrs: Dict):
# fmt: on
self._attrs.update(attrs)

# Node must be belong to a diagrams.
self._diagram = getdiagram()
if self._diagram is None:
raise EnvironmentError("Global diagrams context not set up")
self._cluster = getcluster()

# If a node is in the cluster context, add it to cluster.
Expand Down
6 changes: 6 additions & 0 deletions tests/test_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ def test_empty_name(self):
with Diagram(show=False):
Node("node1")
self.assertTrue(os.path.exists(f"{self.name}.png"))

def test_autolabel(self):
with Diagram(name=os.path.join(self.name, "nodes_to_node"), show=False):
node1 = Node("node1")
self.assertTrue(node1.label,"Node\nnode1")



class ClusterTest(unittest.TestCase):
Expand Down