Skip to content

Create 19-count-resources.py #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions examples/19-count-resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
This script connects to an eLabFTW instance via the REST API to retrieve and list all available resource categories (also called item types).
For each category, it prints the category ID and title, followed by the number of entries (items) that belong to that category.
"""

import elabapi_python
# use the locally defined client.py module to get the api_client object, fully configured and ready to be used to instantiate api objects
from client import api_client

# Create API instances for fetching categories (ItemsTypes) and items (Entries) for each category
items_api = elabapi_python.ItemsTypesApi(api_client)
items_api2 = elabapi_python.ItemsApi(api_client)

try:
# Fetch the categories (ItemsTypes) from the API
item_types = items_api.read_items_types() # Fetch all categories

# Print the number of categories
print(f"Number of categories: {len(item_types)}")

# Iterate through each category and display the ID and Title
for item_type in item_types:
print(f"ID: {item_type.id}, Title: {item_type.title}")

# Fetch the items (entries) for each category by ID
entries = items_api2.read_items(cat=item_type.id, limit=100) # Get the items for the category
entry_count = len(entries) # Count the number of items

# Display the number of items in the current category
print(f"Number of entries in this category: {entry_count}\n")

# Error handling for API requests
except elabapi_python.rest.ApiException as e:
print(f"Error fetching categories or entries: {e}")