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

Add graph visualization feature #1976

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
46 changes: 46 additions & 0 deletions taipy/common/config/ScenarioConfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import matplotlib.pyplot as plt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be an optional dependency. Please verify it is installed before using it.

import networkx as nx


class ScenarioConfig:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a Scenario Config class that already contains a DAG. It must be re-used.
The purpose is to add a draw method to this class.

def __init__(self):
self.graph = nx.DiGraph() # Create a directed graph

def add_node(self, node):
"""Add a node to the graph."""
self.graph.add_node(node)

def add_edge(self, from_node, to_node):
"""Add an edge between two nodes."""
self.graph.add_edge(from_node, to_node)

def export_graph_as_image(self, file_path):
"""Export the graph as an image."""
plt.figure(figsize=(10, 6))
pos = nx.spring_layout(self.graph) # Positioning the nodes
nx.draw(self.graph, pos, with_labels=True, node_size=3000, node_color='lightblue', font_size=10,
font_weight='bold')

# Save the graph as an image
plt.savefig(file_path)
plt.close() # Close the plot to avoid display
print(f"Graph exported as image: {file_path}")


if __name__ == "__main__":
config = ScenarioConfig()

# Add vertices (nodes)
config.add_node("Node1")
config.add_node("Node2")
config.add_node("Node3")
config.add_node("Node4")

# Add edges (connections)
config.add_edge("Node1", "Node2")
config.add_edge("Node1", "Node3")
config.add_edge("Node2", "Node4")
config.add_edge("Node3", "Node4")

# Export the graph as an image
config.export_graph_as_image("scenario_graph.png")
17 changes: 0 additions & 17 deletions taipy/common/config/graph_visualizer.py

This file was deleted.

Binary file added taipy/common/config/scenario_graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added taipy/common/config/scenario_grph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.