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.
Prerequisites
Section titled “Prerequisites”Before you begin, you must have access to:
-
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:
-
Navigate to the Python 3.12 download page. Scroll down to the Files section. Download the Windows installer (64-bit).
-
Open the installer and follow the prompts to install Python.
-
Open a terminal and verify that Python is installed by running:
Terminal window python --versionThe output should be similar to
Python 3.12.10.
To install Python on macOS, follow these steps:
-
Navigate to the Python 3.12 download page, scroll down to the Files section, and download the macOS 64-bit universal2 installer.
-
Open the installer and follow the prompts to install Python. Use the default installation options. When the installation is complete, double click the Install Certificates command in the Finder window.
-
Open a terminal and verify that Python is installed by running:
Terminal window python3.12 --versionThe output should be similar to
Python 3.12.10.
To install Python on Linux, follow these steps:
-
Use your distribution’s package manager to install Python 3.12. Alternatively, navigate to the Python 3.12 download page. Scroll down to the Files section, download the Gzipped source tarball and compile it yourself. See the Python documentation for details.
-
Open a terminal and verify that Python is installed by running:
Terminal window python3.12 --versionThe output should be similar to
Python 3.12.10.
-
-
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:- Log in to your Snowflake account.
- Run the following SQL command in a worksheet:
SHOW APPLICATIONS;
- 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. -
A Snowflake user with the
RAI_DEVELOPERdatabase role.How do I check my roles?
To check your roles in Snowflake:- Log in to your Snowflake account.
- Run the following SQL command in a worksheet:
SHOW GRANTS TO USER <your_username>;
- Check the output in the
namecolumn for theRAI_DEVELOPERrole.- 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.
Step 1: Install Jupyter
Section titled “Step 1: Install Jupyter”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:
-
Create a new project directory and navigate into it:
Terminal window mkdir <my_project> && cd <my_project> -
Create a virtual environment:
Terminal window python -m venv .venv -
Activate the virtual environment:
Terminal window .venv\Scripts\activate -
Upgrade
pipand install thejupyterPython package:Terminal window python -m pip install --upgrade pippython -m pip install jupyterNote that you can use
pythoninstead ofpython3.XXhere since the virtual environment is activated.
To install Jupyter in your project on a macOS machine, follow the steps below:
-
Create a new project directory and navigate into it:
Terminal window mkdir <my_project> && cd <my_project> -
Create a virtual environment:
Terminal window python3.12 -m venv .venvReplace
python3.12withpython3.11orpython3.10if you’re using a different version. -
Activate the virtual environment:
Terminal window source .venv/bin/activate -
Upgrade
pipand install thejupyterPython package:Terminal window python -m pip install --upgrade pippython -m pip install jupyterNote that you can use
pythoninstead ofpython3.XXhere since the virtual environment is activated.
To install Jupyter in your project on a Linux machine, follow the steps below:
-
Create a new project directory and navigate into it:
Terminal window mkdir <my_project> && cd <my_project> -
Create a virtual environment:
Terminal window python3.12 -m venv .venvReplace
python3.12withpython3.11orpython3.10if you’re using a different version. -
Activate the virtual environment:
Terminal window source .venv/bin/activate -
Upgrade
pipand install thejupyterPython package:Terminal window python -m pip install --upgrade pippython -m pip install jupyterNote that you can use
pythoninstead ofpython3.XXhere since the virtual environment is activated.
Step 2: Install PyRel
Section titled “Step 2: Install PyRel”PyRel is installed via the relationalai Python package.
Run the following command to install PyRel in the same Python environment where Jupyter is installed:
python -m pip install relationalai==1.0.0a1Step 3: Configure PyRel
Section titled “Step 3: Configure PyRel”In order to use PyRel, you need to configure it to connect to your Snowflake account and the RAI Native App.
-
In your terminal, run the
rai initcommand to create araiconfig.yamlfile in your current directory:Terminal window $ rai init -
Fill in the required information in the
raiconfig.ymlto configure access to Snowflake and the RAI Native App.
Step 4: Create Your First Model
Section titled “Step 4: Create Your First Model”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:
-
Start a new Jupyter Notebook:
Terminal window jupyter notebook -
In the Jupyter interface, create a new Python notebook. Make sure to select the same Python environment where you installed the
relationalaipackage as the kernel for the notebook. -
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) -
Run the code cell to execute the model definition and query.
Next Steps
Section titled “Next Steps”To learn more about building and querying semantic models with PyRel, check out the following guides: