Skip to content

Use PyRel in a Jupyter Notebook

This guide walks you through the steps to set up a local Python development environment for using PyRel in a Jupyter Notebook.

Before you begin, you must have access to:

  1. A compatible version of Python installed on your development machine.

    How do I install Python?

    Click on the tab for your operating system and follow the steps to install Python.

    To install Python on Windows, follow these steps:

    1. Navigate to the Python 3.12 download page. Scroll down to the Files section. Download the Windows installer (64-bit).

    2. Open the installer and follow the prompts to install Python.

    3. Open a terminal and verify that Python is installed by running:

      Terminal window
      python --version

      The output should be similar to Python 3.12.10.

  2. A Snowflake account with the RelationalAI (RAI) Native App installed.

    How do I know if the RAI Native App is installed? To check if the RAI Native App is installed in your Snowflake account:

    1. Log in to your Snowflake account.
    2. Run the following SQL command in a worksheet:
      SHOW APPLICATIONS;
    3. Check the output for an application named RELATIONALAI.
      • If you see it listed, the RAI Native App is installed.
      • If not, you need to install it.

    How do I install the RAI Native App? See Install the RAI Native App for Snowflake for instructions.

  3. A Snowflake user with the RAI_DEVELOPER database role.

    How do I check my roles? To check your roles in Snowflake:

    1. Log in to your Snowflake account.
    2. Run the following SQL command in a worksheet:
      SHOW GRANTS TO USER <your_username>;
    3. Check the output in the name column for the RAI_DEVELOPER role.
      • If you see it listed, you have the required role.
      • If not, contact your Snowflake account administrator to have the role assigned to you. See Set Up User Access for the RAI Native App for more information.

Click on the tab below that corresponds to your operating system to see the installation instructions.

To install Jupyter in your project on a Windows machine, follow the steps below:

  1. Create a new project directory and navigate into it:

    Terminal window
    mkdir <my_project> && cd <my_project>
  2. Create a virtual environment:

    Terminal window
    python -m venv .venv
  3. Activate the virtual environment:

    Terminal window
    .venv\Scripts\activate
  4. Upgrade pip and install the jupyter Python package:

    Terminal window
    python -m pip install --upgrade pip
    python -m pip install jupyter

    Note that you can use python instead of python3.XX here since the virtual environment is activated.

PyRel is installed via the relationalai Python package.

Run the following command to install PyRel in the same Python environment where Jupyter is installed:

Terminal window
python -m pip install relationalai==1.0.0a1

In order to use PyRel, you need to configure it to connect to your Snowflake account and the RAI Native App.

  1. In your terminal, run the rai init command to create a raiconfig.yaml file in your current directory:

    Terminal window
    $ rai init
  2. Fill in the required information in the raiconfig.yml to configure access to Snowflake and the RAI Native App.

PyRel lets you define a semantic model of your business domain using Python syntax. You can then use this model to write and run queries to answer business questions.

To get started, create a new Jupyter Notebook and add a new code cell with the following code to create a simple RAI model:

  1. Start a new Jupyter Notebook:

    Terminal window
    jupyter notebook
  2. In the Jupyter interface, create a new Python notebook. Make sure to select the same Python environment where you installed the relationalai package as the kernel for the notebook.

  3. In the first code cell of the notebook, add the following code to create a simple RAI model:

    my_first_model.py
    from relationalai.semantics import Model
    # === 1. CREATE A SEMANTIC MODEL ===
    # Initialize a new model.
    model = Model("MyFirstModel")
    # Create concepts and relationships.
    Person = model.Concept("Person")
    Person.friend = model.Relationship(f"{Person} is friends with {Person:friend}")
    # Define some sample data.
    model.define(
    alice := Person.new(name="Alice"),
    bob := Person.new(name="Bob"),
    alice.friend(bob) # Alice is friends with Bob
    )
    # === 2. QUERY YOUR SEMANTIC MODEL ===
    df = model.select(Person.name, Person.friend.name).to_df()
    print(df)
  4. Run the code cell to execute the model definition and query.

To learn more about building and querying semantic models with PyRel, check out the following guides: