Skip to content

Commit 3778aef

Browse files
authored
Merge branch 'master' into docs/tools_reference
2 parents 2f4bbaf + 4a88bf7 commit 3778aef

File tree

17 files changed

+299
-98
lines changed

17 files changed

+299
-98
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
---
2+
title: Python data loading with Supabase
3+
description: An example of how to load data into Supabase using supabase-py
4+
author: ant_wilson
5+
image: python-1/python.jpg
6+
thumb: python-1/python.jpg
7+
tags:
8+
- python
9+
- open-source
10+
- community
11+
date: '2022-06-17'
12+
toc_depth: 3
13+
---
14+
15+
Python is an excellent choice when it comes to data science. With a wide selection of libraries and built-in analytics tools, you can crunch data with ease, analyze even the most complex datasets, and visualize your results in beautiful charts and graphs.
16+
17+
Supabase is backend-as-a-service built on top of PostgreSQL. It’s an excellent choice for building modern data-intensive apps and tooling.
18+
19+
Thanks to our incredible community, Supabase now has a powerful and open source [Python SDK](https://github.com/supabase-community/supabase-py). With Supabase and Python, you can automate tasks such as CRUD operations with only a few lines of code. This guide will first create a simple schema in Supabase, then we’ll use the Supabase Python SDK to show how you can load sample data.
20+
21+
## Prerequisites
22+
23+
Before we dive in, let’s look at some prerequisites you'll need:
24+
- Python version > 3.7
25+
- The SDK only supports version > 3.7. You can download a supported Python version from [here](https://www.python.org/downloads/).
26+
- Python virtual environment
27+
- This is optional, but it will avoid issues of package dependencies and version conflicts. You can find the steps to create a virtual environment [here](https://uoa-eresearch.github.io/eresearch-cookbook/recipe/2014/11/26/python-virtual-env/). We will be using PyCharm to harness its venv creation capabilities.
28+
- Faker python package
29+
- We will be using the [faker-commerce](https://pypi.org/project/faker-commerce/) package from the Faker library in Python to generate realistic sample data.
30+
31+
32+
## Loading data into Supabase using Python
33+
Supabase is built for developers, and you can [get started for free](https://app.supabase.io) using your existing Github account. Once your Supabase account is set up, you will access the Supabase dashboard. From here, go to All Project > New Project.
34+
35+
![screen shot of supabase dashboard](/images/blog/python-1/1.png)
36+
37+
Give your project a name and set the database password. You can also choose the region and adjust the pricing plan based on the requirements of your project.
38+
39+
![screen shot of supabase dashboard](/images/blog/python-1/2.png)
40+
41+
Your project will spin up within 2 minutes.
42+
43+
## Creating tables in Supabase
44+
45+
In this example, we’ll be creating 2 tables in Supabase:
46+
- Vendor (fields are vendor_name, vendor_location, and total_employees)
47+
- Product (vendor_id as FK, product_name, price, and total orders)
48+
49+
The database schema will look like the following:
50+
51+
![screen shot of supabase dashboard](/images/blog/python-1/3.png)
52+
53+
Let us now begin creating the tables. Once you create a project, you will need to go to Table Editor > New Table
54+
55+
![screen shot of supabase dashboard](/images/blog/python-1/4.png)
56+
57+
Now, you can create a table according to the defined schema.
58+
59+
![screen shot of supabase dashboard](/images/blog/python-1/5.png)
60+
Click on Save to create your vendor table. Similarly, create the product table.
61+
62+
![screen shot of supabase dashboard](/images/blog/python-1/6.png)
63+
64+
Before you click Save, you need to set up the foreign key relationship between the Product and Vendor table. To do this, select the button next to “vendor_id”
65+
66+
![screen shot of supabase dashboard](/images/blog/python-1/7.png)
67+
Select the vendor_id primary key from the “Vendor” table.
68+
69+
![screen shot of supabase dashboard](/images/blog/python-1/8.png)
70+
71+
Click on Save and you are good to go. You should now see the 2 tables under Table Editor.
72+
73+
![screen shot of supabase dashboard](/images/blog/python-1/9.png)
74+
75+
## Installing the Python SDK
76+
77+
Once you have set up the tables and installed the prerequisites, you can now start playing around with the [Python SDK](https://github.com/supabase-community/supabase-py). To install the SDK, run the following command:
78+
79+
`pip3 install supabase`
80+
81+
Ensure that you are running this inside your python virtual environment. This will take a few minutes to complete.
82+
83+
## Using Supabase API keys
84+
85+
The SDK authentication uses API keys pointing to a project URL. To find out your project URL and APIs, go to Settings > API.
86+
87+
![screen shot of supabase dashboard](/images/blog/python-1/10.png)
88+
89+
## Setting up the environment variables
90+
91+
API credentials and project URL can be stored in environment variables. Setting the environment variables in bash/zsh is very simple. All we need to do is run:
92+
93+
```bash
94+
export <variable_name>=<variable_value>
95+
```
96+
97+
So for our example we will set them up like this:
98+
99+
```bash
100+
export SUPABASE_URL=<<the value under config > URL>>
101+
export SUPABASE_KEY=<<the value present in Project API keys > anon public>>
102+
export SUPABASE_SECRET_KEY=<<the value present in Project API keys > service_role secret>>
103+
```
104+
105+
## Inserting data into Supabase
106+
107+
Here is a snippet of the code we will be using to insert random data into our tables:
108+
109+
```python
110+
import os
111+
import json
112+
from dotenv import load_dotenv
113+
from supabase import create_client, Client
114+
from faker import Faker
115+
import faker_commerce
116+
117+
118+
def add_entries_to_vendor_table(supabase, vendor_count):
119+
fake = Faker()
120+
foreign_key_list = []
121+
fake.add_provider(faker_commerce.Provider)
122+
main_list = []
123+
for i in range(vendor_count):
124+
value = {'vendor_name': fake.company(), 'total_employees': fake.random_int(40, 169),
125+
'vendor_location': fake.country()}
126+
127+
main_list.append(value)
128+
data = supabase.table('Vendor').insert(main_list).execute()
129+
data_json = json.loads(data.json())
130+
data_entries = data_json['data']
131+
for i in range(len(data_entries)):
132+
foreign_key_list.append(int(data_entries[i]['vendor_id']))
133+
return foreign_key_list
134+
135+
136+
def add_entries_to_product_table(supabase, vendor_id):
137+
fake = Faker()
138+
fake.add_provider(faker_commerce.Provider)
139+
main_list = []
140+
iterator = fake.random_int(1, 15)
141+
for i in range(iterator):
142+
value = {'vendor_id': vendor_id, 'product_name': fake.ecommerce_name(),
143+
'inventory_count': fake.random_int(1, 100), 'price': fake.random_int(45, 100)}
144+
main_list.append(value)
145+
data = supabase.table('Product').insert(main_list).execute()
146+
147+
148+
def main():
149+
vendor_count = 10
150+
load_dotenv()
151+
url: str = os.environ.get("SUPABASE_URL")
152+
key: str = os.environ.get("SUPABASE_KEY")
153+
supabase: Client = create_client(url, key)
154+
fk_list = add_entries_to_vendor_table(supabase, vendor_count)
155+
for i in range(len(fk_list)):
156+
add_entries_to_product_table(supabase, fk_list[i])
157+
158+
159+
main()
160+
```
161+
162+
To summarize what we have done using this code snippet:
163+
- We have inserted 10 random vendors to the table.
164+
- For each of the 10 vendors, we have inserted a number of different products
165+
166+
## Reading the data stored in Supabase
167+
168+
Data can also be viewed directly from the Supabase dashboard. To do this, go to `Table Editor > All tables`
169+
170+
![screen shot of supabase dashboard](/images/blog/python-1/11.png)
171+
172+
![screen shot of supabase dashboard](/images/blog/python-1/12.png)
173+
Note: In case you cannot see any of the data, you should hit the Refresh button.
174+
175+
## Conclusion
176+
With Python, data loading into Supabase is easy. It just takes a few easy steps to get started with the Python SDK and Supabase. In the next part of this blog series, we will learn how to visualize the data that we just loaded into Supabase using Metabase. Stay tuned!
177+
178+
If you have any questions please reach out via [Twitter](https://twitter.com/supabase) or join our [Discord](https://discord.supabase.com).
47.4 KB
Loading
179 KB
Loading
170 KB
Loading
179 KB
Loading
222 KB
Loading
264 KB
Loading
105 KB
Loading
157 KB
Loading
162 KB
Loading

0 commit comments

Comments
 (0)