forked from tableau/server-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpagination_sample.py
More file actions
72 lines (58 loc) · 3.44 KB
/
pagination_sample.py
File metadata and controls
72 lines (58 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
####
# This script demonstrates how to use pagination item that is returned as part
# of many of the .get() method calls.
#
# This script will iterate over every workbook that exists on the server using the
# pagination item to fetch additional pages as needed.
#
# While this sample uses workbook, this same technique will work with any of the .get() methods that return
# a pagination item
####
import argparse
import logging
import os.path
import tableauserverclient as TSC
def main():
parser = argparse.ArgumentParser(description='Demonstrate pagination on the list of workbooks on the server.')
# Common options; please keep those in sync across all samples
parser.add_argument('--server', '-s', required=True, help='server address')
parser.add_argument('--site', '-S', help='site name')
parser.add_argument('--token-name', '-n', required=True,
help='name of the personal access token used to sign into the server')
parser.add_argument('--token-value', '-v', required=True,
help='value of the personal access token used to sign into the server')
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error',
help='desired logging level (set to error by default)')
# Options specific to this sample
# This sample has no additional options, yet. If you add some, please add them here
args = parser.parse_args()
# Set logging level based on user input, or error by default
logging_level = getattr(logging, args.logging_level.upper())
logging.basicConfig(level=logging_level)
# Sign in to server
tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site)
server = TSC.Server(args.server, use_server_version=True)
with server.auth.sign_in(tableau_auth):
# Pager returns a generator that yields one item at a time fetching
# from Server only when necessary. Pager takes a server Endpoint as its
# first parameter. It will call 'get' on that endpoint. To get workbooks
# pass `server.workbooks`, to get users pass` server.users`, etc
# You can then loop over the generator to get the objects one at a time
# Here we print the workbook id for each workbook
print("Your server contains the following workbooks:\n")
for wb in TSC.Pager(server.workbooks):
print(wb.name)
# Pager can also be used in list comprehensions or generator expressions
# for compactness and easy filtering. Generator expressions will use less
# memory than list comprehsnsions. Consult the Python laguage documentation for
# best practices on which are best for your use case. Here we loop over the
# Pager and only keep workbooks where the name starts with the letter 'a'
# >>> [wb for wb in TSC.Pager(server.workbooks) if wb.name.startswith('a')] # List Comprehension
# >>> (wb for wb in TSC.Pager(server.workbooks) if wb.name.startswith('a')) # Generator Expression
# Since Pager is a generator it follows the standard conventions and can
# be fed to a list if you really need all the workbooks in memory at once.
# If you need everything, it may be faster to use a larger page size
# >>> request_options = TSC.RequestOptions(pagesize=1000)
# >>> all_workbooks = list(TSC.Pager(server.workbooks, request_options))
if __name__ == '__main__':
main()