--- title: 'Use Supabase with Python' subtitle: 'Learn how to create a Supabase project, add some sample data to your database, and query the data from a Python app.' breadcrumb: 'Framework Quickstarts' --- <$Partial path="quickstart_db_setup.mdx" /> ## 3. Create a Python app with Flask [#create-an-app] Create a directory for your Python app and set up a virtual environment. ```bash mkdir my-app && cd my-app python3 -m venv venv source venv/bin/activate ``` ## 4. Set up AI tooling (optional) [#set-up-ai-tooling] <$Partial path="quickstart_ai_tooling.mdx" /> ## 5. Install Flask and the Supabase client library [#install-the-client-library] Use Flask as the web framework and the `supabase-py` client library to query and update your Supabase data from a Python app. Install both packages using `pip`. ```bash pip install flask supabase ``` ## 6. Declare Supabase environment variables [#declare-environment-variables] Create a `.env` file in your project root and add your Supabase connection variables. Get them from the following helper, or from the project [**Connect** panel](/dashboard/project/_?showConnect=true&connectTab=frameworks&framework=flask): ```dotenv name=.env SUPABASE_URL= SUPABASE_PUBLISHABLE_KEY= ``` <$Partial path="api_settings.mdx" variables={{ "framework": "flask", "tab": "frameworks" }} /> ## 7. Create the Supabase client [#create-the-client] Install the `python-dotenv` package to load environment variables: ```bash pip install python-dotenv ``` Create an `app.py` file and initialize the Flask app and the Supabase client from your environment variables. ```python name=app.py import os from flask import Flask from supabase import create_client, Client from postgrest import APIError from dotenv import load_dotenv load_dotenv() app = Flask(__name__) supabase: Client = create_client( os.environ.get("SUPABASE_URL"), os.environ.get("SUPABASE_PUBLISHABLE_KEY") ) ``` ## 8. Query data from the app [#query-data] Add a route to `app.py` that fetches data from your `instruments` table. ```python name=app.py @app.route('/') def index(): try: response = supabase.table('instruments').select("*").execute() except APIError as error: return f'

Error loading instruments: {error.message}

' instruments = response.data html = '

Instruments

    ' for instrument in instruments: html += f'
  • {instrument["name"]}
  • ' html += '
' return html if __name__ == '__main__': app.run(debug=True) ``` ## 9. Start the app [#start-the-app] Run the Flask development server and go to `http://localhost:5000` in a browser. The page lists the instruments. ```bash python app.py ``` On macOS, the page might not load because the AirPlay Receiver service uses port 5000. Turn off **AirPlay Receiver** in **System Settings > General > AirDrop & Handoff**, or run the app on another port with `app.run(debug=True, port=5001)`. <$Partial path="quickstart_going_to_production.mdx" /> ## Next steps Build on this app with the following resources: - Set up [Auth](/docs/guides/auth) for your app - [Insert more data](/docs/guides/database/import-data) into your database - Upload and serve static files using [Storage](/docs/guides/storage)