supabase-py-async


Namesupabase-py-async JSON
Version 2.5.6 PyPI version JSON
download
home_pagehttps://github.com/Atticuszz/supabase-py_async
Summarysupabase-py with async synax
upload_time2024-03-22 00:39:52
maintainerNone
docs_urlNone
authorAtticuszz
requires_python<4.0,>=3.9
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            # supabase-py-async

[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg?label=license)](https://opensource.org/licenses/MIT)
[![CI](https://github.com/Atticuszz/supabase-py-async/actions/workflows/ci.yml/badge.svg)](https://github.com/Atticuszz/supabase-py-async/actions/workflows/ci.yml)
[![Python](https://img.shields.io/pypi/pyversions/supabase-py-async)](https://pypi.org/project/supabase-py-async)
[![Version](https://img.shields.io/pypi/v/supabase-py-async?color=%2334D058)](https://pypi.org/project/supabase-py-async)

<!-- Add more badges as needed -->
[![Downloads](https://pepy.tech/badge/supabase-py-async)](https://pepy.tech/project/supabase-py-async)
[![Open Issues](https://img.shields.io/github/issues/Atticuszz/supabase-py-async)](https://github.com/Atticuszz/supabase-py-async/issues)
[![Pull Requests](https://img.shields.io/github/issues-pr/Atticuszz/supabase-py-async)](https://github.com/Atticuszz/supabase-py-async/pulls)
[![Contributors](https://img.shields.io/github/contributors/Atticuszz/supabase-py-async)](https://github.com/Atticuszz/supabase-py-async/graphs/contributors)
[![Code Size](https://img.shields.io/github/languages/code-size/Atticuszz/supabase-py-async)](https://github.com/Atticuszz/supabase-py-async)

async-part of supabase-py Python client for [Supabase-py](https://github.com/supabase-community/supabase-py)

- Documentation: [supabase.com/docs](https://supabase.com/docs/reference/python/introduction)
- Usage:
  - [Supabase with Python ⚡fastapi⚡ app](https://github.com/Atticuszz/fastapi_supabase_template)
  - [GitHub OAuth in your Python Flask app](https://supabase.com/blog/oauth2-login-python-flask-apps)
  - [Python data loading with Supabase](https://supabase.com/blog/loading-data-supabase-python)


## Installation

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

### PyPi installation

Install the package (for > Python 3.9):

```bash
# 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.

## differences
what's different from [supabase-py](https://github.com/supabase-community/supabase-py)?
1. a optional of `access_token: str | None = None,` key word argument in `create_client` function, which is used to set the `Authorization` header in the request.
2. more [tests](./tests) on crud operations and auth operations.

more tutorials in
- [Supabase with Python ⚡fastapi⚡ app](https://github.com/Atticuszz/fastapi_supabase_template)

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

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

init the client in fastapi lifespan event

```python
import os
from contextlib import asynccontextmanager
from dotenv import load_dotenv
from fastapi import FastAPI
from supabase_py_async import create_client
from supabase_py_async.lib.client_options import ClientOptions
client = None
@asynccontextmanager
async def lifespan(app: FastAPI):
    """ life span events"""
    identify_worker = None
    try:
        # start client
        load_dotenv()
        url: str = os.getenv("SUPABASE_URL")
        key: str = os.getenv("SUPABASE_KEY")
        client = await create_client(url, key, options=ClientOptions(
            postgrest_client_timeout=10, storage_client_timeout=10))
        yield
    finally:
        pass
```

Use the supabase client to interface with your database.

#### Authenticate

```python
async def authenticate(email: str, password: str):
    """ authenticate user """
     # Create a random user login email and password.
    random_email: str = "3hf82fijf92@supamail.com"
    random_password: str = "fqj13bnf2hiu23h"
    user = await client.auth.sign_in(email=email, password=password)
```

#### Sign-in

```python
async def sign_in(email: str, password: str):
    """ sign in user """
    # Sign in using the user email and password.
    random_email: str = "3hf82fijf92@supamail.com"
    random_password: str = "fqj13bnf2hiu23h"
    user =  await client.auth.sign_in_with_password({ "email": random_email, "password": random_password })
```

#### Insert Data

```python
async def insert_data():
    """ insert data """
    # Insert a new country into the 'countries' table.
    data = await client.table("countries").insert({"name":"Germany"}).execute()
    assert len(data.data) > 0
```

#### Select Data

```python
async def select_data():
    """ select data """
    # Select all countries from the 'countries' table.
    data = await client.table("countries").select("*").execute()
    assert len(data.data) > 0
```

#### Update Data

```python
async def update_data():
    """ update data """
    # Update the country with id of 1.
    data = await client.table("countries").update({"country": "Indonesia", "capital_city": "Jakarta"}).eq("id", 1).execute()
    assert len(data.data) > 0
```


#### Update data with duplicate keys

```python
async def update_data_with_duplicate_keys():
    """ update data with duplicate keys """
    # Update the country with id of 1.
    data = await client.table("countries").update({"country": "Indonesia", "capital_city": "Jakarta"}).eq("id", 1).execute()
```

#### Delete Data

```python
async def delete_data():
    """ delete data """
    # Delete the country with id of 1.
    data = await client.table("countries").delete().eq("id", 1).execute()
```


#### Call Edge Functions

```python


async def test_func():
  try:
    resp = await client.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

```python

async def download_file():
    """ download file """
    # Download a file from Storage.
    bucket_name: str = "photos"
    data = await client.storage.from_(bucket_name).download("photo1.png")
```

#### Upload a file

```python
async def upload_file():
    """ upload file """
    # Upload a file to Storage.
    bucket_name: str = "photos"
    new_file = getUserFile()
    data = await client.storage.from_(bucket_name).upload("/user1/profile.png", new_file)
```


#### Remove a file

```python
async def remove_file():
    """ remove file """
    # Remove a file from Storage.
    bucket_name: str = "photos"
    data = await client.storage.from_(bucket_name).remove(["old_photo.png", "image5.jpg"])
```


#### List all files

```python
async def list_files():
    """ list files """
    # List all files in Storage.
    bucket_name: str = "photos"
    data = await client.storage.from_(bucket_name).list()
```


#### Move and rename files

```python
async def move_files():
    """ move files """
    # Move and rename files in Storage.
    
    bucket_name: str = "charts"
    old_file_path: str = "generic/graph1.png"
    new_file_path: str = "important/revenue.png"
    
    data = await client.storage.from_(bucket_name).move(old_file_path, new_file_path)
```

## Roadmap

- [x] Wrap [Postgrest-py](https://github.com/supabase-community/postgrest-py/)
  - [x] Add remaining filters
  - [ ] Add support for EXPLAIN
  - [ ] Add proper error handling
- [ ] Wrap [Realtime-py](https://github.com/supabase-community/realtime-py)
    - [ ]  Integrate with Supabase-py
    - [ ]  Support WALRUS
    - [ ]  Support broadcast (to check if already supported)
- [x] Wrap [auth-py](https://github.com/supabase-community/auth-py)
    - [x] 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
    - [x] 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](https://supabase.com/docs/guides/auth/server-side/email-based-auth-with-pkce-flow-for-ssr#create-api-endpoint-for-handling-tokenhash) in tandem with `verifyOTP` instead.
- [x] Wrap [storage-py](https://github.com/supabase-community/storage-py)
    - [ ]  Support resumable uploads
    - [x]  Setup testing environment
    - [x]  Document how to properly upload different file types (e.g. jpeg/png and download it)
- [x] Wrap [functions-py](https://github.com/supabase-community/functions-py)

Overall Tasks:
- [x] 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.







            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Atticuszz/supabase-py_async",
    "name": "supabase-py-async",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": null,
    "keywords": null,
    "author": "Atticuszz",
    "author_email": "1831768457@qq.com",
    "download_url": "https://files.pythonhosted.org/packages/5c/80/ef5bb1458c07c7508c6ab1de19f932d980d65a42db023ad9a65a94ad984e/supabase_py_async-2.5.6.tar.gz",
    "platform": null,
    "description": "# supabase-py-async\n\n[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg?label=license)](https://opensource.org/licenses/MIT)\n[![CI](https://github.com/Atticuszz/supabase-py-async/actions/workflows/ci.yml/badge.svg)](https://github.com/Atticuszz/supabase-py-async/actions/workflows/ci.yml)\n[![Python](https://img.shields.io/pypi/pyversions/supabase-py-async)](https://pypi.org/project/supabase-py-async)\n[![Version](https://img.shields.io/pypi/v/supabase-py-async?color=%2334D058)](https://pypi.org/project/supabase-py-async)\n\n<!-- Add more badges as needed -->\n[![Downloads](https://pepy.tech/badge/supabase-py-async)](https://pepy.tech/project/supabase-py-async)\n[![Open Issues](https://img.shields.io/github/issues/Atticuszz/supabase-py-async)](https://github.com/Atticuszz/supabase-py-async/issues)\n[![Pull Requests](https://img.shields.io/github/issues-pr/Atticuszz/supabase-py-async)](https://github.com/Atticuszz/supabase-py-async/pulls)\n[![Contributors](https://img.shields.io/github/contributors/Atticuszz/supabase-py-async)](https://github.com/Atticuszz/supabase-py-async/graphs/contributors)\n[![Code Size](https://img.shields.io/github/languages/code-size/Atticuszz/supabase-py-async)](https://github.com/Atticuszz/supabase-py-async)\n\nasync-part of supabase-py Python client for [Supabase-py](https://github.com/supabase-community/supabase-py)\n\n- Documentation: [supabase.com/docs](https://supabase.com/docs/reference/python/introduction)\n- Usage:\n  - [Supabase with Python \u26a1fastapi\u26a1 app](https://github.com/Atticuszz/fastapi_supabase_template)\n  - [GitHub OAuth in your Python Flask app](https://supabase.com/blog/oauth2-login-python-flask-apps)\n  - [Python data loading with Supabase](https://supabase.com/blog/loading-data-supabase-python)\n\n\n## Installation\n\nWe recommend activating your virtual environment. For example, we like `poetry` and `conda`!\n\n### PyPi installation\n\nInstall the package (for > Python 3.9):\n\n```bash\n# with pip\npip install supabase-py-async\n# with poetry (recommended)\npoetry add supabase-py-async\n```\n\n### Local installation\n\nYou 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.\n\n## differences\nwhat's different from [supabase-py](https://github.com/supabase-community/supabase-py)?\n1. a optional of `access_token: str | None = None,` key word argument in `create_client` function, which is used to set the `Authorization` header in the request.\n2. more [tests](./tests) on crud operations and auth operations.\n\nmore tutorials in\n- [Supabase with Python \u26a1fastapi\u26a1 app](https://github.com/Atticuszz/fastapi_supabase_template)\n\n## Usage\nSet your Supabase environment variables in a dotenv file, or using the shell:\n\n```bash\nexport SUPABASE_URL=\"my-url-to-my-awesome-supabase-instance\"\nexport SUPABASE_KEY=\"my-supa-dupa-secret-supabase-api-key\"\n```\n\ninit the client in fastapi lifespan event\n\n```python\nimport os\nfrom contextlib import asynccontextmanager\nfrom dotenv import load_dotenv\nfrom fastapi import FastAPI\nfrom supabase_py_async import create_client\nfrom supabase_py_async.lib.client_options import ClientOptions\nclient = None\n@asynccontextmanager\nasync def lifespan(app: FastAPI):\n    \"\"\" life span events\"\"\"\n    identify_worker = None\n    try:\n        # start client\n        load_dotenv()\n        url: str = os.getenv(\"SUPABASE_URL\")\n        key: str = os.getenv(\"SUPABASE_KEY\")\n        client = await create_client(url, key, options=ClientOptions(\n            postgrest_client_timeout=10, storage_client_timeout=10))\n        yield\n    finally:\n        pass\n```\n\nUse the supabase client to interface with your database.\n\n#### Authenticate\n\n```python\nasync def authenticate(email: str, password: str):\n    \"\"\" authenticate user \"\"\"\n     # Create a random user login email and password.\n    random_email: str = \"3hf82fijf92@supamail.com\"\n    random_password: str = \"fqj13bnf2hiu23h\"\n    user = await client.auth.sign_in(email=email, password=password)\n```\n\n#### Sign-in\n\n```python\nasync def sign_in(email: str, password: str):\n    \"\"\" sign in user \"\"\"\n    # Sign in using the user email and password.\n    random_email: str = \"3hf82fijf92@supamail.com\"\n    random_password: str = \"fqj13bnf2hiu23h\"\n    user =  await client.auth.sign_in_with_password({ \"email\": random_email, \"password\": random_password })\n```\n\n#### Insert Data\n\n```python\nasync def insert_data():\n    \"\"\" insert data \"\"\"\n    # Insert a new country into the 'countries' table.\n    data = await client.table(\"countries\").insert({\"name\":\"Germany\"}).execute()\n    assert len(data.data) > 0\n```\n\n#### Select Data\n\n```python\nasync def select_data():\n    \"\"\" select data \"\"\"\n    # Select all countries from the 'countries' table.\n    data = await client.table(\"countries\").select(\"*\").execute()\n    assert len(data.data) > 0\n```\n\n#### Update Data\n\n```python\nasync def update_data():\n    \"\"\" update data \"\"\"\n    # Update the country with id of 1.\n    data = await client.table(\"countries\").update({\"country\": \"Indonesia\", \"capital_city\": \"Jakarta\"}).eq(\"id\", 1).execute()\n    assert len(data.data) > 0\n```\n\n\n#### Update data with duplicate keys\n\n```python\nasync def update_data_with_duplicate_keys():\n    \"\"\" update data with duplicate keys \"\"\"\n    # Update the country with id of 1.\n    data = await client.table(\"countries\").update({\"country\": \"Indonesia\", \"capital_city\": \"Jakarta\"}).eq(\"id\", 1).execute()\n```\n\n#### Delete Data\n\n```python\nasync def delete_data():\n    \"\"\" delete data \"\"\"\n    # Delete the country with id of 1.\n    data = await client.table(\"countries\").delete().eq(\"id\", 1).execute()\n```\n\n\n#### Call Edge Functions\n\n```python\n\n\nasync def test_func():\n  try:\n    resp = await client.functions.invoke(\"hello-world\", invoke_options={'body':{}})\n    return resp\n  except (FunctionsRelayError, FunctionsHttpError) as exception:\n    err = exception.to_dict()\n    print(err.get(\"message\"))\n```\n\n#### Download a file from Storage\n\n```python\n\nasync def download_file():\n    \"\"\" download file \"\"\"\n    # Download a file from Storage.\n    bucket_name: str = \"photos\"\n    data = await client.storage.from_(bucket_name).download(\"photo1.png\")\n```\n\n#### Upload a file\n\n```python\nasync def upload_file():\n    \"\"\" upload file \"\"\"\n    # Upload a file to Storage.\n    bucket_name: str = \"photos\"\n    new_file = getUserFile()\n    data = await client.storage.from_(bucket_name).upload(\"/user1/profile.png\", new_file)\n```\n\n\n#### Remove a file\n\n```python\nasync def remove_file():\n    \"\"\" remove file \"\"\"\n    # Remove a file from Storage.\n    bucket_name: str = \"photos\"\n    data = await client.storage.from_(bucket_name).remove([\"old_photo.png\", \"image5.jpg\"])\n```\n\n\n#### List all files\n\n```python\nasync def list_files():\n    \"\"\" list files \"\"\"\n    # List all files in Storage.\n    bucket_name: str = \"photos\"\n    data = await client.storage.from_(bucket_name).list()\n```\n\n\n#### Move and rename files\n\n```python\nasync def move_files():\n    \"\"\" move files \"\"\"\n    # Move and rename files in Storage.\n    \n    bucket_name: str = \"charts\"\n    old_file_path: str = \"generic/graph1.png\"\n    new_file_path: str = \"important/revenue.png\"\n    \n    data = await client.storage.from_(bucket_name).move(old_file_path, new_file_path)\n```\n\n## Roadmap\n\n- [x] Wrap [Postgrest-py](https://github.com/supabase-community/postgrest-py/)\n  - [x] Add remaining filters\n  - [ ] Add support for EXPLAIN\n  - [ ] Add proper error handling\n- [ ] Wrap [Realtime-py](https://github.com/supabase-community/realtime-py)\n    - [ ]  Integrate with Supabase-py\n    - [ ]  Support WALRUS\n    - [ ]  Support broadcast (to check if already supported)\n- [x] Wrap [auth-py](https://github.com/supabase-community/auth-py)\n    - [x] Remove references to GoTrue-js v1 and do a proper release\n    - [ ] Test and document common flows (e.g. sign in with OAuth, sign in with OTP)\n    - [ ] Add MFA methods and SSO methods\n    - [x] 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](https://supabase.com/docs/guides/auth/server-side/email-based-auth-with-pkce-flow-for-ssr#create-api-endpoint-for-handling-tokenhash) in tandem with `verifyOTP` instead.\n- [x] Wrap [storage-py](https://github.com/supabase-community/storage-py)\n    - [ ]  Support resumable uploads\n    - [x]  Setup testing environment\n    - [x]  Document how to properly upload different file types (e.g. jpeg/png and download it)\n- [x] Wrap [functions-py](https://github.com/supabase-community/functions-py)\n\nOverall Tasks:\n- [x] Add async support across the entire library\n- [ ] Add FastAPI helper library (external to supabase-py)\n- [ ] Add `django-supabase-postgrest` (external to supabase-py)\n\n## Contributing\n\nContributing 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.\n\n\n\n\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "supabase-py with async synax",
    "version": "2.5.6",
    "project_urls": {
        "Homepage": "https://github.com/Atticuszz/supabase-py_async",
        "Repository": "https://github.com/Atticuszz/supabase-py_async"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2a18d32731a325c687cef1fc503fea653db0bd0c14ae2a9692c16b288a036cc3",
                "md5": "1732964bcc31585459d7530b5f69b61d",
                "sha256": "0a55c319b9d200e89312ea6e1f25d24a24ff4bc53ad38baf2c650ef68ea9f933"
            },
            "downloads": -1,
            "filename": "supabase_py_async-2.5.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1732964bcc31585459d7530b5f69b61d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 11776,
            "upload_time": "2024-03-22T00:39:50",
            "upload_time_iso_8601": "2024-03-22T00:39:50.316359Z",
            "url": "https://files.pythonhosted.org/packages/2a/18/d32731a325c687cef1fc503fea653db0bd0c14ae2a9692c16b288a036cc3/supabase_py_async-2.5.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5c80ef5bb1458c07c7508c6ab1de19f932d980d65a42db023ad9a65a94ad984e",
                "md5": "ea60746fd3a8a2148cd2b3c6dad8aa18",
                "sha256": "f7cd2af6042ed59820942ff30ef4a4170afb1ed68107b55f605667f964881a4d"
            },
            "downloads": -1,
            "filename": "supabase_py_async-2.5.6.tar.gz",
            "has_sig": false,
            "md5_digest": "ea60746fd3a8a2148cd2b3c6dad8aa18",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 12067,
            "upload_time": "2024-03-22T00:39:52",
            "upload_time_iso_8601": "2024-03-22T00:39:52.005611Z",
            "url": "https://files.pythonhosted.org/packages/5c/80/ef5bb1458c07c7508c6ab1de19f932d980d65a42db023ad9a65a94ad984e/supabase_py_async-2.5.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-22 00:39:52",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Atticuszz",
    "github_project": "supabase-py_async",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "lcname": "supabase-py-async"
}
        
Elapsed time: 0.24459s