Skip to content

Commit

Permalink
Merge pull request #39 from deepset-ai/generalimport
Browse files Browse the repository at this point in the history
Simplify `pygraphviz` optional import
  • Loading branch information
ZanSara committed Jul 4, 2023
2 parents 86771b5 + 03ad062 commit 43b0e0c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 12 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ jobs:
- name: Install Canals (MacOS)
if: matrix.os == 'macos-latest'
run: |
brew install graphviz
pip install .[mermaid,graphviz,dev]
# brew only offers graphviz 8, which seems to be incompatible with pygraphviz :(
# brew install [email protected]
pip install .[mermaid,dev]
- name: Install Canals (Windows)
if: matrix.os == 'windows-latest'
Expand All @@ -141,7 +142,7 @@ jobs:
pip install .[mermaid,dev]
- name: Run
run: hatch run cov
run: pytest --cov-report xml:coverage.xml --cov="canals" test/

- name: Coverage
if: matrix.os == 'ubuntu-latest' && matrix.version == 3.11
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pip install canals

gives you the bare minimum necessary to run Canals.

To be able to draw pipelines, please make sure you have either an internet connection (to reach the Mermaid graph renderer at `https://mermaid.ink`) or [graphviz](https://graphviz.org/download/) installed and then install Canals as:
To be able to draw pipelines, please make sure you have either an internet connection (to reach the Mermaid graph renderer at `https://mermaid.ink`) or [graphviz](https://graphviz.org/download/) (version 2.49.0) installed and then install Canals as:

### Mermaid
```console
Expand Down
4 changes: 0 additions & 4 deletions canals/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# SPDX-FileCopyrightText: 2022-present deepset GmbH <[email protected]>
#
# SPDX-License-Identifier: Apache-2.0
from generalimport import generalimport

from canals.__about__ import __version__

generalimport("pygraphviz")
12 changes: 10 additions & 2 deletions canals/draw/graphviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@
import logging

import networkx

from networkx.drawing.nx_agraph import to_agraph as nx_to_agraph
from pygraphviz import AGraph


logger = logging.getLogger(__name__)


def to_agraph(graph: networkx.MultiDiGraph) -> AGraph:
def to_agraph(graph: networkx.MultiDiGraph):
"""
Renders a pipeline graph using PyGraphViz. You need to install it and all its system dependencies for it to work.
"""
try:
import pygraphviz # pylint: disable=unused-import,import-outside-toplevel
except (ModuleNotFoundError, ImportError) as exc:
raise ImportError(
"Can't use 'pygraphviz' to draw this pipeline: pygraphviz could not be imported. "
"Make sure pygraphviz is installed and all its system dependencies are setup correctly."
) from exc

for inp, outp, key, data in graph.out_edges("input", keys=True, data=True):
data["style"] = "dashed"
graph.add_edge(inp, outp, key=key, **data)
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ classifiers = [
dynamic = ["version"]
dependencies = [
"networkx", # Pipeline graphs
"generalimport", # Optional pygraphviz
]

[project.optional-dependencies]
Expand All @@ -44,6 +43,8 @@ dev = [
"pylint==2.15.10",
"black[jupyter]==22.6.0",
"pytest",
"pytest-cov",
"requests",
"coverage",
]
docs = [
Expand Down
3 changes: 2 additions & 1 deletion test/pipelines/unit/test_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from test.sample_components import Double


@pytest.mark.skipif(sys.platform.startswith("win"), reason="pygraphviz is not really available in Windows")
@pytest.mark.skipif(sys.platform.lower().startswith("darwin"), reason="the available graphviz version is too recent")
@pytest.mark.skipif(sys.platform.lower().startswith("win"), reason="pygraphviz is not really available in Windows")
def test_draw_pygraphviz(tmp_path, test_files):
pipe = Pipeline()
pipe.add_component("comp1", Double())
Expand Down

0 comments on commit 43b0e0c

Please sign in to comment.