Source code for protis.utils.jupyter

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Benjamin Vial
# This file is part of protis
# License: GPLv3
# See the documentation at protis.gitlab.io

all = ["local_hardware_info", "VersionTable"]

import importlib.metadata as metadata
import os
import platform
import sys
import time

import nannos
import psutil
from IPython.core.magic import Magics, line_magic, magics_class
from IPython.display import HTML, display

dir_path = os.path.dirname(os.path.realpath(__file__))


def local_hardware_info():
    """Basic hardware information about the local machine.
    Gives actual number of CPU's in the machine, even when hyperthreading is
    turned on. CPU count defaults to 1 when true count can't be determined.
    Returns:
        dict: The hardware information.
    """
    return {
        "python_compiler": platform.python_compiler(),
        "python_build": ", ".join(platform.python_build()),
        "python_version": platform.python_version(),
        "os": platform.system(),
        "memory": psutil.virtual_memory().total / (1024**3),
        "cpus": psutil.cpu_count(logical=False) or 1,
    }


[docs] @magics_class class VersionTable(Magics): """A class of status magic functions."""
[docs] @line_magic def protis_version_table(self, line="", cell=None): """ Print an HTML-formatted table with version numbers for Protis and its dependencies. This should make it possible to reproduce the environment and the calculation later on. """ html = "<h3>Version Information</h3>" + "<table>" html += "<tr><th>Package</th></tr>" packages = [] for pkg in ["protis"] + nannos.available_backends: ver = metadata.version(pkg) packages.append((f"<code>{pkg}</code>", ver)) packages.append((f"<code>{pkg}</code>", ver)) for name, version in packages: html += f"<tr><td>{name}</td><td>{version}</td></tr>" html += "<tr><th>System information</th></tr>" local_hw_info = local_hardware_info() sys_info = [ ("Python version", local_hw_info["python_version"]), ("Python compiler", local_hw_info["python_compiler"]), ("Python build", local_hw_info["python_build"]), ("OS", f'{local_hw_info["os"]}'), ("CPUs", f'{local_hw_info["cpus"]}'), ("Memory (Gb)", f'{local_hw_info["memory"]}'), ] for name, version in sys_info: html += f"<tr><td>{name}</td><td>{version}</td></tr>" html += f"""<tr><td colspan='2'>{time.strftime("%a %b %d %H:%M:%S %Y %Z")}</td></tr>""" html += "</table>" return display(HTML(html))