Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

NocoDB Python API Client

License

NotificationsYou must be signed in to change notification settings

elchicodepython/python-nocodb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Installation

pip install nocodb

Usage

Client configuration

fromnocodb.nocodbimportNocoDBProject,APIToken,JWTAuthTokenfromnocodb.filtersimportLikeFilter,EqFilter,Andfromnocodb.infra.requests_clientimportNocoDBRequestsClient# Usage with API Tokenclient=NocoDBRequestsClient(# Your API Token retrieved from NocoDB confAPIToken("YOUR-API-TOKEN"),# Your nocodb root path"http://localhost:8080")# Usage with JWT Tokenclient=NocoDBRequestsClient(# Your API Token retrieved from NocoDB confJWTAuthToken("your.jwt.token"),# Your nocodb root path"http://localhost:8080")

Project creation

# Example with default databaseproject_body= {"title":"My new project"}# Example with Postgresqlproject_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 tabletable_rows=client.table_row_list(project,table_name)# Retrieve the first 1000 rowstable_rows=client.table_row_list(project,table_name,params={'limit':1000})# Skip 100 rowstable_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 paginateto 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 querytable_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 rowscount=client.table_count(project,table_name,filter_obj=EqFilter("Id",100))# Find one rowtable_row=client.table_find_one(project,table_name,filter_obj=EqFilter("Id",100),params={"sort":"-created_at"})# Retrieve a single rowrow_id=10row=client.table_row_detail(project,table_name,row_id)# Create a new rowrow_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 rowrow_id=2row_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

fromnocodbimportfilters# 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 filtersor_filter=filters.Or(nick_filter,country_filter)and_filter=filters.And(girlfriend_code,current_mood_code)# Negating filters with a Not filternot_me=filters.Not(filters.EqFilter("nickname","elchicodepython"))# You can also combine combinationsor_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 newfeature that could not be added yet.For those filters you can build your own.

Example for basic filters:

fromnocodb.filters.factoryimportbasic_filter_class_factoryBasicFilter=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 operatorshere.

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.

fromnocodb.filters.raw_filterimportRawFiltertable_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 usingraw_template_filter_class_factory.

fromnocodb.filters.factoryimportraw_template_filter_class_factoryBirthdayDateFilter=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 itwill help other developers from the python community. It's not completed butit 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

[8]ページ先頭

©2009-2025 Movatter.jp