Skip to main content

Pagination

Paginate your data batches


Many API calls are paginated. It is an helpful feature when working with our API, especially if you have a huge number of results to display.

You can provide page and per_page params to the API request, and your results will be split into pages.

The page params indicates the page number⁠—defaults to 1. The per_page params indicates the number of results that will be contained in each page⁠—defaults to 128. You can get up to 1,000 results per page.

In the following example, we are getting all inputs and specifying to start at page 2 and retrieve 20 results per page.

info

The initialization code used in the following example is outlined in detail on the client installation page.

################################################################
# In this section, we set the user authentication and app ID.
# Change these strings to run your own example.
#################################################################

USER_ID = 'YOUR_USER_ID_HERE'
# Your PAT (Personal Access Token) can be found in the Account's Security section
PAT = 'YOUR_PAT_HERE'
APP_ID = 'YOUR_APP_ID_HERE'

##########################################################################
# YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE TO RUN THIS EXAMPLE
##########################################################################

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2

channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)

metadata = (('authorization', 'Key ' + PAT),)

userDataObject = resources_pb2.UserAppIDSet(user_id=USER_ID, app_id=APP_ID) # The userDataObject is required when using a PAT

list_inputs_response = stub.ListInputs(
service_pb2.ListInputsRequest(
user_app_id=userDataObject,
page=2,
per_page=20
),
metadata=metadata
)

if list_inputs_response.status.code != status_code_pb2.SUCCESS:
print(list_inputs_response.status)
raise Exception("List inputs failed, status: " + list_inputs_response.status.description)

for input_object in list_inputs_response.inputs:
print(input_object)