feat: Enhance Pipeline.__repr__() (#6963)

* Enhance Pipeline.draw() to show image directly in Jupyter notebook

* Add util method to check if we're in a Jupyter notebook

* Split Pipeline.draw() in two methods

* Update tests

* Update releasenotes

* Enhance Pipeline.__repr__

* Simplify Pipeline.__repr__

* Update release notes
This commit is contained in:
Silvano Cerza 2024-02-09 14:44:34 +01:00 committed by GitHub
parent a7f36fdd32
commit d2d01f9fe1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 76 additions and 0 deletions

View File

@ -71,6 +71,34 @@ class Pipeline:
return False
return self.to_dict() == other.to_dict()
def __repr__(self) -> str:
"""
Returns a text representation of the Pipeline.
If this runs in a Jupyter notebook, it will instead display the Pipeline image.
"""
if is_in_jupyter():
# If we're in a Jupyter notebook we want to display the image instead of the text repr.
self.show()
return ""
res = f"{object.__repr__(self)}\n"
if self.metadata:
res += "🧱 Metadata\n"
for k, v in self.metadata.items():
res += f" - {k}: {v}\n"
res += "🚅 Components\n"
for name, instance in self.graph.nodes(data="instance"):
res += f" - {name}: {instance.__class__.__name__}\n"
res += "🛤️ Connections\n"
for sender, receiver, edge_data in self.graph.edges(data=True):
sender_socket = edge_data["from_socket"].name
receiver_socket = edge_data["to_socket"].name
res += f" - {sender}.{sender_socket} -> {receiver}.{receiver_socket} ({edge_data['conn_type']})\n"
return res
def to_dict(self) -> Dict[str, Any]:
"""
Returns this Pipeline instance as a dictionary.

View File

@ -0,0 +1,5 @@
---
enhancements:
- |
Customize `Pipeline.__repr__()` to return a nice text representation of it.
If run on a Jupyter notebook it will instead have the same behaviour as `Pipeline.show()`.

View File

@ -79,6 +79,49 @@ def test_get_component_name_not_added_to_pipeline():
assert pipe.get_component_name(some_component) == ""
@patch("haystack.core.pipeline.pipeline.is_in_jupyter")
def test_repr(mock_is_in_jupyter):
pipe = Pipeline(metadata={"test": "test"}, max_loops_allowed=42)
pipe.add_component("add_two", AddFixedValue(add=2))
pipe.add_component("add_default", AddFixedValue())
pipe.add_component("double", Double())
pipe.connect("add_two", "double")
pipe.connect("double", "add_default")
expected_repr = (
f"{object.__repr__(pipe)}\n"
"🧱 Metadata\n"
" - test: test\n"
"🚅 Components\n"
" - add_two: AddFixedValue\n"
" - add_default: AddFixedValue\n"
" - double: Double\n"
"🛤️ Connections\n"
" - add_two.result -> double.value (int)\n"
" - double.value -> add_default.value (int)\n"
)
# Simulate not being in a notebook
mock_is_in_jupyter.return_value = False
assert repr(pipe) == expected_repr
@patch("haystack.core.pipeline.pipeline.is_in_jupyter")
def test_repr_in_notebook(mock_is_in_jupyter):
pipe = Pipeline(metadata={"test": "test"}, max_loops_allowed=42)
pipe.add_component("add_two", AddFixedValue(add=2))
pipe.add_component("add_default", AddFixedValue())
pipe.add_component("double", Double())
pipe.connect("add_two", "double")
pipe.connect("double", "add_default")
# Simulate being in a notebook
mock_is_in_jupyter.return_value = True
with patch.object(Pipeline, "show") as mock_show:
assert repr(pipe) == ""
mock_show.assert_called_once_with()
def test_run_with_component_that_does_not_return_dict():
BrokenComponent = component_class(
"BrokenComponent", input_types={"a": int}, output_types={"b": int}, output=1 # type:ignore