"""
Basic Usage Example for Lark Base Client
This example demonstrates fundamental CRUD operations with Lark Base:
- Creating a table
- Adding fields
- Creating, reading, updating, and deleting records
"""
import os
from dotenv import load_dotenv
from lark_client import LarkBaseClient, LarkConfig
def main():
"""Run basic usage example."""
# Load environment variables
load_dotenv()
# Initialize client
config = LarkConfig(
app_id=os.getenv('LARK_APP_ID'),
app_secret=os.getenv('LARK_APP_SECRET'),
log_level='INFO'
)
client = LarkBaseClient(config, rate_limit=10)
# Your Lark Base app token
app_token = os.getenv('LARK_APP_TOKEN')
print("=== Lark Base Client - Basic Usage Example ===\n")
# 1. List existing tables
print("1. Listing tables...")
tables = client.list_tables(app_token)
print(f"Found {len(tables)} tables:")
for table in tables:
print(f" - {table['name']} (ID: {table['table_id']})")
print()
# 2. Create a new table
print("2. Creating a new table...")
table_id = client.create_table(
app_token=app_token,
table_name="Customer Data",
default_view_name="All Customers"
)
print(f"Created table with ID: {table_id}\n")
# 3. Add fields to the table
print("3. Adding fields to the table...")
# Text field for customer name
name_field_id = client.create_field(
app_token=app_token,
table_id=table_id,
field_name="Customer Name",
field_type=1 # Text
)
print(f"Created 'Customer Name' field: {name_field_id}")
# Email field
email_field_id = client.create_field(
app_token=app_token,
table_id=table_id,
field_name="Email",
field_type=15 # URL/Email
)
print(f"Created 'Email' field: {email_field_id}")
# Number field for revenue
revenue_field_id = client.create_field(
app_token=app_token,
table_id=table_id,
field_name="Revenue",
field_type=2 # Number
)
print(f"Created 'Revenue' field: {revenue_field_id}")
# Single select field for status
status_field_id = client.create_field(
app_token=app_token,
table_id=table_id,
field_name="Status",
field_type=3, # Single Select
property_dict={
"options": [
{"name": "Active"},
{"name": "Inactive"},
{"name": "Pending"}
]
}
)
print(f"Created 'Status' field: {status_field_id}\n")
# 4. Create records
print("4. Creating records...")
# Single record
record_id = client.create_record(
app_token=app_token,
table_id=table_id,
fields={
"Customer Name": "Acme Corporation",
"Email": "contact@acme.com",
"Revenue": 150000,
"Status": "Active"
}
)
print(f"Created single record: {record_id}")
# Batch create multiple records
batch_records = [
{
"Customer Name": "TechStart Inc",
"Email": "info@techstart.com",
"Revenue": 75000,
"Status": "Active"
},
{
"Customer Name": "Global Solutions",
"Email": "sales@globalsol.com",
"Revenue": 200000,
"Status": "Active"
},
{
"Customer Name": "Local Business",
"Email": "hello@localbiz.com",
"Revenue": 30000,
"Status": "Pending"
}
]
record_ids = client.batch_create_records(
app_token=app_token,
table_id=table_id,
records=batch_records
)
print(f"Batch created {len(record_ids)} records\n")
# 5. List records
print("5. Listing all records...")
all_records = client.get_all_records(
app_token=app_token,
table_id=table_id
)
print(f"Total records: {len(all_records)}")
for record in all_records:
print(f" - {record['fields'].get('Customer Name')}: "
f"${record['fields'].get('Revenue', 0):,.0f} "
f"({record['fields'].get('Status')})")
print()
# 6. Update a record
print("6. Updating a record...")
client.update_record(
app_token=app_token,
table_id=table_id,
record_id=record_id,
fields={
"Revenue": 175000,
"Status": "Active"
}
)
print(f"Updated record {record_id}\n")
# 7. Batch update records
print("7. Batch updating records...")
update_data = [
{
"record_id": record_ids[0],
"fields": {"Revenue": 80000}
},
{
"record_id": record_ids[1],
"fields": {"Revenue": 225000}
}
]
updated_ids = client.batch_update_records(
app_token=app_token,
table_id=table_id,
records=update_data
)
print(f"Batch updated {len(updated_ids)} records\n")
# 8. List records with specific fields
print("8. Listing records with specific fields...")
result = client.list_records(
app_token=app_token,
table_id=table_id,
field_names=["Customer Name", "Revenue"],
page_size=10
)
print(f"Retrieved {len(result['items'])} records:")
for record in result['items']:
print(f" - {record['fields'].get('Customer Name')}: "
f"${record['fields'].get('Revenue', 0):,.0f}")
print()
# 9. Delete a record
print("9. Deleting a record...")
client.delete_record(
app_token=app_token,
table_id=table_id,
record_id=record_ids[-1]
)
print(f"Deleted record {record_ids[-1]}\n")
# 10. Final count
print("10. Final record count...")
final_records = client.get_all_records(
app_token=app_token,
table_id=table_id
)
print(f"Total records after deletion: {len(final_records)}\n")
print("=== Example completed successfully! ===")
print(f"\nYour new table ID: {table_id}")
print("You can now create dashboards that visualize this data!")
if __name__ == '__main__':
main()