Categories

Versions

Python SDK

The Python extension Devkit empowers developers to create cutting-edge AI solutions for the Altair AI ecosystem, using Python. This comprehensive toolkit offers two essential components.

  1. Python Extension Loader (PEL): A Java extension that seamlessly manages the Python extensions and Python environments across various platforms, ensuring consistent execution of the extensions.

  2. Python devkit package: A Python package designed to streamline the creation of Python extensions for the Altair AI tools.

This getting-started document provides a simple tutorial for creating a new Python extension and using it in Altair AI Studio. The guide also helps with concepts and advanced examples.

Table of contents

Pre-requisites

To develop a Python extension, you must have the following:

  • Your favourite IDE (VS Code, PyCharm).
  • Miniforge or mambaforge.
  • conda/mamba environment.
  • Python 3.9 or higher.

And, to test the extension,

  • Python Extension Loader extension.
  • Altair AI Studio 10.3.0 or later

Create a new environment

You can use either conda or mamba while creating the environment and downloading the package. This tutorial uses mamba.

Take the following steps:

  1. Start a terminal, create a new conda/mamba environment, and then activate it.

     mamba create -n sample-environment python=3.12
     mamba activate sample-environment
    
  2. Install the devkit conda package.

     mamba install -c conda-forge/label/altair-aitools-devkit_rc altair-aitools-devkit=1.0b1
    

Start a new project

Take the following steps:

  1. Create a new directory called python-sample-extension.

  2. Open the directory in your favorite IDE. (The tutorial is prepared using VS Code.)

  3. Create a new folder called samples (refered to as module in the extension).

  4. Under the samples folder, create a new Python file hello.py

  5. Add a new hello_world function. See the code below.

import pandas as pd
from typing import Optional


def hello_world(name: Optional[str] = None, times: int = 3) -> pd.DataFrame:
    """Returns a DataFrame with a greeting message.

    Demonstrates basic input/output functionality.

    Args:
        name (Optional[str]): The name to greet.
        times (int): The number of times to repeat the greeting.

    Returns:
        pd.DataFrame: The DataFrame with the greeting message.
    """
    message = "Hello!" if name is None else f"Hello, {name}!"
    return pd.DataFrame({"Greeting": [message for i in range(times)]})

Extension definition

Take the following steps:

  1. Create a new file and save it as extension.toml
  2. Add the following extension properties to extension.toml

     [extension]
     name = "Python Sample Extension"
     namespace = "pysa"
     version = "0.1.0"
     environment = "sample-environment"
     module = "samples"
    
     [operators.hello_world]
     icon = "message.png"
     group_key = "hello"
    

    To let the devkit know which hello_world function you want to use, you need to add the function to the __init__.py file in the samples module.

  3. Create a new file __init__.py and add the following code

     from .hello import hello_world
    

After following the above steps, you should have the following folder structure

python-sample-extension/
├── extension.toml
├── samples
│   ├── __init__.py
│   ├── hello.py

Build the extension

Note: By default, Python extension .zip files are searched for in

  • ~/.Rapidminer/extensions/python-extensions or
  • ~/.AltairRapidMiner/AI Studio/shared/extensions/python-extensions

In your IDE, start a terminal, navigate to the root folder, and enter the following command. Replace the username with your user on the machine.

  • On Windows:

      build-extension -o "%USERPROFILE%\.AltairRapidMiner\AI Studio\shared\extensions\python-extensions" .
    

    Note: While copying the above command, make sure to correct the extra newline between "AI Studio" on your terminal.

  • On MacOS/Linux:

      build-extension -o "~\.AltairRapidMiner\AI Studio\shared\extensions\python-extensions" .
    

    Note: While copying the above command, please check the extra character in the "python-extensions".

Once the extension build is successful, you should see the following message:

Loading D:\ped_demo\python-sample-extension\extension.toml
==================================================
SDK version: 1.0b1
Extension:   Python Sample Extension-
Version:     0.1.0
Namespace:   pysa
Module:      samples
Environment: sample-environment
License:     None
==================================================
Found 0 dependency in configuration (skipping).
Found 1 function definitions in module 'samples'.
Writing extension to: C:\Users\anissh\.AltairRapidMiner\AI Studio\shared\extensions\python-extensions\pysa-0.1.0.zip

and you will find the zip file of the extension in the output directory. The zip file contains the code, extension.json and env.yml file. The next step now is to load the extension in Altair AI Studio.

AI Studio setup

Note: Install the Python Extension Loader extension if not already done. The extension should be present in

  • ~/.AltairRapidMiner/AI Studio/shared/extensions or
  • ~/.Rapidminer/extensions/python-extensions

Take the following steps:

  1. Start Altair AI Studio.
  2. After the launch, AI Studio, will check for the Python extension and will perform certain steps to install the required Python distribution (miniforge), and set up the environment.

    Note: It may take sometime to install the conda/mamba distribution and environment, depending on your internet connection.

  3. Check if the Python extension is loaded correctly by clicking Extensions -> About Installed Python Extensions.

    alt text

If the environment setup was successful, you should see the Hello World operator under the extensions operator in AI Studio.

alt text

Run the operator

Now, let's drag the operator to the process canvas and connect the output port. Add Altair as the value for the parameter name and hit Run.

alt text

With this example, the text "Hello Altair!" appears three times in the output.

alt text