Connecting a Flask App to Supabase

Few interesting things about me:
- Software Engineer @ JP Morgan Chase & Co.
- AWS Community Builder 2023
- I love working with technology
- I enjoy playing video games 😄
Search for a command to run...

Few interesting things about me:
Does the supabase package have any documentation? This is not working: "from supabase import create_client, Client"
Hey there! Take a look at this - https://github.com/supabase-community/supabase-py
Since it's in development and Supabase is open source, they constantly keep making changes.
Now the command to install supabase is simply -

Hope this helps! :)
In this series, I cover creating an API using Python Flask right from scratch, connecting it to a database and later deploying it.
Welcome to Part 3 of the Python Flask API Series. I'm really glad you've made it till here. If you have followed along so far, you will have a RESTful API which returns a list of your favorite games from our database and also helps you add games to ...
If you’re new to machine learning, reinforcement learning, especially, then it’s completely okay to have a hard time understanding these fancy jargons in front of you. Here’s my attempt to simplify these while we work on a little robo-car :) About AW...

Dynamo DB is AWS's NoSQL offering in the vast set of managed databases as a service that they provide. Like most other services, it's fully serverless, flexible and easy to scale. The Data Model As we're working on NoSQL here, there's no real restric...

I was recently a part of a hackathon and was found searching for good Jupyter environments on the cloud to be able to work on my ML Project. Soon enough, I found myself here. However, it took me a while to figure out how powerful it truly can be. F...

I spend 5 days out of a week coding for work, so I'm definitely not motivated to code a lot on weekends. This means that I do not have any software development tools installed on my personal machine. Not even IDEs. However, being the curious little g...

Almost a week to this and not a lot of drama on Indian Twitter, so I thought I could write about it. TLDR: Malicious code with a backdoor was added to one of the core packages of Linux affecting almost all Debian and Redhat distros; was later discove...

Welcome to Part 2 of the Flask Series.
If you have been following along, you'd have a simple API in Flask which returns your favorite games. If you haven't, simply clone this GitHub repository and install flask and dotenv packages.
What we did the last time, was displaying a list of our favorite games from a list in a separate games file, which isn't really the ideal thing to do. In this thread, let's connect our application to a PostgreSQL database.
Before we begin with Python Code, I'd like to take you through Supabase.
Supabase is an Open Source Backend as a Service. It provides a Postgres Database on the fly, it has a very easy-to-use UI and it provides its PostgREST API and client-libraries on various languages (thanks to the Community support) for easy database operations.
Installing pgadmin and setting up a database in that and then connecting to it, is a PAIN. Let's just do it the easy way. Click here, login with GitHub and create a new Supabase project.

Choose your default organization (already created for you), enter a name and a secure password for your project. Then select the region which is closest to you for less delay.

And here we are. In less than 5 minutes, we have our Postgres database set up. Now, click on Table Editor and then Click on Create New Table.

Create a new 'games' table. You can add columns/attributes to this table using the Add Column button. Finally, save this.
Great! Now our table is ready. All we need to do here now, is to add some rows to this table. To do this, simply click on Insert Row.
You just have to enter the name of the game and the no. of hours you've played it. I'll just add 4-5 of these.
Here's what the table looks like after a few entries and now you know I game a lot XD
Add environment variables to your .flaskenv file

You can find these in Supabase settings => API

The python supabase-py module (and many other libraries for different languages) was created (and is still in development) by the awesome Supabase Community. You can find the docs for this module here.
Install this module in your virtual env using pip -
python -m pip install supabase_py
Let's go ahead and connect our app to Supabase! Create a new file store.py and import supabase. Then, create a supabase Client variable which we can use for our database operations.
import os
from supabase import create_client, Client
url: str = os.environ.get("SUPABASE_URL")
key: str = os.environ.get("SUPABASE_ANON_KEY")
supabase: Client = create_client(url, key)
Next up, in the same file, simply create a new function which returns data of all games from our database.
# Function to Fetch All Games
def find_all_games():
data = supabase.table("games").select("*").execute()
# Equivalent for SQL Query "SELECT * FROM games;"
return data['data']
games = find_all_games()
You can go ahead and print this on the console to see what it looks like -

In app.py, import the games variable from the store instead of our hardcoded games file (you can actually go ahead and delete that, we aren't using it anymore).
#from games import games ## Delete this line
from store import games
That's pretty much it. Now run the app using -
flask run
If you now go to the route, you'll see a list of games show up, exactly what we added in our database!
/games

Our API has dynamic data coming from a database!
In store.py, let's create a new function to add a new game to the database. We just take in title and hours as the parameters and insert it into the database
# Function to add a new game
def add_game_to_DB(title, hours) -> dict:
game = {
"title": title,
"hours": hours
}
data = supabase.table("games").insert(game).execute()
# Equivalent to the SQL Insert
return data['data']
Now here's where things get a little interesting. To send data over the API, we need to send a POST Request to the API. To do that, we need to create a new endpoint which accepts POST requests and inserts this data into the database.
Before we create a new route, let's just import request and Response to deal with the request data that we pass in. We'll also import the new function we just created.
from flask import request, Response
from store import add_game_to_DB
We will now create a route which takes a POST request, grabs the data from the request body and adds the data to the database.
@app.route('/games/add', methods=['POST'])
def add_game():
data = request.get_json()
try:
title = data['title']
hours = data['hours']
if title and hours:
data = add_game_to_DB(title, int(hours))
return jsonify(data), 201
except:
return Response('''{"message": "Bad Request"}''', status=400, mimetype='application/json')
You can identify a few changes here. We add a methods parameter to the route method, specifying that the route only responds to POST requests.
We are also doing some data validation here, hence the try-except blocks. So if any of the values for hours or title are missing, we return a 400-Bad Request in the response.
If everything is good, we call the add_game_to_DB function and send it the game details. We return the data we get as our response.
To put this to test, open up your REST Client. I'm using Postman in this case.
Create a New Request and add the game details like so -

Click on Send and you should see a response like this -

We can go back to Supabase and check if the data was inserted correctly.

There you go, we now have a fully functional API where we get dynamic data from our database and also add to it. I hope you had a good time working with Flask
I also hope you enjoyed working with Supabase. Trust me there's a lot more you can explore :)
That's it for this thread folks, next up, we will deploy our application to Heroku. I'm sure that'll be exciting! See you then, thanks for reading!