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

Allow an output filename to be passed to Diagram explicitly. #28

Merged
merged 1 commit into from
Feb 20, 2020
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
10 changes: 8 additions & 2 deletions diagrams/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class Diagram:
def __init__(
self,
name: str = "",
filename: str = "",
direction: str = "LR",
outformat: str = "png",
show: bool = True,
Expand All @@ -86,7 +87,10 @@ def __init__(
):
"""Diagram represents a global diagrams context.

:param name: Diagram name. It will be used for output filename.
:param name: Diagram name. It will be used for output filename if the
filename isn't given.
:param filename: The output filename, without the extension (.png).
If not given, it will be generated from the name.
:param direction: Data flow direction. Default is 'left to right'.
:param outformat: Output file format. Default is 'png'.
:param show: Open generated image after save if true, just only save otherwise.
Expand All @@ -96,7 +100,9 @@ def __init__(
"""
self.name = name

self.filename = "_".join(self.name.split()).lower()
if not filename:
filename = "_".join(self.name.split()).lower()
self.filename = filename
self.dot = Digraph(self.name, filename=self.filename)

# Set attributes.
Expand Down
10 changes: 10 additions & 0 deletions docs/guides/diagram.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ with Diagram("Simple Diagram", outformat="jpg"):
EC2("web")
```

You can specify the output filename with `filename` parameter. The extension shouldn't be included, it's determined by the `outformat` parameter.

```python
from diagrams import Diagram
from diagrams.aws.compute import EC2

with Diagram("Simple Diagram", filename="my_diagram"):
EC2("web")
```

You can also disable the automatic file opening by setting the `show` parameter as **false**. Default is **true**.

```python
Expand Down
12 changes: 12 additions & 0 deletions tests/test_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ def test_nodes_to_node(self):
self.assertEqual(nodes >> node1, node1)
self.assertEqual(nodes << node1, node1)

def test_default_filename(self):
self.name = "example_1"
with Diagram(name="Example 1", show=False):
Node("node1")
self.assertTrue(os.path.exists(f"{self.name}.png"))

def test_custom_filename(self):
self.name = "my_custom_name"
with Diagram(name="Example 1", filename=self.name, show=False):
Node("node1")
self.assertTrue(os.path.exists(f"{self.name}.png"))


class ClusterTest(unittest.TestCase):
def setUp(self):
Expand Down