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.
- 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
- Python 3.7+
- Supabase project (create one at supabase.com)
-
Clone this repository:
git clone https://github.com/yourusername/supabase-python-template.git cd supabase-python-template -
Install the Supabase SDK:
pip install supabase
-
Create a
.envfile in the project root with your Supabase credentials:SUPABASE_URL=your_supabase_project_url SUPABASE_KEY=your_supabase_anon_key
from supabase_client import create_client # Client is automatically initialized with environment variables supabase = create_client() # Or manually provide credentials # supabase = create_client("YOUR_SUPABASE_URL", "YOUR_SUPABASE_KEY")
# Get all items from a table data = supabase.table('table_name').select('*').execute() # Get items with filtering data = supabase.table('table_name').select('*').eq('column', 'value').execute() # Insert data data = supabase.table('table_name').insert({'column': 'value'}).execute() # Update data data = supabase.table('table_name').update({'column': 'new_value'}).eq('id', 1).execute() # Delete data data = supabase.table('table_name').delete().eq('id', 1).execute()
# Sign up user = supabase.auth.sign_up({ "email": "[email protected]", "password": "example-password" }) # Sign in user = supabase.auth.sign_in_with_password({ "email": "[email protected]", "password": "example-password" }) # Sign in with magic link supabase.auth.sign_in_with_otp({ "email": "[email protected]" }) # Sign out supabase.auth.sign_out() # Get user user = supabase.auth.get_user()
# Upload file result = supabase.storage.from_('bucket_name').upload( 'file_path.txt', open('local_file.txt', 'rb'), {'content-type': 'text/plain'} ) # Download file data = supabase.storage.from_('bucket_name').download('file_path.txt') # Get public URL url = supabase.storage.from_('bucket_name').get_public_url('file_path.txt') # List files files = supabase.storage.from_('bucket_name').list() # Remove file supabase.storage.from_('bucket_name').remove(['file_path.txt'])
def handle_broadcast(payload): print(payload) # Subscribe to changes subscription = supabase.table('table_name').on('INSERT', handle_broadcast).subscribe() # Unsubscribe supabase.remove_subscription(subscription)
# Invoke an edge function response = supabase.functions.invoke('function_name', {'data': 'value'})
This template includes tests that work well with Django's testing framework. To use them:
-
Add the template to your Django project
-
Configure Supabase in your Django settings:
# settings.py SUPABASE_URL = 'your_supabase_project_url' SUPABASE_KEY = 'your_supabase_anon_key'
-
Run the included tests with Django's test runner:
python manage.py test supabase_template
When using RLS policies in Supabase, you'll need to include the user's JWT token in your requests:
# Sign in first response = supabase.auth.sign_in_with_password({ "email": "[email protected]", "password": "example-password" }) # Now you can access tables with RLS policies data = supabase.table('protected_table').select('*').execute()
# Call a PostgreSQL function data = supabase.rpc('function_name', {'arg1': 'value1'}).execute()
# Query with joins data = supabase.table('table_name').select('*, foreign_table(*)').execute()
The template includes pre-configured error handling for common Supabase operations:
try: data = supabase.table('table_name').select('*').execute() print(data) except Exception as e: print(f"An error occurred: {e}")
The template supports multiple environments through Django settings or environment variables:
# In Django settings.py for different environments: if DEBUG: SUPABASE_URL = 'development_url' SUPABASE_KEY = 'development_key' else: SUPABASE_URL = 'production_url' SUPABASE_KEY = 'production_key'
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.