Skip to content

utils

Attributes

Functions

ensure_kiara_plugins(*plugins, update: bool = False)

Ensure that the specified packages are installed.

Parameters:

Name Type Description Default
package_names

The names of the packages to install.

required
update bool

If True, update the packages if they are already installed

False

Returns:

Type Description

'None' if nothing was done, else a string containing information about what was installed

Source code in kiara_plugin/jupyter/utils/__init__.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
def ensure_kiara_plugins(*plugins, update: bool = False):
    """Ensure that the specified packages are installed.

    Arguments:
      package_names: The names of the packages to install.
      update: If True, update the packages if they are already installed

    Returns:
        'None' if nothing was done, else a string containing information about what was installed
    """

    installed_packages: Dict[str, str] = {}
    all_packages = packages_distributions()

    for name, pkgs in all_packages.items():
        for pkg in pkgs:
            dist = distribution(pkg)
            if (
                pkg in installed_packages.keys()
                and installed_packages[pkg] != dist.version
            ):
                raise Exception(
                    f"Multiple versions of package '{pkg}' available: {installed_packages[pkg]} and {dist.version}."
                )
            installed_packages[pkg] = dist.version

    if not plugins:
        plugins = tuple(OFFICIAL_KIARA_PLUGINS)

    if not update:
        plugin_packages: List[str] = []
        pkgs = [p.replace("_", "-") for p in installed_packages.keys()]
        for package_name in plugins:
            if package_name.startswith("git:"):
                package_name = package_name.replace("git:", "")
                git = True
            else:
                git = False
            package_name = package_name.replace("_", "-")
            if not package_name.startswith("kiara-plugin."):
                package_name = f"kiara-plugin.{package_name}"

            if git or package_name.replace("_", "-") not in pkgs:
                if git:
                    package_name = package_name.replace("-", "_")
                    plugin_packages.append(
                        f"git+https://x:x@github.com/DHARPA-project/{package_name}@develop"
                    )
                else:
                    plugin_packages.append(package_name)
    else:
        plugin_packages = list(plugins)

    in_jupyter = "google.colab" in sys.modules or "jupyter_client" in sys.modules
    if not in_jupyter:
        raise Exception("No juptyer environment detected.")

    if not plugin_packages:
        # nothing to do
        return None

    cmd = ["-q", "--isolated", "install"]
    if update:
        cmd.append("--upgrade")
    cmd.extend(plugin_packages)

    from IPython import get_ipython
    from rich.console import Console

    console = Console()
    with console.status("Installing kiara plugins..."):
        ipython = get_ipython()
        cmd_str = f"sc -l stdout = {sys.executable} -m pip {' '.join(cmd)}"
        ipython.magic(cmd_str)

    importlib.invalidate_caches()

    msg = "Installed packages:\n\n"
    for p in plugin_packages:
        msg += f" - {p}\n"
    msg = f"{msg}\n\nDepending on the state of your current environment, you might see error messages below, in which case you'll have to restart the jupyter kernel manually."
    return Panel(Markdown(msg), title="kiara plugin(s) installed", title_align="left")