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-templateInstall 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
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")# 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()# 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()# 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'])defhandle_broadcast(payload):
print(payload)
# Subscribe to changessubscription=supabase.table('table_name').on('INSERT', handle_broadcast).subscribe()
# Unsubscribesupabase.remove_subscription(subscription)# Invoke an edge functionresponse=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.pySUPABASE_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 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()# Call a PostgreSQL functiondata=supabase.rpc('function_name', {'arg1': 'value1'}).execute()# Query with joinsdata=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)
exceptExceptionase:
print(f"An error occurred: {e}")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'- 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.