diff --git a/src/faebryk/exporters/visualize/interactive_graph.py b/src/faebryk/exporters/visualize/interactive_graph.py index 8f75cb07..8d0ebbf9 100644 --- a/src/faebryk/exporters/visualize/interactive_graph.py +++ b/src/faebryk/exporters/visualize/interactive_graph.py @@ -10,14 +10,11 @@ from faebryk.core.node import Node from faebryk.exporters.visualize.util import ( generate_pastel_palette, - offer_missing_install, ) from faebryk.libs.util import FuncSet def interactive_graph(G: Graph): - offer_missing_install("dash_cytoscape") - offer_missing_install("dash") import dash_cytoscape as cyto from dash import Dash, html diff --git a/src/faebryk/exporters/visualize/util.py b/src/faebryk/exporters/visualize/util.py index 69c85054..34a85e94 100644 --- a/src/faebryk/exporters/visualize/util.py +++ b/src/faebryk/exporters/visualize/util.py @@ -1,49 +1,4 @@ import colorsys -import importlib -import subprocess -import sys -from types import ModuleType - -from rich.prompt import Confirm - - -class InstallationError(Exception): - """Raised when there's a problem installing a module.""" - - -def offer_install(module_name, install_name=None, ex=None) -> ModuleType | None: - """ - Offer to install a missing module using pip. - """ - cmd = [sys.executable, "-m", "pip", "install", install_name or module_name] - - print(f"The module '{module_name}' is not installed.") - - if Confirm.ask( - f"Do you want to run the install command [cyan mono]`{' '.join(cmd)}`[/]" - ): - try: - # Attempt to install the module using pip - subprocess.check_call(cmd) - - except subprocess.CalledProcessError: - print(f"Failed to install {module_name}. Please install it manually.") - raise ex or InstallationError(f"Failed to install {module_name}") - - print(f"Successfully installed {module_name}") - return importlib.import_module(module_name) - - -def offer_missing_install( - module_name: str, install_name: str = None -) -> ModuleType | None: - """ - Offer to install a missing module using pip. - """ - try: - return importlib.import_module(module_name) - except ModuleNotFoundError: - return offer_install(module_name, install_name) def generate_pastel_palette(num_colors: int) -> list[str]: diff --git a/src/faebryk/libs/installation.py b/src/faebryk/libs/installation.py new file mode 100644 index 00000000..c3f1207e --- /dev/null +++ b/src/faebryk/libs/installation.py @@ -0,0 +1,46 @@ +import importlib +import subprocess +import sys +from types import ModuleType + +from rich.prompt import Confirm + + +class InstallationError(Exception): + """Raised when there's a problem installing a module.""" + + +def offer_install(module_name, install_name=None, ex=None) -> ModuleType | None: + """ + Offer to install a missing module using pip. + """ + cmd = [sys.executable, "-m", "pip", "install", install_name or module_name] + + print(f"The module '{module_name}' is not installed.") + + if Confirm.ask( + f"Do you want to run the install command [cyan mono]`{' '.join(cmd)}`[/]" + ): + try: + # Attempt to install the module using pip + subprocess.check_call(cmd) + + except subprocess.CalledProcessError: + print(f"Failed to install {module_name}. Please install it manually.") + raise ex or InstallationError(f"Failed to install {module_name}") + + print(f"Successfully installed {module_name}") + return importlib.import_module(module_name) + + +def offer_missing_install( + module_name: str, install_name: str = None +) -> ModuleType | None: + """ + Offer to install a missing module using pip. + """ + try: + return importlib.import_module(module_name) + except ModuleNotFoundError: + return offer_install(module_name, install_name) +