How to Set Up and Use Supabase with Python

Aug 19, 2021 | Data Science

Supabase is a powerful back-end as a service platform that provides a suite of tools for web and mobile applications. In this article, we’ll walk through how to set up the Supabase Python client, enabling you to seamlessly connect and interact with your Supabase database. Let’s dive into the steps you need to take to get started!

Setting Up a Local Development Environment

Step 1: Clone the Repository

First, you need to clone the Supabase Python client repository. Open your terminal and run the following command:

bash
git clone https://github.com/supabase/supabase-py.git
cd supabase-py

Step 2: Create and Activate a Virtual Environment

It’s always a good idea to use a virtual environment to manage your dependencies. You can choose from various tools like venv, conda, or poetry. Here, we’ll demonstrate using both venv and conda:

  • Using venv:
    bash
    python3 -m venv env
    source env/bin/activate  # On Windows, use .env\Scripts\activate
    
  • Using conda:
    bash
    conda create --name supabase-py
    conda activate supabase-py
    

Step 3: Install the Package

You can install the Supabase package using pip or conda. For Python 3.7 and above, use the following commands:

  • With pip:
    bash
    pip install supabase
    
  • With conda:
    bash
    conda install -c conda-forge supabase
    

Using Supabase Client

Step 1: Configure Environment Variables

You need to set your Supabase environment variables. You can either create a dotenv file or export them from the shell:

bash
export SUPABASE_URL=my-url-to-my-awesome-supabase-instance
export SUPABASE_KEY=my-supa-dupa-secret-supabase-api-key

Step 2: Initialize the Client

The next step is to initialize your Supabase client:

python
import os
from supabase import create_client, Client

url: str = os.environ.get(SUPABASE_URL)
key: str = os.environ.get(SUPABASE_KEY)

supabase: Client = create_client(url, key)

Basic Operations with Supabase Client

Now that your client is set up, you can perform operations like user sign-up, inserting, selecting, updating, and deleting data. Think of interacting with your Supabase database like using a library:

  • Sign Up: Adding a new book to your library.
  • python
    user = supabase.auth.sign_up(
        email="users_email",
        password="users_password"
    )
    
  • Sign In: Checking out a book from your library.
  • python
    user = supabase.auth.sign_in_with_password(
        email="users_email",
        password="users_password"
    )
    
  • Insert Data: Adding new books to your library.
  • python
    data = supabase.table("countries").insert({"name": "Germany"}).execute()
    
  • Select Data: Looking for books about a specific country.
  • python
    data = supabase.table("countries").select("*").eq("country", "IL").execute()
    
  • Update Data: Replacing an old version of a book with a new one.
  • python
    data = supabase.table("countries").update({"country": "Indonesia", "capital_city": "Jakarta"}).eq("id", 1).execute()
    
  • Delete Data: Discarding books you no longer need.
  • python
    data = supabase.table("countries").delete().eq("id", 1).execute()
    

Troubleshooting

If you run into issues during setup or usage, here are some ideas to consider:

  • Ensure your environment variables are correctly set.
  • Check for typos in your code or SQL queries.
  • Verify that your Supabase project is correctly set up and accessible.
  • In case of version conflicts with packages, consider creating a new virtual environment.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

Connecting to Supabase using Python is straightforward and allows you to leverage a powerful database management system for your applications. Leveraging the structured operations we discussed can streamline your development process.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox