flixopt 2.1.5__py3-none-any.whl → 2.1.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of flixopt might be problematic. Click here for more details.

flixopt/flow_system.py CHANGED
@@ -61,6 +61,8 @@ class FlowSystem:
61
61
 
62
62
  self._connected = False
63
63
 
64
+ self._network_app = None
65
+
64
66
  @classmethod
65
67
  def from_dataset(cls, ds: xr.Dataset):
66
68
  timesteps_extra = pd.DatetimeIndex(ds.attrs['timesteps_extra'], name='time')
@@ -241,6 +243,58 @@ class FlowSystem:
241
243
  node_infos, edge_infos = self.network_infos()
242
244
  return plotting.plot_network(node_infos, edge_infos, path, controls, show)
243
245
 
246
+ def start_network_app(self):
247
+ """Visualizes the network structure of a FlowSystem using Dash, Cytoscape, and networkx.
248
+ Requires optional dependencies: dash, dash-cytoscape, networkx, werkzeug.
249
+ """
250
+ from .network_app import DASH_CYTOSCAPE_AVAILABLE, VISUALIZATION_ERROR, flow_graph, shownetwork
251
+
252
+ warnings.warn(
253
+ 'The network visualization is still experimental and might change in the future.',
254
+ stacklevel=2,
255
+ category=UserWarning,
256
+ )
257
+
258
+ if not DASH_CYTOSCAPE_AVAILABLE:
259
+ raise ImportError(
260
+ f'Network visualization requires optional dependencies. '
261
+ f'Install with: pip install flixopt[viz], flixopt[full] or pip install dash dash_cytoscape networkx werkzeug. '
262
+ f'Original error: {VISUALIZATION_ERROR}'
263
+ )
264
+
265
+ if not self._connected:
266
+ self._connect_network()
267
+
268
+ if self._network_app is not None:
269
+ logger.warning('The network app is already running. Restarting it.')
270
+ self.stop_network_app()
271
+
272
+ self._network_app = shownetwork(flow_graph(self))
273
+
274
+ def stop_network_app(self):
275
+ """Stop the network visualization server."""
276
+ from .network_app import DASH_CYTOSCAPE_AVAILABLE, VISUALIZATION_ERROR
277
+
278
+ if not DASH_CYTOSCAPE_AVAILABLE:
279
+ raise ImportError(
280
+ f'Network visualization requires optional dependencies. '
281
+ f'Install with: pip install flixopt[viz]. '
282
+ f'Original error: {VISUALIZATION_ERROR}'
283
+ )
284
+
285
+ if self._network_app is None:
286
+ logger.warning('No network app is currently running. Cant stop it')
287
+ return
288
+
289
+ try:
290
+ logger.info('Stopping network visualization server...')
291
+ self._network_app.server_instance.shutdown()
292
+ logger.info('Network visualization stopped.')
293
+ except Exception as e:
294
+ logger.error(f'Failed to stop the network visualization app: {e}')
295
+ finally:
296
+ self._network_app = None
297
+
244
298
  def network_infos(self) -> Tuple[Dict[str, Dict[str, str]], Dict[str, Dict[str, str]]]:
245
299
  if not self._connected:
246
300
  self._connect_network()