Skip to content

Commit a1e6d5e

Browse files
Eln uzun patch 2 (#29)
* Create 19-count-resources.py I actually made it run wit elabapi_python, which was not easy for me but in the end it worked out. I tested it in my testing team and with a productive used team on our instance * Update 19-count-resources.py I hope I have correctly understood your comments =) * remove whitespaces --------- Co-authored-by: Hüseyin Uzun <109156218+ELN-uzun@users.noreply.github.com>
1 parent 3bb9c1c commit a1e6d5e

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

examples/19-count-resources.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
This script connects to an eLabFTW instance via the REST API to retrieve and list all available resource categories (also called item types).
3+
For each category, it prints the category ID and title, followed by the number of entries (items) that belong to that category.
4+
"""
5+
6+
import elabapi_python
7+
# use the locally defined client.py module to get the api_client object, fully configured and ready to be used to instantiate api objects
8+
from client import api_client
9+
10+
# Create API instances for fetching categories (ItemsTypes) and items (Entries) for each category
11+
items_api = elabapi_python.ItemsTypesApi(api_client)
12+
items_api2 = elabapi_python.ItemsApi(api_client)
13+
14+
try:
15+
# Fetch the categories (ItemsTypes) from the API
16+
item_types = items_api.read_items_types() # Fetch all categories
17+
18+
# Print the number of categories
19+
print(f"Number of categories: {len(item_types)}")
20+
21+
# Iterate through each category and display the ID and Title
22+
for item_type in item_types:
23+
print(f"ID: {item_type.id}, Title: {item_type.title}")
24+
25+
# Fetch the items (entries) for each category by ID
26+
entries = items_api2.read_items(cat=item_type.id, limit=100) # Get the items for the category
27+
entry_count = len(entries) # Count the number of items
28+
29+
# Display the number of items in the current category
30+
print(f"Number of entries in this category: {entry_count}\n")
31+
32+
# Error handling for API requests
33+
except elabapi_python.rest.ApiException as e:
34+
print(f"Error fetching categories or entries: {e}")

0 commit comments

Comments
 (0)