Table Class
The Table
class provides an interface to interact with a Baserow table. Through this class, users can perform CRUD operations on rows, query table information, retrieve field properties, and utilize various filtering and sorting options. Below, we showcase the properties and methods available in the Table
class along with examples of common use cases.
Important Note: The get_rows()
function from the Baserow API returns paged results with a default of 100 rows per page. When using this function, an iterator is returned. This iterator abstracts the paging mechanism, providing a seamless experience for fetching rows.
Properties
id
: Table’s unique identifier.primary_field
: The primary field of the table.fields
: Dictionary of table fields (columns) with their properties.field_names
: List of field names present in the table.
Methods and Usage
from baserowapi import Baserow, Filter
baserow = Baserow(token='mytoken')
# Initializing a table instance
table = baserow.get_table(1234567)
# Displaying table properties
print(table.id)
print(table.primary_field)
# Accessing table fields properties
print(table.fields['Name'])
print(table.fields['Name'].order)
print(table.fields['Name'].field_data)
# Getting field names for the table
print(table.field_names)
# Fetching a single row by its ID
my_row = table.get_row(1)
# Fetching all rows, returns list
for row in table.get_rows():
print(row['Name'])
# Fetching all rows as a generator (efficient for large datasets)
for row in table.get_rows(iterator=True):
print(row['Name'])
# Fetching rows with filters
rows_with_name_grace = table.get_rows(filters=[Filter("Name", "Grace")])
for row in rows_with_name_grace:
print(row.to_dict())
# Fetching rows using multiple filters (OR logic)
rows_with_name_ada_or_grace = table.get_rows(filters=[Filter("Name", "Ada"), Filter("Name", "Grace")], filter_type='OR')
for row in rows_with_name_ada_or_grace:
print(row.to_dict())
# Sorting rows
rows_name_ascending = table.get_rows(order_by=["Name"])
for row in rows_name_ascending:
print(row.to_dict())
# Fetching rows with a search term
rows_with_test = table.get_rows(search="test")
for row in rows_with_test:
print(row.to_dict())
# Fetching rows from a specific view
rows_from_view = table.get_rows(view_id=12345678)
for row in rows_from_view:
print(row.to_dict())
# Limiting fields in the fetched rows
rows_with_include = table.get_rows(include=['Name','Last name', 'Notes', 'Active'])
for row in rows_with_include:
print(row.to_dict())
# Excluding specific fields from the fetched rows
rows_with_exclude = table.get_rows(exclude=['Notes','Active'])
for row in rows_with_exclude:
print(row.to_dict())
# Limit number of rows fetched
single_row = table.get_rows(limit=1)
# Adding a new row
new_row_data = {
'Name': 'Ringo',
'Last name': 'Staar',
'Notes': 'drums',
'Active': True
}
added_row = table.add_rows(new_row_data)
# Add multiple rows
rows_data = [
{"Name": "Alice", "Last name": "Smith", "Notes": "VIP customer", "Active": True},
{"Name": "Bob", "Last name": "Johnson", "Notes": "Pending review", "Active": False},
]
# Add the rows to the table
added_rows = table.add_rows(rows_data)
# Updating rows
rows_data = [
{"id": 1, "Notes": "Alice has a new order", "Active": True},
{"id": 2, "Notes": "Bob's review completed", "Active": True},
]
# Update the rows
updated_rows = table.update_rows(rows_data)
# Deleting rows
row_ids = [1, 2]
# Delete the rows
success = table.delete_rows(row_ids)
# Confirm the deletion
if success:
print(f"Deleted rows with IDs: {row_ids}")