Start with Python

Learn how to setup your first Python project powered by Appwrite.

1

Create project

Head to the Appwrite Console.

If this is your first time using Appwrite, create an account and create your first project.

Create project screen

Create project screen

Then, under Integrate with your server, add an API Key with the following scopes.

Server integrations

Server integrations

CategoryRequired scopesPurpose
Databasedatabases.writeAllows API key to create, update, and delete databases.
collections.writeAllows API key to create, update, and delete collections.
attributes.writeAllows API key to create, update, and delete attributes.
documents.readAllows API key to read documents.
documents.writeAllows API key to create, update, and delete documents.

Other scopes are optional.

Project settings screen

Project settings screen

2

Create Python project

Create a directory for the project.

Shell
mkdir my_app
cd my_app

After that, create a virtual environment in this directory and activate it.

Shell
# Create a venv
python -m venv .venv

# Active the venv in Unix shell
source .venv/bin/activate

# Or in Powershell
.venv/Scripts/Activate.ps1

Finally, create a file my_app.py.

3

Install Appwrite

Install the Python Appwrite SDK.

Shell
pip install appwrite==4.1.0
4

Import Appwrite

Find your project ID in the Settings page. Also, click on the View API Keys button to find the API key that was created earlier.

Project settings screen

Project settings screen

Open my_app.py and initialize the Appwrite Client. Replace <YOUR_PROJECT_ID> with your project ID and <YOUR_API_KEY> with your API key.

Python
from appwrite.client import Client
from appwrite.services.databases import Databases
from appwrite.id import ID

client = Client()
client.set_endpoint('https://cloud.appwrite.io/v1')
client.set_project('<YOUR_PROJECT_ID>')
client.set_key('<YOUR_API_KEY>')
5

Initialize database

Once the Appwrite Client is initialized, create a function to configure a todo collection.

Python
databases = Databases(client)

todoDatabase = None
todoCollection = None

def prepare_database():
  global todoDatabase
  global todoCollection

  todoDatabase = databases.create(
    database_id=ID.unique(),
    name='TodosDB'
  )

  todoCollection = databases.create_collection(
    database_id=todoDatabase['$id'],
    collection_id=ID.unique(),
    name='Todos'
  )

  databases.create_string_attribute(
    database_id=todoDatabase['$id'],
    collection_id=todoCollection['$id'],
    key='title',
    size=255,
    required=True
  )

  databases.create_string_attribute(
    database_id=todoDatabase['$id'],
    collection_id=todoCollection['$id'],
    key='description',
    size=255,
    required=False,
    default='This is a test description.'
  )

  databases.create_boolean_attribute(
    database_id=todoDatabase['$id'],
    collection_id=todoCollection['$id'],
    key='isComplete',
    required=True
  )
6

Add documents

Create a function to add some mock data into your new collection.

Python
def seed_database():
  testTodo1 = {
    'title': "Buy apples",
    'description': "At least 2KGs",
    'isComplete': True
  }

  testTodo2 = {
    'title': "Wash the apples", 
    'isComplete': True
  }

  testTodo3 = {
    'title': "Cut the apples",
    'description': "Don\'t forget to pack them in a box",
    'isComplete': False
  }

  databases.create_document(
    database_id=todoDatabase['$id'],
    collection_id=todoCollection['$id'],
    document_id=ID.unique(),
    data=testTodo1
  )

  databases.create_document(
    database_id=todoDatabase['$id'],
    collection_id=todoCollection['$id'],
    document_id=ID.unique(),
    data=testTodo2
  )

  databases.create_document(
    database_id=todoDatabase['$id'],
    collection_id=todoCollection['$id'],
    document_id=ID.unique(),
    data=testTodo3
  )
7

Retrieve documents

Create a function to retrieve the mock todo data, then execute the functions in _main_.

Python
def get_todos():
  todos = databases.list_documents(
    database_id=todoDatabase['$id'],
    collection_id=todoCollection['$id']
  )
  for todo in todos['documents']:
    print(f"Title: {todo['title']}\nDescription: {todo['description']}\nIs Todo Complete: {todo['isComplete']}\n\n")

if __name__ == "__main__":
  prepare_database()
  seed_database()
  get_todos()
8

All set

Run your project with python my_app.py and view the response in your console.