This repository was archived by the owner on Nov 2, 2020. It is now read-only.
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
71 lines (53 loc) · 2.96 KB
/
pagination_sample.py
File metadata and controls
71 lines (53 loc) · 2.96 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
####
# 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 getpass
import logging
import os.path
import tableauserverclient as TSC
def main():
parser = argparse.ArgumentParser(description='Return a list of all of the workbooks on your server')
parser.add_argument('--server', '-s', required=True, help='server address')
parser.add_argument('--username', '-u', required=True, help='username to sign into server')
parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error',
help='desired logging level (set to error by default)')
args = parser.parse_args()
password = getpass.getpass("Password: ")
# 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
tableau_auth = TSC.TableauAuth(args.username, password)
server = TSC.Server(args.server)
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()