Skip to main content

supabase-py with async synax

Project description

supabase-py-async

License: MIT CI Python Version

Downloads Open Issues Pull Requests Contributors Code Size

async-part of supabase-py Python client for Supabase

Installation

We recommend activating your virtual environment. For example, we like poetry and conda!

PyPi installation

Install the package (for > Python 3.7):

# with pip
pip install supabase-py-async
# with poetry (recommended)
poetry add supabase-py-async

Local installation

You can also install locally after cloning this repo. Install Development mode with pip install -e, which makes it so when you edit the source code the changes will be reflected in your python module.

Usage

Set your Supabase environment variables in a dotenv file, or using the shell:

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

We can then read the keys in the python source code:

import os
from supabase_py_async import create_client, AsyncClient

url: str = os.environ.get("SUPABASE_URL")
key: str = os.environ.get("SUPABASE_KEY")
supabase: Client = create_client(url, key)

Use the supabase client to interface with your database.

Authenticate

from supabase import create_client, Client

url: str = os.environ.get("SUPABASE_TEST_URL")
key: str = os.environ.get("SUPABASE_TEST_KEY")
supabase: Client = create_client(url, key)
# Create a random user login email and password.
random_email: str = "3hf82fijf92@supamail.com"
random_password: str = "fqj13bnf2hiu23h"
user = supabase.auth.sign_up({ "email": random_email, "password": random_password })

Sign-in

from supabase import create_client, Client

url: str = os.environ.get("SUPABASE_TEST_URL")
key: str = os.environ.get("SUPABASE_TEST_KEY")
supabase: Client = create_client(url, key)
# Sign in using the user email and password.
random_email: str = "3hf82fijf92@supamail.com"
random_password: str = "fqj13bnf2hiu23h"
user = supabase.auth.sign_in_with_password({ "email": random_email, "password": random_password })

Insert Data

from supabase import create_client, Client

url: str = os.environ.get("SUPABASE_TEST_URL")
key: str = os.environ.get("SUPABASE_TEST_KEY")
supabase: Client = create_client(url, key)
data = supabase.table("countries").insert({"name":"Germany"}).execute()
assert len(data.data) > 0

Select Data

from supabase import create_client, Client

url: str = os.environ.get("SUPABASE_TEST_URL")
key: str = os.environ.get("SUPABASE_TEST_KEY")
supabase: Client = create_client(url, key)
data = supabase.table("countries").select("*").eq("country", "IL").execute()
# Assert we pulled real data.
assert len(data.data) > 0

Update Data

from supabase import create_client, Client

url: str = os.environ.get("SUPABASE_TEST_URL")
key: str = os.environ.get("SUPABASE_TEST_KEY")
supabase: Client = create_client(url, key)
data = supabase.table("countries").update({"country": "Indonesia", "capital_city": "Jakarta"}).eq("id", 1).execute()

Update data with duplicate keys

from supabase import create_client, Client

url: str = os.environ.get("SUPABASE_TEST_URL")
key: str = os.environ.get("SUPABASE_TEST_KEY")
supabase: Client = create_client(url, key)

country = {
  "country": "United Kingdom",
  "capital_city": "London" # this was missing when it was added
}

data = supabase.table("countries").upsert(country).execute()
assert len(data.data) > 0

Delete Data

from supabase import create_client, Client

url: str = os.environ.get("SUPABASE_TEST_URL")
key: str = os.environ.get("SUPABASE_TEST_KEY")
supabase: Client = create_client(url, key)
data = supabase.table("countries").delete().eq("id", 1).execute()

Call Edge Functions

from supabase import create_client, Client

url: str = os.environ.get("SUPABASE_TEST_URL")
key: str = os.environ.get("SUPABASE_TEST_KEY")
supabase: Client = create_client(url, key)

def test_func():
  try:
    resp = supabase.functions.invoke("hello-world", invoke_options={'body':{}})
    return resp
  except (FunctionsRelayError, FunctionsHttpError) as exception:
    err = exception.to_dict()
    print(err.get("message"))

Download a file from Storage

from supabase import create_client, Client

url: str = os.environ.get("SUPABASE_TEST_URL")
key: str = os.environ.get("SUPABASE_TEST_KEY")
supabase: Client = create_client(url, key)

bucket_name: str = "photos"

data = supabase.storage.from_(bucket_name).download("photo1.png")

Upload a file

from supabase import create_client, Client

url: str = os.environ.get("SUPABASE_TEST_URL")
key: str = os.environ.get("SUPABASE_TEST_KEY")
supabase: Client = create_client(url, key)

bucket_name: str = "photos"
new_file = getUserFile()

data = supabase.storage.from_(bucket_name).upload("/user1/profile.png", new_file)

Remove a file

from supabase import create_client, Client

url: str = os.environ.get("SUPABASE_TEST_URL")
key: str = os.environ.get("SUPABASE_TEST_KEY")
supabase: Client = create_client(url, key)

bucket_name: str = "photos"

data = supabase.storage.from_(bucket_name).remove(["old_photo.png", "image5.jpg"])

List all files

from supabase import create_client, Client

url: str = os.environ.get("SUPABASE_TEST_URL")
key: str = os.environ.get("SUPABASE_TEST_KEY")
supabase: Client = create_client(url, key)

bucket_name: str = "charts"

data = supabase.storage.from_(bucket_name).list()

Move and rename files

from supabase import create_client, Client

url: str = os.environ.get("SUPABASE_TEST_URL")
key: str = os.environ.get("SUPABASE_TEST_KEY")
supabase: Client = create_client(url, key)

bucket_name: str = "charts"
old_file_path: str = "generic/graph1.png"
new_file_path: str = "important/revenue.png"

data = supabase.storage.from_(bucket_name).move(old_file_path, new_file_path)

Roadmap

  • Wrap Postgrest-py
    • Add remaining filters
    • Add support for EXPLAIN
    • Add proper error handling
  • Wrap Realtime-py
    • Integrate with Supabase-py
    • Support WALRUS
    • Support broadcast (to check if already supported)
  • Wrap auth-py
    • Remove references to GoTrue-js v1 and do a proper release
    • Test and document common flows (e.g. sign in with OAuth, sign in with OTP)
    • Add MFA methods and SSO methods
    • Add Proof Key for Code Exchange (PKCE) methods. Unlike the JS library, we do not currently plan to support Magic Link (PKCE). Please use the token hash in tandem with verifyOTP instead.
  • Wrap storage-py
    • Support resumable uploads
    • Setup testing environment
    • Document how to properly upload different file types (e.g. jpeg/png and download it)
  • Wrap functions-py

Overall Tasks:

  • Add async support across the entire library
  • Add FastAPI helper library (external to supabase-py)
  • Add django-supabase-postgrest (external to supabase-py)

Contributing

Contributing to the Python libraries are a great way to get involved with the Supabase community. Reach out to us on Discord if you want to get involved.

Running Tests

Currently the test suites are in a state of flux. We are expanding our clients tests to ensure things are working, and for now can connect to this test instance, that is populated with the following table:

The above test database is a blank supabase instance that has populated the countries table with the built in countries script that can be found in the supabase UI. You can launch the test scripts and point to the above test database by running

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

supabase_py_async-0.0.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

supabase_py_async-0.0.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file supabase_py_async-0.0.0.tar.gz.

File metadata

  • Download URL: supabase_py_async-0.0.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for supabase_py_async-0.0.0.tar.gz
Algorithm Hash digest
SHA256 a0e7643a4f2e55c087aa7a9c2c8a7e198111f1a3c84260fc4768be7db4afe896
MD5 78606d1f8a4b6809fbb68d99166e23b0
BLAKE2b-256 11532431316d751b5ad42dc7111f9de96464255271d0e98374efcf61b855290f

See more details on using hashes here.

File details

Details for the file supabase_py_async-0.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for supabase_py_async-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2f568d30338ecce498b351729bb7cf762a6d96a76dfb94ac63f458fb8df11df8
MD5 275e47b40f3a1b73106b11c68aab1312
BLAKE2b-256 6a6f0c6b2ac1b4a5aca490c04a8bc973f1d5f45e3f8566d74e6419f8344262c1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page