Skip to content

jupyter

execute_command(command)

Capture the outputs from subprocess and send them to current process' stdout

The output to subprocess' stdout will not be printed on the jupyter notebook. So we need to capture the output and print to current process' stdout.

Parameters:

Name Type Description Default
command str

Bash command to execute

required

Returns:

Type Description
int

Exit code of the bash command

Source code in aicrowd/utils/jupyter.py
def execute_command(command: str) -> int:
    """Capture the outputs from subprocess and send them to current process'
    stdout

    The output to subprocess' stdout will not be printed on the jupyter
    notebook. So we need to capture the output and print to current process'
    stdout.

    Args:
        command: Bash command to execute

    Returns:
        Exit code of the bash command
    """
    process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)

    def print_output(handle):
        if handle is not None:
            output = handle.read()
            if output:
                print(output.decode().strip())

    while True:
        if process.poll() is not None:
            break
        print_output(process.stdout)
        print_output(process.stderr)
    rc = process.poll()
    return rc

is_google_colab_env()

Checks if the CLI is running on Google colab notebook.

Source code in aicrowd/utils/jupyter.py
def is_google_colab_env() -> bool:
    """
    Checks if the CLI is running on Google colab notebook.
    """
    try:
        import google.colab  # pylint: disable=import-error

        return True
    except ImportError:
        return False

is_ipykernel_present()

Check if process is running inside IPython kernel

Source code in aicrowd/utils/jupyter.py
def is_ipykernel_present():
    """
    Check if process is running inside IPython kernel
    """
    try:
        # pylint: disable=import-error,no-name-in-module
        from IPython import get_ipython

        ipy_kernel = get_ipython()
        return ipy_kernel is not None
    except ImportError:
        return False

is_jupyter()

Tries to guess whether it's running inside jupyter notebook or not

Source code in aicrowd/utils/jupyter.py
def is_jupyter():
    """
    Tries to guess whether it's running inside jupyter notebook or not
    """
    # first, check if get_ipython() itself is defined
    try:
        ip = get_ipython_object()
        # now check if it's running as kernel
        return ip.has_trait("kernel")
    except:
        return False

mount_google_drive(mount_path='/content/drive', mount_reason='Your Google Drive will be mounted to access the colab notebook')

Mount user's Google Drive to a path

Parameters:

Name Type Description Default
mount_path str

Path in local FS where the user's GDrive will be mounted

'/content/drive'
mount_reason str

Message displayed on the terminal when attempting to mount

'Your Google Drive will be mounted to access the colab notebook'

Returns:

Type Description
str

Mount path for user's GDrive

Source code in aicrowd/utils/jupyter.py
def mount_google_drive(
    mount_path: str = "/content/drive",
    mount_reason: str = "Your Google Drive will be mounted to access the colab notebook",
) -> str:
    """Mount user's Google Drive to a path

    Args:
        mount_path: Path in local FS where the user's GDrive will be mounted
        mount_reason: Message displayed on the terminal when attempting to mount

    Returns:
        Mount path for user's GDrive
    """
    if os.path.exists(mount_path):
        return mount_path

    from google.colab import drive  # pylint: disable=import-error,no-name-in-module

    console = Console()
    console.print("Mounting Google Drive :floppy_disk:", style="bold blue")
    console.print(mount_reason)
    drive.mount(mount_path)
    return mount_path