Giter Club home page Giter Club logo

python-nocodb's Introduction

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 1000 rows
table_rows = client.table_row_list(project, table_name, params={'limit': 1000})

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

⚠️ Seems that we can't retrieve more than 1000 rows at the same time but we can paginate to retrieve all the rows from a table

Pagination example

first_100_rows = client.table_row_list(project, table_name, params={'limit': 100})
next_100_rows = client.table_row_list(project, table_name, params={'limit': 100, 'offset': 100})
next_100_rows = client.table_row_list(project, table_name, params={'limit': 100, 'offset': 200})

More row operations

# 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

python-nocodb's People

Contributors

alex-berlin-tv avatar davert0 avatar delenamalan avatar elchicodepython avatar fernando24164 avatar jangxx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

python-nocodb's Issues

Read all entrys from table (pagination)

Thanks for providing this projekt in python and the initial documentation.
If I send my "table_row_list", the return list only contains 25 rows of the table. Is there a parameter to fetch all rows at once or drill down to fetch the next 25 rows?
(client.table_row_list(project, table_name, InFilter("lager", lagerType)))

Thanks for answer.

getting all rows from a table doesn't really work

#10 (comment)

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

I saw that the only first 1000 rows can be extracted at once and not 10000 as you say on readme
I have also attached this image showing the length of rows.
If you can fix that then it would be great, Thanks :)

YeMn0RTZCg

πŸš€ Python-nocodb is getting bigger so time for adding more tests and ci to it πŸ™ŒπŸ»

This was something that I've made for myself at the beginning but as it seems that is being used by a lot of people we need to make sure that everything works like a charm.

I'm creating this issue to self-assign some tasks although help is always welcome.

Tasks to be done:

  • Add more tests.
  • Add black to format the project.
  • Automate black, mypy and tests in ci.
  • Improve docs. It would be great to have a webpage instead of a README that its getting bigger and bigger.

How to create links between records?

Hi,
I tried to create links between records with:

client.table_row_create(project, "disks", {"machine.Id": machine_id)
In my example I have a machine table and a disk table having a one to many relationship.
Is it possible to create links records? Or at least to create the link between existing records?

Create automatic pagination feature

It could be great to have an object or method that let you paginate the returned rows.

Something like:

client.table_row_list(...auto_paginate=True) -> Generator[Row]

Related issue: #10

whl is missing at pypi - pip install failed

Hi,
Seems like whl file is missing for nocodb 2.0.0 and 2.0.1 at pypi.org. pip is skipping releases where egg file exists:
pip install nocodb==2.0.1 -vvv ... https://files.pythonhosted.org/packages/64/58/dbd52713b948b7c30ed4bdabd3b2b9655781e4dd581d89807cb5af379185/nocodb-1.0.0-py3-none-any.whl (from https://pypi.org/simple/nocodb/), version: 1.0.0 Found link https://files.pythonhosted.org/packages/93/c2/fab4554f7d4206df5a776b48923d16c1e45167541e7b14a7f2ff8e3cfa76/nocodb-1.0.1-py3-none-any.whl (from https://pypi.org/simple/nocodb/), version: 1.0.1 Found link https://files.pythonhosted.org/packages/fb/6b/1a7e04c3f128860396e4734fc569ad150608f7a8861099a4d21dbce79096/nocodb-2.0.0a1.tar.gz (from https://pypi.org/simple/nocodb/), version: 2.0.0a1 Found link https://files.pythonhosted.org/packages/dd/2f/c72c42d254b085e59dc8900c0bf612ab1773d4ff52e55bcba030302bcd3c/nocodb-2.0.0a2-py3-none-any.whl (from https://pypi.org/simple/nocodb/), version: 2.0.0a2 Skipping link: unsupported archive format: .egg: https://files.pythonhosted.org/packages/37/0a/37e8450163537055fd93c42a081457b4892ce19baca6b402bea3938ef828/nocodb-2.0.0-py3.9.egg (from https://pypi.org/simple/nocodb/) Skipping link: unsupported archive format: .egg: https://files.pythonhosted.org/packages/31/53/088ee8cf35ec4e1831ec6f0851b5ab2aa7ba58d4ade3d68b1aa8bdde5ce8/nocodb-2.0.1-py3.9.egg (from https://pypi.org/simple/nocodb/) Skipping link: not a file: https://pypi.org/simple/nocodb/ Given no hashes to check 0 links for project 'nocodb': discarding no candidates ERROR: Could not find a version that satisfies the requirement nocodb==2.0.1 (from versions: 0.0.1, 0.0.2, 0.1.0, 1.0.0, 1.0.1, 2.0.0a1, 2.0.0a2) ERROR: No matching distribution found for nocodb==2.0.1 ...
Isn't egg - outdated? Can wheel format be added for pip to work properly?
Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.