Repository: deepset-ai/haystack

Issue:
Consider adding `show` and/or `draw` method to SuperComponent to easily plot the underlying pipeline
It was brought up [here](https://github.com/deepset-ai/haystack-experimental/discussions/189#discussioncomment-12935617) that it isn't immediately intuitive to figure out how to display the mermaid graph of the underlying pipeline in a SuperComponent.

It could be nice to add `show` and `draw` methods to the BaseSuperComponent class to better expose these options. 

In the meantime users should be able to do `MySuperComponent().pipeline.show()` to visualize the pipeline graph. 


Discussion:

Patch:
diff --git a/haystack/core/super_component/super_component.py b/haystack/core/super_component/super_component.py
index fd4027eec5..b0c401bc5e 100644
--- a/haystack/core/super_component/super_component.py
+++ b/haystack/core/super_component/super_component.py
@@ -3,6 +3,7 @@
 # SPDX-License-Identifier: Apache-2.0
 
 import functools
+from pathlib import Path
 from types import new_class
 from typing import Any, Dict, List, Optional, Tuple, Union
 
@@ -459,6 +460,76 @@ def from_dict(cls, data: Dict[str, Any]) -> "SuperComponent":
         data["init_parameters"]["pipeline"] = pipeline
         return default_from_dict(cls, data)
 
+    def show(self, server_url: str = "https://mermaid.ink", params: Optional[dict] = None, timeout: int = 30) -> None:
+        """
+        Display an image representing this SuperComponent's underlying pipeline in a Jupyter notebook.
+
+        This function generates a diagram of the Pipeline using a Mermaid server and displays it directly in
+        the notebook.
+
+        :param server_url:
+            The base URL of the Mermaid server used for rendering (default: 'https://mermaid.ink').
+            See https://github.com/jihchi/mermaid.ink and https://github.com/mermaid-js/mermaid-live-editor for more
+            info on how to set up your own Mermaid server.
+
+        :param params:
+            Dictionary of customization parameters to modify the output. Refer to Mermaid documentation for more details
+            Supported keys:
+                - format: Output format ('img', 'svg', or 'pdf'). Default: 'img'.
+                - type: Image type for /img endpoint ('jpeg', 'png', 'webp'). Default: 'png'.
+                - theme: Mermaid theme ('default', 'neutral', 'dark', 'forest'). Default: 'neutral'.
+                - bgColor: Background color in hexadecimal (e.g., 'FFFFFF') or named format (e.g., '!white').
+                - width: Width of the output image (integer).
+                - height: Height of the output image (integer).
+                - scale: Scaling factor (1–3). Only applicable if 'width' or 'height' is specified.
+                - fit: Whether to fit the diagram size to the page (PDF only, boolean).
+                - paper: Paper size for PDFs (e.g., 'a4', 'a3'). Ignored if 'fit' is true.
+                - landscape: Landscape orientation for PDFs (boolean). Ignored if 'fit' is true.
+
+        :param timeout:
+            Timeout in seconds for the request to the Mermaid server.
+
+        :raises PipelineDrawingError:
+            If the function is called outside of a Jupyter notebook or if there is an issue with rendering.
+        """
+        self.pipeline.show(server_url=server_url, params=params, timeout=timeout)
+
+    def draw(
+        self, path: Path, server_url: str = "https://mermaid.ink", params: Optional[dict] = None, timeout: int = 30
+    ) -> None:
+        """
+        Save an image representing this SuperComponent's underlying pipeline to the specified file path.
+
+        This function generates a diagram of the Pipeline using the Mermaid server and saves it to the provided path.
+
+        :param path:
+            The file path where the generated image will be saved.
+        :param server_url:
+            The base URL of the Mermaid server used for rendering (default: 'https://mermaid.ink').
+            See https://github.com/jihchi/mermaid.ink and https://github.com/mermaid-js/mermaid-live-editor for more
+            info on how to set up your own Mermaid server.
+        :param params:
+            Dictionary of customization parameters to modify the output. Refer to Mermaid documentation for more details
+            Supported keys:
+                - format: Output format ('img', 'svg', or 'pdf'). Default: 'img'.
+                - type: Image type for /img endpoint ('jpeg', 'png', 'webp'). Default: 'png'.
+                - theme: Mermaid theme ('default', 'neutral', 'dark', 'forest'). Default: 'neutral'.
+                - bgColor: Background color in hexadecimal (e.g., 'FFFFFF') or named format (e.g., '!white').
+                - width: Width of the output image (integer).
+                - height: Height of the output image (integer).
+                - scale: Scaling factor (1–3). Only applicable if 'width' or 'height' is specified.
+                - fit: Whether to fit the diagram size to the page (PDF only, boolean).
+                - paper: Paper size for PDFs (e.g., 'a4', 'a3'). Ignored if 'fit' is true.
+                - landscape: Landscape orientation for PDFs (boolean). Ignored if 'fit' is true.
+
+        :param timeout:
+            Timeout in seconds for the request to the Mermaid server.
+
+        :raises PipelineDrawingError:
+            If there is an issue with rendering or saving the image.
+        """
+        self.pipeline.draw(path=path, server_url=server_url, params=params, timeout=timeout)
+
 
 def super_component(cls: Any):
     """
diff --git a/releasenotes/notes/add-pipeline-viz-to-supercomponent-80165756cc777056.yaml b/releasenotes/notes/add-pipeline-viz-to-supercomponent-80165756cc777056.yaml
new file mode 100644
index 0000000000..0bee17218a
--- /dev/null
+++ b/releasenotes/notes/add-pipeline-viz-to-supercomponent-80165756cc777056.yaml
@@ -0,0 +1,5 @@
+---
+highlights: >
+  Adding visualization capabilities to the `SuperComponent` via the following methods:
+  - `show()` displays pipeline diagrams in Jupyter notebooks.
+  - `draw()` saves pipeline diagrams as images to specified file paths.


Test patch:
diff --git a/test/core/super_component/test_super_component.py b/test/core/super_component/test_super_component.py
index 59ecd55695..fda7e9359d 100644
--- a/test/core/super_component/test_super_component.py
+++ b/test/core/super_component/test_super_component.py
@@ -16,6 +16,8 @@
 from haystack.utils.auth import Secret
 from haystack.core.serialization import default_from_dict, default_to_dict
 from haystack.core.super_component.super_component import InvalidMappingTypeError, InvalidMappingValueError
+from haystack.testing.sample_components import AddFixedValue, Double
+from unittest.mock import patch
 
 
 @pytest.fixture
@@ -109,6 +111,17 @@ def run(self, prompt: str, **kwargs):
     return pipeline
 
 
+@pytest.fixture
+def sample_super_component():
+    """Creates a sample SuperComponent for testing visualization methods"""
+    pipe = Pipeline()
+    pipe.add_component("comp1", AddFixedValue(add=3))
+    pipe.add_component("comp2", Double())
+    pipe.connect("comp1.result", "comp2.value")
+
+    return SuperComponent(pipeline=pipe)
+
+
 @super_component
 class CustomSuperComponent:
     def __init__(self, var1: int, var2: str = "test"):
@@ -314,3 +327,42 @@ def test_custom_super_component_from_dict(self):
         assert isinstance(custom_super_component, CustomSuperComponent)
         assert custom_super_component.var1 == 1
         assert custom_super_component.var2 == "test"
+
+    @patch("haystack.core.pipeline.Pipeline.show")
+    def test_show_delegates_to_pipeline(self, mock_show, sample_super_component):
+        """Test that SuperComponent.show() correctly delegates to Pipeline.show() with all parameters"""
+
+        server_url = "https://custom.mermaid.server"
+        params = {"theme": "dark", "format": "svg"}
+        timeout = 60
+
+        sample_super_component.show(server_url=server_url, params=params, timeout=timeout)
+        mock_show.assert_called_once_with(server_url=server_url, params=params, timeout=timeout)
+
+    @patch("haystack.core.pipeline.Pipeline.draw")
+    def test_draw_delegates_to_pipeline(self, mock_draw, sample_super_component, tmp_path):
+        """Test that SuperComponent.draw() correctly delegates to Pipeline.draw() with all parameters"""
+
+        path = tmp_path / "test_pipeline.png"
+        server_url = "https://custom.mermaid.server"
+        params = {"theme": "dark", "format": "png"}
+        timeout = 60
+
+        sample_super_component.draw(path=path, server_url=server_url, params=params, timeout=timeout)
+        mock_draw.assert_called_once_with(path=path, server_url=server_url, params=params, timeout=timeout)
+
+    @patch("haystack.core.pipeline.Pipeline.show")
+    def test_show_with_default_parameters(self, mock_show, sample_super_component):
+        """Test that SuperComponent.show() works with default parameters"""
+
+        sample_super_component.show()
+        mock_show.assert_called_once_with(server_url="https://mermaid.ink", params=None, timeout=30)
+
+    @patch("haystack.core.pipeline.Pipeline.draw")
+    def test_draw_with_default_parameters(self, mock_draw, sample_super_component, tmp_path):
+        """Test that SuperComponent.draw() works with default parameters except path"""
+
+        path = tmp_path / "test_pipeline.png"
+
+        sample_super_component.draw(path=path)
+        mock_draw.assert_called_once_with(path=path, server_url="https://mermaid.ink", params=None, timeout=30)