10000 initial commit of server api python · sotnich/server-client-python@1084907 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1084907

Browse files
committed
initial commit of server api python
1 parent 4545175 commit 1084907

File tree

97 files changed

+4581
-408
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+4581
-408
lines changed

.gitignore

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Byte-compiled / optimized / DLL files
22
__pycache__/
33
*.py[cod]
4+
*$py.class
45

56
# C extensions
67
*.so
@@ -13,6 +14,7 @@ develop-eggs/
1314
dist/
1415
downloads/
1516
eggs/
17+
.eggs/
1618
lib/
1719
lib64/
1820
parts/
@@ -36,19 +38,104 @@ pip-delete-this-directory.txt
3638
htmlcov/
3739
.tox/
3840
.coverage
41+
.coverage.*
3942
.cache
4043
nosetests.xml
4144
coverage.xml
45+
*,cover
46+
.hypothesis/
4247

4348
# Translations
4449
*.mo
4550
*.pot
4651

4752
# Django stuff:
4853
*.log
54+
local_settings.py
55+
56+
# Flask stuff:
57+
instance/
58+
.webassets-cache
59+
60+
# Scrapy stuff:
61+
.scrapy
4962

5063
# Sphinx documentation
5164
docs/_build/
5265

5366
# PyBuilder
5467
target/
68+
69+
# IPython Notebook
70+
.ipynb_checkpoints
71+
72+
# pyenv
73+
.python-version
74+
75+
# celery beat schedule file
76+
celerybeat-schedule
77+
78+
# dotenv
79+
.env
80+
81+
# virtualenv
82+
venv/
83+
ENV/
84+
85+
# Spyder project settings
86+
.spyderproject
87+
88+
# Rope project settings
89+
.ropeproject
90+
91+
92+
93+
# macOS.gitignore from https://github.com/github/gitignore
94+
*.DS_Store
95+
.AppleDouble
96+
.LSOverride
97+
98+
# Icon must end with two \r
99+
Icon
100+
101+
102+
# Thumbnails
103+
._*
104+
105+
# Files that might appear in the root of a volume
106+
.DocumentRevisions-V100
107+
.fseventsd
108+
.Spotlight-V100
109+
.TemporaryItems
110+
.Trashes
111+
.VolumeIcon.icns
112+
.com.apple.timemachine.donotpresent
113+
114+
# Directories potentially created on remote AFP share
115+
.AppleDB
116+
.AppleDesktop
117+
Network Trash Folder
118+
Temporary Items
119+
.apdisk
120+
121+
122+
123+
# Windows.gitignore from https://github.com/github/gitignore
124+
# Windows image file caches
125+
Thumbs.db
126+
ehthumbs.db
127+
128+
# Folder config file
129+
Desktop.ini
130+
131+
# Recycle Bin used on file shares
132+
$RECYCLE.BIN/
133+
134+
# Windows Installer files
135+
*.cab
136+
*.msi
137+
*.msm
138+
*.msp
139+
140+
# Windows shortcuts
141+
*.lnk

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Tableau
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# server-api-python
2+
This repo contains Python source and sample files for the Tableau Server API.
3+
4+
This repository is currently NOT ready to be made public. Please leave this
5+
as a private repo for now.
6+
7+
Server API
8+
---------------
9+
###Getting Started
10+
To use this SDK, you must have Python installed. You can use either 2.7.X or 3.3 and later.

samples/explore_datasource.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
####
2+
# This script demonstrates how to use the Tableau Server API
3+
# to interact with datasources. It explores the different
4+
# functions that the Server API supports on datasources.
5+
#
6+
# With no flags set, this sample will query all datasources,
7+
# pick one datasource and populate its connections, and update
8+
# the datasource. Adding flags will demonstrate the specific feature
9+
# on top of the general operations.
10+
####
11+
12+
13+
import tableauserverapi as TSA
14+
import os.path
15+
import argparse
16+
import logging
17+
18+
parser = argparse.ArgumentParser(description='Explore datasource functions supported by the Server API.')
19+
parser.add_argument('server', help='server address')
20+
parser.add_argument('username', help='username to sign into server')
21+
parser.add_argument('password', help='password to sign into server')
22+
parser.add_argument('-info', action='store_true', help='set this flag to show logging information on INFO level')
23+
parser.add_argument('-debug', action='store_true', help='set this flag to show logging information on DEBUG level')
24+
parser.add_argument('-publish', '-p', metavar='FILEPATH', help='path to datasource to publish')
25+
parser.add_argument('-download', '-d', metavar='FILEPATH', help='path to save downloaded datasource')
26+
args = parser.parse_args()
27+
28+
if args.debug:
29+
logging.basicConfig(level=logging.DEBUG)
30+
elif args.info:
31+
logging.basicConfig(level=logging.INFO)
32+
33+
##### SIGN IN #####
34+
tableau_auth = TSA.TableauAuth(args.username, args.password)
35+
server = TSA.Server(args.server)
36+
with server.auth.sign_in(tableau_auth):
37+
38+
# Query projects for use when demonstrating publishing and updating
39+
pagination_item, all_projects = server.projects.get()
40+
default_project = next((project for project in all_projects if project.is_default()), None)
41+
42+
# Publish datasource if publish flag is set (-publish, -p)
43+
if args.publish:
44+
if default_project is not None:
45+
new_datasource = TSA.DatasourceItem(default_project.id)
46+
new_datasource = server.datasources.publish(new_datasource, args.publish, server.PublishMode.Overwrite)
47+
print("Datasource published. ID: {}".format(new_datasource.id))
48+
else:
49+
print("Publish failed. Could not find the default project.")
50+
51+
# Gets all datasource items
52+
pagination_item, all_datasources = server.datasources.get()
53+
print("\nThere are {} datasources on site: ".format(pagination_item.total_available))
54+
print([datasource.name for datasource in all_datasources])
55+
56+
if all_datasources:
57+
# Pick one datasource from the list
58+
sample_datasource = all_datasources[0]
59+
60+
# Populate connections
61+
server.datasources.populate_connections(sample_datasource)
62+
print("\nConnections for datasource: ")
63+
print(["{0}({1})".format(connection.id, connection.datasource_name)
64+
for connection in sample_datasource.connections])
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+

samples/explore_workbook.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
####
2+
# This script demonstrates how to use the Tableau Server API
3+
# to interact with workbooks. It explores the different
4+
# functions that the Server API supports on workbooks.
5+
#
6+
# With no flags set, this sample will query all workbooks,
7+
# pick one workbook and populate its connections/views, and update
8+
# the workbook. Adding flags will demonstrate the specific feature
9+
# on top of the general operations.
10+
####
11+
12+
import tableauserverapi as TSA
13+
import os.path
14+
import copy
15+
import argparse
16+
import logging
17+
18+
parser = argparse.ArgumentParser(description='Explore workbook functions supported by the Server API.')
19+
parser.add_argument('server', help='server address')
20+
parser.add_argument('username', help='username to sign into server')
21+
parser.add_argument('password', help='password to sign into server')
22+
parser.add_argument('-info', action='store_true', help='set this flag to show logging information on INFO level')
23+
parser.add_argument('-debug', action='store_true', help='set this flag to show logging information on DEBUG level')
24+
parser.add_argument('-publish', '-p', metavar='FILEPATH', help='path to workbook to publish')
25+
parser.add_argument('-download', '-d', metavar='FILEPATH', help='path to save downloaded workbook')
26+
parser.add_argument('-preview-image', '-i', metavar='FILEPATH', help='path to save populated preview image')
27+
args = parser.parse_args()
28+
29+
if args.debug:
30+
logging.basicConfig(level=logging.DEBUG)
31+
elif args.info:
32+
logging.basicConfig(level=logging.INFO)
33+
34+
##### SIGN IN #####
35+
tableau_auth = TSA.TableauAuth(args.username, args.password)
36+
server = TSA.Server(args.server)
37+
with server.auth.sign_in(tableau_auth):
38+
39+
# Publish workbook if publish flag is set (-publish, -p)
40+
if args.publish:
41+
pagination_info, all_projects = server.projects.get()
42+
default_project = next((project for project in all_projects if project.is_default()), None)
43+
44+
if default_project is not None:
45+
new_workbook = TSA.WorkbookItem(default_project.id)
46+
new_workbook = server.workbooks.publish(new_workbook, args.publish, server.PublishMode.Overwrite)
47+
print("Workbook published. ID: {}".format(new_workbook.id))
48+
else:
49+
print('Publish failed. Could not find the default project.')
50+
51+
# Gets all workbook items
52+
pagination_item, all_workbooks = server.workbooks.get()
53+
print("\nThere are {} workbooks on site: ".format(pagination_item.total_available))
54+
print([workbook.name for workbook in all_workbooks])
55+
56+
if all_workbooks:
57+
# Pick one workbook from the list
58+
sample_workbook = all_workbooks[0]
59+
60+
# Populate views
61+
server.workbooks.populate_views(sample_workbook)
62+
print("\nName of views in workbook {}: ".format(sample_workbook.name))
63+
print([view.name for view in sample_workbook.views])
64+
65+
# Populate connections
66+
server.workbooks.populate_connections(sample_workbook)
67+
print("\nConnections for workbook: ")
68+
print(["{0}({1})".format(connection.id, connection.datasource_name)
69+
for connection in sample_workbook.connections])
70+
71+
# Update tags and show_tabs flag
72+
original_tag_set = copy.copy(sample_workbook.tags)
73+
sample_workbook.tags.update('a', 'b', 'c', 'd')
74+
sample_workbook.show_tabs = True
75+
server.workbooks.update(sample_workbook)
76+
print("\nOld tag set: {}".format(original_tag_set))
77+
print("New tag set: {}".format(sample_workbook.tags))
78+
print("Workbook tabbed: {}".format(sample_workbook.show_tabs))
79+
80+
# Delete all tags that were added by setting tags to original
81+
sample_workbook.tags = original_tag_set
82+
server.workbooks.update(sample_workbook)
83+
84+
if args.download:
85+
# Download
86+
path = server.workbooks.download(sample_workbook.id, args.download)
87+
print("\nDownloaded workbook to {}".format(path))
88+
89+
if args.preview_image:
90+
# Populate workbook preview image
91+
server.workbooks.populate_preview_image(sample_workbook)
92+
with open(args.preview_image, 'wb') as f:
93+
f.write(sample_workbook.preview_image)
94+
print("\nDownloaded preview image of workbook to {}".format(os.path.abspath(args.preview_image)))

samples/move_workbook_projects.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
####
2+
# This script demonstrates how to use the Tableau Server API
3+
# to move a workbook from one project to another. It will find
4+
# a workbook that matches a given name and update it to be in
5+
# the desired project.
6+
#
7+
# To run the script, you must have installed Python 2.7.9 or later.
8+
####
9+
10+
import tableauserverapi as TSA
11+
import argparse
12+
13+
# import logging
14+
# logging.basicConfig(level=logging.DEBUG)
15+
16+
parser = argparse.ArgumentParser(description='Move one workbook from the default project to another.')
17+
parser.add_argument('server', help='server address')
18+
parser.add_argument('username', help='username to sign into server')
19+
parser.add_argument('password', help='password to sign into server')
20+
parser.add_argument('workbook_name', help='name of workbook to move')
21+
parser.add_argument('destination_project', help='name of project to move workbook into')
22+
args = parser.parse_args()
23+
24+
# Step 1: Sign in to server
25+
tableau_auth = TSA.TableauAuth(args.username, args.password)
26+
server = TSA.Server(args.server)
27+
with server.auth.sign_in(tableau_auth):
28+
# Step 2: Query workbook to move
29+
req_option = TSA.RequestOptions()
30+
req_option.filter.add(TSA.Filter(TSA.RequestOptions.Field.Name,
31+
TSA.RequestOptions.Operator.Equals, args.workbook_name))
32+
pagination_info, all_workbooks = server.workbooks.get(req_option)
33+
34+
# Step 3: Find destination project
35+
pagination_info, all_projects = server.projects.get()
36+
dest_project = next((project for project in all_projects if project.name == args.destination_project), None)
37+
38+
# Step 4: Update workbook with new project id
39+
if all_workbooks:
40+
print("Old project: {}".format(all_workbooks[0].project_name))
41+
all_workbooks[0].project_id = dest_project.id
42+
target_workbook = server.workbooks.update(all_workbooks[0])
43+
print("New project: {}".format(target_workbook.project_name))
44+
else:
45+
print('No workbook named {} found.'.format(args.workbook_name))

0 commit comments

Comments
 (0)
0