Skip to content

Repository files navigation

Supabase Python Template

This template provides a quick start for integrating Supabase with Python applications using the official Supabase Python SDK. All API functions are pre-implemented for immediate use in your projects, with seamless Django integration.

Features

  • Complete Supabase SDK integration
  • Pre-implemented CRUD operations for database tables
  • Authentication methods (sign up, sign in, magic link, etc.)
  • Storage operations for file uploads and downloads
  • Real-time subscriptions
  • Edge Functions support
  • Ready-to-use test suite compatible with Django

Requirements

Installation

  1. Clone this repository:

    git clone https://github.com/yourusername/supabase-python-template.git
    cd supabase-python-template 
  2. Install the Supabase SDK:

    pip install supabase
  3. Create a .env file in the project root with your Supabase credentials:

    SUPABASE_URL=your_supabase_project_url
    SUPABASE_KEY=your_supabase_anon_key
    

Quick Start

Initialize Supabase Client

fromsupabase_clientimportcreate_client# Client is automatically initialized with environment variablessupabase=create_client()
# Or manually provide credentials# supabase = create_client("YOUR_SUPABASE_URL", "YOUR_SUPABASE_KEY")

Database Operations

# Get all items from a tabledata=supabase.table('table_name').select('*').execute()
# Get items with filteringdata=supabase.table('table_name').select('*').eq('column', 'value').execute()
# Insert datadata=supabase.table('table_name').insert({'column': 'value'}).execute()
# Update datadata=supabase.table('table_name').update({'column': 'new_value'}).eq('id', 1).execute()
# Delete datadata=supabase.table('table_name').delete().eq('id', 1).execute()

Authentication

# Sign upuser=supabase.auth.sign_up({
"email": "example@email.com",
"password": "example-password"
})
# Sign inuser=supabase.auth.sign_in_with_password({
"email": "example@email.com",
"password": "example-password"
})
# Sign in with magic linksupabase.auth.sign_in_with_otp({
"email": "example@email.com"
})
# Sign outsupabase.auth.sign_out()
# Get useruser=supabase.auth.get_user()

Storage

# Upload fileresult=supabase.storage.from_('bucket_name').upload(
'file_path.txt',
open('local_file.txt', 'rb'),
{'content-type': 'text/plain'}
)
# Download filedata=supabase.storage.from_('bucket_name').download('file_path.txt')
# Get public URLurl=supabase.storage.from_('bucket_name').get_public_url('file_path.txt')
# List filesfiles=supabase.storage.from_('bucket_name').list()
# Remove filesupabase.storage.from_('bucket_name').remove(['file_path.txt'])

Real-time Subscriptions

defhandle_broadcast(payload):
print(payload)
# Subscribe to changessubscription=supabase.table('table_name').on('INSERT', handle_broadcast).subscribe()
# Unsubscribesupabase.remove_subscription(subscription)

Edge Functions

# Invoke an edge functionresponse=supabase.functions.invoke('function_name', {'data': 'value'})

Django Integration

This template includes tests that work well with Django's testing framework. To use them:

  1. Add the template to your Django project

  2. Configure Supabase in your Django settings:

    # settings.pySUPABASE_URL='your_supabase_project_url'SUPABASE_KEY='your_supabase_anon_key'
  3. Run the included tests with Django's test runner:

    python manage.py test supabase_template

Advanced Usage

Using Row Level Security (RLS)

When using RLS policies in Supabase, you'll need to include the user's JWT token in your requests:

# Sign in firstresponse=supabase.auth.sign_in_with_password({
"email": "example@email.com",
"password": "example-password"
})
# Now you can access tables with RLS policiesdata=supabase.table('protected_table').select('*').execute()

Handling PostgreSQL Functions

# Call a PostgreSQL functiondata=supabase.rpc('function_name', {'arg1': 'value1'}).execute()

Foreign Table Joins

# Query with joinsdata=supabase.table('table_name').select('*, foreign_table(*)').execute()

Error Handling

The template includes pre-configured error handling for common Supabase operations:

try:
data=supabase.table('table_name').select('*').execute()
print(data)
exceptExceptionase:
print(f"An error occurred: {e}")

Environment Configuration

The template supports multiple environments through Django settings or environment variables:

# In Django settings.py for different environments:ifDEBUG:
SUPABASE_URL='development_url'SUPABASE_KEY='development_key'else:
SUPABASE_URL='production_url'SUPABASE_KEY='production_key'

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Resources

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages