Skip to main content

A package to use NocoDB API in a simple way

Project description

NocoDB Python Client

NocoDB is a great Airtable alternative. This client allows python developers to use NocoDB API in a simple way.

Installation

pip install nocodb

Usage

Client configuration

from nocodb.nocodb import NocoDBProject, APIToken, JWTAuthToken
from nocodb.filters import LikeFilter, EqFilter, And
from nocodb.infra.requests_client import NocoDBRequestsClient


# Usage with API Token
client = NocoDBRequestsClient(
        # Your API Token retrieved from NocoDB conf
        APIToken("YOUR-API-TOKEN"),
        # Your nocodb root path
        "http://localhost:8080"
)

# Usage with JWT Token
client = NocoDBRequestsClient(
        # Your API Token retrieved from NocoDB conf
        JWTAuthToken("your.jwt.token"),
        # Your nocodb root path
        "http://localhost:8080"
)

Project creation

# Example with default database
project_body = {"title": "My new project"}

# Example with Postgresql
project_body = {
    "title": "MyProject",
    "bases": [
        {
            "type": "pg",
            "config": {
                "client": "pg",
                "connection": {
                    "host": "localhost",
                    "port": "5432",
                    "user": "postgres",
                    "password": "postgres",
                    "database": "postgres"
                },
                "searchPath": [
                    "public"
                ]
            },
            "inflection_column": "camelize",
            "inflection_table": "camelize"
        }
    ],
    "external": True
}

project = client.project_create(body=project_body)

Project selection

# Be very carefull with org, project_name and table names
# weird errors from nocodb can arrive if they are wrong
# example: id is not defined...
# probably they will fix that in a future release.
project = NocoDBProject(
        "noco", # org name. noco by default
        "myproject" # project name. Case sensitive!!
)

Table rows operations

table_name = "tablename"

# Retrieve a page of rows from a table
table_rows = client.table_row_list(project, table_name)

# Retrieve the first 10000 rows
table_rows = client.table_row_list(project, table_name, params={'limit': 10000})

# Skip 100 rows
table_rows = client.table_row_list(project, table_name, params={'offset': 100})

# Filter the query
table_rows = client.table_row_list(project, table_name, LikeFilter("name", "%sam%"))
table_rows = client.table_row_list(project, table_name, And(LikeFilter("name", "%sam%"), EqFilter("age", 26)))
table_rows = client.table_row_list(project, table_name, filter_obj=EqFilter("Id", 100))

# Filter and count rows
count = client.table_count(project, table_name, filter_obj=EqFilter("Id", 100))

# Find one row
table_row = client.table_find_one(project, table_name, filter_obj=EqFilter("Id", 100), params={"sort": "-created_at"})

# Retrieve a single row
row_id = 10
row = client.table_row_detail(project, table_name, row_id)

# Create a new row
row_info = {
    "name": "my thoughts",
    "content": "i'm going to buy samuel a beer 🍻 because I 💚 this module",
    "mood": ":)"
}
client.table_row_create(project, table_name, row_info)

# Update a row
row_id = 2
row_info = {
    "content": "i'm going to buy samuel a new car 🚙 because I 💚 this module",
}
client.table_row_update(project, table_name, row_id, row_info)

# Delete a row (only if you've already bought me a beer)
client.table_row_delete(project, table_name, row_id)

Available filters

  • EqFilter
  • EqualFilter (Alias of EqFilter)
  • NotEqualFilter
  • GreaterThanFilter
  • GreaterOrEqualFilter
  • LessThanFilter
  • LessOrEqualFilter
  • LikeFilter
  • Or
  • Not
  • And

Combining filters using Logical operations

from nocodb import filters

# Basic filters...
nick_filter = filters.EqFilter("nickname", "elchicodepython")
country_filter = filters.EqFilter("country", "es")
girlfriend_code = filters.EqFilter("gfcode", "404")
current_mood_code = filters.EqFilter("moodcode", "418")

# Combining filters using logical filters
or_filter = filters.Or(nick_filter, country_filter)
and_filter = filters.And(girlfriend_code, current_mood_code)

# Negating filters with a Not filter
not_me = filters.Not(filters.EqFilter("nickname", "elchicodepython"))

# You can also combine combinations
or_combined_filter = filters.Or(or_filter, and_filter)
and_combined_filter = filters.And(or_filter, and_filter)

Using custom filters

Nocodb is evolving and new operators are coming with each release.

Most of the basic operations are inside this package but you could need some new feature that could not be added yet. For those filters you can build your own.

Example for basic filters:

from nocodb.filters.factory import basic_filter_class_factory

BasicFilter = basic_filter_class_factory('=')
table_rows = client.table_row_list(project, table_name, BasicFilter('age', '16'))

You can find the updated list of all the available nocodb operators here.

In some cases you might want to write your own filter string as described in the previous link. For that cases you can use the less-semmantic RawFilter.

from nocodb.filters.raw_filter import RawFilter

table_rows = client.table_row_list(project, table_name, RawFilter('(birthday,eq,exactDate,2023-06-01)'))

In some cases we might want to have a file with some custom raw filters already defined by us. We can easily create custom raw filter classes using raw_template_filter_class_factory.

from nocodb.filters.factory import raw_template_filter_class_factory

BirthdayDateFilter = raw_template_filter_class_factory('(birthday,eq,exactDate,{})')
ExactDateEqFilter = raw_template_filter_class_factory('({},eq,exactDate,{})')
ExactDateOpFilter = raw_template_filter_class_factory('({},{op},exactDate,{})')

table_rows = client.table_row_list(project, table_name, BirthdayDateFilter('2023-06-01'))
table_rows = client.table_row_list(project, table_name, ExactDateEqFilter('column', '2023-06-01'))
table_rows = client.table_row_list(project, table_name, ExactDateOpFilter('column', '2023-06-01', op='eq'))

Credits to @MitPitt for asking this feature.

Author notes

I created this package to bootstrap some personal projects and I hope it will help other developers from the python community. It's not completed but it has what I needed: A full CRUD with some filters.

Feel free to add new capabilities by creating a new MR.

Contributors

Contributors image

  • Samuel López Saura @elchicodepython
  • Ilya Sapunov @davert0
  • Delena Malan @delenamalan
  • Jan Scheiper @jangxx

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nocodb-2.0.1.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

nocodb-2.0.1-py3.9.egg (24.8 kB view details)

Uploaded Source

File details

Details for the file nocodb-2.0.1.tar.gz.

File metadata

  • Download URL: nocodb-2.0.1.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.16

File hashes

Hashes for nocodb-2.0.1.tar.gz
Algorithm Hash digest
SHA256 5cd44d79bbe3f2cdfd3fcda80e534bf208da28e8d316be9c87f05f80fbaa865c
MD5 702d257c86577ac0b2d1969aefcf6e98
BLAKE2b-256 fa3a9104066c4570a6d5fcdb294a385b12dddfba97c818bc90ea8500be647896

See more details on using hashes here.

File details

Details for the file nocodb-2.0.1-py3.9.egg.

File metadata

  • Download URL: nocodb-2.0.1-py3.9.egg
  • Upload date:
  • Size: 24.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for nocodb-2.0.1-py3.9.egg
Algorithm Hash digest
SHA256 0c96321518f71fd4bc361dd4937a6415451cfe7196ebf429254bcdbce8e381d8
MD5 b34cce933e7b7f8d0cde209b50452adb
BLAKE2b-256 3153088ee8cf35ec4e1831ec6f0851b5ab2aa7ba58d4ade3d68b1aa8bdde5ce8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page