From cf625a86b464c0487a13806255e389b00994b463 Mon Sep 17 00:00:00 2001 From: FBumann <117816358+FBumann@users.noreply.github.com> Date: Wed, 17 Sep 2025 09:54:10 +0200 Subject: [PATCH 1/3] Make networkx usage in type hints in brackets --- flixopt/network_app.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/flixopt/network_app.py b/flixopt/network_app.py index 957b9e4dd..49a2b2c04 100644 --- a/flixopt/network_app.py +++ b/flixopt/network_app.py @@ -7,7 +7,7 @@ try: import dash_cytoscape as cyto import dash_daq as daq - import networkx + import networkx as nx from dash import Dash, Input, Output, State, callback_context, dcc, html, no_update from werkzeug.serving import make_server @@ -110,7 +110,7 @@ class VisualizationConfig: ] -def flow_graph(flow_system: FlowSystem) -> networkx.DiGraph: +def flow_graph(flow_system: FlowSystem) -> 'nx.DiGraph': """Convert FlowSystem to NetworkX graph - simplified and more robust""" nodes = list(flow_system.components.values()) + list(flow_system.buses.values()) edges = list(flow_system.flows.values()) @@ -141,7 +141,7 @@ def get_shape(element): else: return 'rectangle' - graph = networkx.DiGraph() + graph = nx.DiGraph() # Add nodes with attributes for node in nodes: @@ -168,7 +168,7 @@ def get_shape(element): return graph -def make_cytoscape_elements(graph: networkx.DiGraph) -> List[Dict[str, Any]]: +def make_cytoscape_elements(graph: 'nx.DiGraph') -> List[Dict[str, Any]]: """Convert NetworkX graph to Cytoscape elements""" elements = [] @@ -378,7 +378,7 @@ def create_sidebar(): ) -def shownetwork(graph: networkx.DiGraph): +def shownetwork(graph: 'nx.DiGraph'): """Main function to create and run the network visualization""" if not DASH_CYTOSCAPE_AVAILABLE: raise ImportError(f'Required packages not available: {VISUALIZATION_ERROR}') From 7edd734871061e9bc13a61f8b4706330f8f393c2 Mon Sep 17 00:00:00 2001 From: FBumann <117816358+FBumann@users.noreply.github.com> Date: Sat, 20 Sep 2025 18:07:16 +0200 Subject: [PATCH 2/3] Add extra Exception --- flixopt/network_app.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/flixopt/network_app.py b/flixopt/network_app.py index 49a2b2c04..1feb86e70 100644 --- a/flixopt/network_app.py +++ b/flixopt/network_app.py @@ -2,7 +2,7 @@ import logging import socket import threading -from typing import Any, Dict, List +from typing import TYPE_CHECKING, Any, Dict, List try: import dash_cytoscape as cyto @@ -17,6 +17,9 @@ DASH_CYTOSCAPE_AVAILABLE = False VISUALIZATION_ERROR = str(e) +if TYPE_CHECKING: + import networkx as nx + from .components import LinearConverter, Sink, Source, SourceAndSink, Storage from .elements import Bus, Component, Flow from .flow_system import FlowSystem @@ -112,6 +115,14 @@ class VisualizationConfig: def flow_graph(flow_system: FlowSystem) -> 'nx.DiGraph': """Convert FlowSystem to NetworkX graph - simplified and more robust""" + if not DASH_CYTOSCAPE_AVAILABLE: + raise ImportError( + 'Network visualization requires optional dependencies. ' + 'Install with: pip install flixopt[viz] or ' + 'pip install dash dash-cytoscape networkx werkzeug. ' + f'Original error: {VISUALIZATION_ERROR}' + ) + nodes = list(flow_system.components.values()) + list(flow_system.buses.values()) edges = list(flow_system.flows.values()) From f65ee319239e74ea82caad18c237579c8297bc18 Mon Sep 17 00:00:00 2001 From: FBumann <117816358+FBumann@users.noreply.github.com> Date: Sun, 21 Sep 2025 20:46:07 +0200 Subject: [PATCH 3/3] Add Changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 151d9758a..e73ce1521 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ Until here --> ### Fixed - Fix color scheme selection in network_app; color pickers now update when a scheme is selected. +- Fix error handling in network visualization if networkx is not installed. ### Known Issues