Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 50 additions & 42 deletions samples/explore_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,64 @@
# on top of the general operations.
####


import tableauserverclient as TSC
import os.path
import argparse
import getpass
import logging

parser = argparse.ArgumentParser(description='Explore datasource functions supported by the Server API.')
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('--publish', '-p', metavar='FILEPATH', help='path to datasource to publish')
parser.add_argument('--download', '-d', metavar='FILEPATH', help='path to save downloaded datasource')
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()
import tableauserverclient as TSC


def main():

parser = argparse.ArgumentParser(description='Explore datasource functions supported by the Server API.')
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('--publish', '-p', metavar='FILEPATH', help='path to datasource to publish')
parser.add_argument('--download', '-d', metavar='FILEPATH', help='path to save downloaded datasource')
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)

password = getpass.getpass("Password: ")
# SIGN IN
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is just a product of github, but the spaces here are showing different than then ones below. Make sure they are all spaces, not a mix of tabs and spaces.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I noticed that when it posted, weird, I'll make sure they're all spaces.

tableau_auth = TSC.TableauAuth(args.username, password)
server = TSC.Server(args.server)
with server.auth.sign_in(tableau_auth):
# Query projects for use when demonstrating publishing and updating
all_projects, pagination_item = server.projects.get()
default_project = next((project for project in all_projects if project.is_default()), None)

# Set logging level based on user input, or error by default
logging_level = getattr(logging, args.logging_level.upper())
logging.basicConfig(level=logging_level)
# Publish datasource if publish flag is set (-publish, -p)
if args.publish:
if default_project is not None:
new_datasource = TSC.DatasourceItem(default_project.id)
new_datasource = server.datasources.publish(
new_datasource, args.publish, TSC.Server.PublishMode.Overwrite)
print("Datasource published. ID: {}".format(new_datasource.id))
else:
print("Publish failed. Could not find the default project.")

# SIGN IN
tableau_auth = TSC.TableauAuth(args.username, password)
server = TSC.Server(args.server)
with server.auth.sign_in(tableau_auth):
# Query projects for use when demonstrating publishing and updating
all_projects, pagination_item = server.projects.get()
default_project = next((project for project in all_projects if project.is_default()), None)
# Gets all datasource items
all_datasources, pagination_item = server.datasources.get()
print("\nThere are {} datasources on site: ".format(pagination_item.total_available))
print([datasource.name for datasource in all_datasources])

# Publish datasource if publish flag is set (-publish, -p)
if args.publish:
if default_project is not None:
new_datasource = TSC.DatasourceItem(default_project.id)
new_datasource = server.datasources.publish(new_datasource, args.publish, TSC.Server.PublishMode.Overwrite)
print("Datasource published. ID: {}".format(new_datasource.id))
else:
print("Publish failed. Could not find the default project.")
if all_datasources:
# Pick one datasource from the list
sample_datasource = all_datasources[0]

# Gets all datasource items
all_datasources, pagination_item = server.datasources.get()
print("\nThere are {} datasources on site: ".format(pagination_item.total_available))
print([datasource.name for datasource in all_datasources])
# Populate connections
server.datasources.populate_connections(sample_datasource)
print("\nConnections for {}: ".format(sample_datasource.name))
print(["{0}({1})".format(connection.id, connection.datasource_name)
for connection in sample_datasource.connections])

if all_datasources:
# Pick one datasource from the list
sample_datasource = all_datasources[0]

# Populate connections
server.datasources.populate_connections(sample_datasource)
print("\nConnections for {}: ".format(sample_datasource.name))
print(["{0}({1})".format(connection.id, connection.datasource_name)
for connection in sample_datasource.connections])
if __name__ == '__main__':
main()
173 changes: 92 additions & 81 deletions samples/explore_workbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,88 +9,99 @@
# on top of the general operations.
####

import tableauserverclient as TSC
import os.path
import copy
import argparse
import getpass
import logging
import os.path

import tableauserverclient as TSC


def main():

parser = argparse.ArgumentParser(description='Explore workbook functions supported by the Server API.')
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('--publish', '-p', metavar='FILEPATH', help='path to workbook to publish')
parser.add_argument('--download', '-d', metavar='FILEPATH', help='path to save downloaded workbook')
parser.add_argument('--preview-image', '-i', metavar='FILENAME',
help='filename (a .png file) to save the preview image')
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)

overwrite_true = TSC.Server.PublishMode.Overwrite

with server.auth.sign_in(tableau_auth):

# Publish workbook if publish flag is set (-publish, -p)
if args.publish:
all_projects, pagination_item = server.projects.get()
default_project = next((project for project in all_projects if project.is_default()), None)

if default_project is not None:
new_workbook = TSC.WorkbookItem(default_project.id)
new_workbook = server.workbooks.publish(new_workbook, args.publish, overwrite_true)
print("Workbook published. ID: {}".format(new_workbook.id))
else:
print('Publish failed. Could not find the default project.')

# Gets all workbook items
all_workbooks, pagination_item = server.workbooks.get()
print("\nThere are {} workbooks on site: ".format(pagination_item.total_available))
print([workbook.name for workbook in all_workbooks])

if all_workbooks:
# Pick one workbook from the list
sample_workbook = all_workbooks[0]

# Populate views
server.workbooks.populate_views(sample_workbook)
print("\nName of views in {}: ".format(sample_workbook.name))
print([view.name for view in sample_workbook.views])

# Populate connections
server.workbooks.populate_connections(sample_workbook)
print("\nConnections for {}: ".format(sample_workbook.name))
print(["{0}({1})".format(connection.id, connection.datasource_name)
for connection in sample_workbook.connections])

# Update tags and show_tabs flag
original_tag_set = set(sample_workbook.tags)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a simpler way to copy a set and doesn't require copy.copy

sample_workbook.tags.update('a', 'b', 'c', 'd')
sample_workbook.show_tabs = True
server.workbooks.update(sample_workbook)
print("\nOld tag set: {}".format(original_tag_set))
print("New tag set: {}".format(sample_workbook.tags))
print("Workbook tabbed: {}".format(sample_workbook.show_tabs))

# Delete all tags that were added by setting tags to original
sample_workbook.tags = original_tag_set
server.workbooks.update(sample_workbook)

if args.download:
# Download
path = server.workbooks.download(sample_workbook.id, args.download)
print("\nDownloaded workbook to {}".format(path))

if args.preview_image:
# Populate workbook preview image
server.workbooks.populate_preview_image(sample_workbook)
with open(args.preview_image, 'wb') as f:
f.write(sample_workbook.preview_image)
print("\nDownloaded preview image of workbook to {}".format(os.path.abspath(args.preview_image)))


parser = argparse.ArgumentParser(description='Explore workbook functions supported by the Server API.')
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('--publish', '-p', metavar='FILEPATH', help='path to workbook to publish')
parser.add_argument('--download', '-d', metavar='FILEPATH', help='path to save downloaded workbook')
parser.add_argument('--preview-image', '-i', metavar='FILENAME',
help='filename (a .png file) to save the preview image')
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):

# Publish workbook if publish flag is set (-publish, -p)
if args.publish:
all_projects, pagination_item = server.projects.get()
default_project = next((project for project in all_projects if project.is_default()), None)

if default_project is not None:
new_workbook = TSC.WorkbookItem(default_project.id)
new_workbook = server.workbooks.publish(new_workbook, args.publish, TSC.Server.PublishMode.Overwrite)
print("Workbook published. ID: {}".format(new_workbook.id))
else:
print('Publish failed. Could not find the default project.')

# Gets all workbook items
all_workbooks, pagination_item = server.workbooks.get()
print("\nThere are {} workbooks on site: ".format(pagination_item.total_available))
print([workbook.name for workbook in all_workbooks])

if all_workbooks:
# Pick one workbook from the list
sample_workbook = all_workbooks[0]

# Populate views
server.workbooks.populate_views(sample_workbook)
print("\nName of views in {}: ".format(sample_workbook.name))
print([view.name for view in sample_workbook.views])

# Populate connections
server.workbooks.populate_connections(sample_workbook)
print("\nConnections for {}: ".format(sample_workbook.name))
print(["{0}({1})".format(connection.id, connection.datasource_name)
for connection in sample_workbook.connections])

# Update tags and show_tabs flag
original_tag_set = copy.copy(sample_workbook.tags)
sample_workbook.tags.update('a', 'b', 'c', 'd')
sample_workbook.show_tabs = True
server.workbooks.update(sample_workbook)
print("\nOld tag set: {}".format(original_tag_set))
print("New tag set: {}".format(sample_workbook.tags))
print("Workbook tabbed: {}".format(sample_workbook.show_tabs))

# Delete all tags that were added by setting tags to original
sample_workbook.tags = original_tag_set
server.workbooks.update(sample_workbook)

if args.download:
# Download
path = server.workbooks.download(sample_workbook.id, args.download)
print("\nDownloaded workbook to {}".format(path))

if args.preview_image:
# Populate workbook preview image
server.workbooks.populate_preview_image(sample_workbook)
with open(args.preview_image, 'wb') as f:
f.write(sample_workbook.preview_image)
print("\nDownloaded preview image of workbook to {}".format(os.path.abspath(args.preview_image)))
if __name__ == '__main__':
main()
92 changes: 51 additions & 41 deletions samples/move_workbook_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,60 @@
# To run the script, you must have installed Python 2.7.X or 3.3 and later.
####

import tableauserverclient as TSC
import argparse
import getpass
import logging

parser = argparse.ArgumentParser(description='Move one workbook from the default project to another.')
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('--workbook-name', '-w', required=True, help='name of workbook to move')
parser.add_argument('--destination-project', '-d', required=True, help='name of project to move workbook into')
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)

# Step 1: Sign in to server
tableau_auth = TSC.TableauAuth(args.username, password)
server = TSC.Server(args.server)
with server.auth.sign_in(tableau_auth):
# Step 2: Query workbook to move
req_option = TSC.RequestOptions()
req_option.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
TSC.RequestOptions.Operator.Equals, args.workbook_name))
all_workbooks, pagination_item = server.workbooks.get(req_option)

# Step 3: Find destination project
all_projects, pagination_item = server.projects.get()
dest_project = next((project for project in all_projects if project.name == args.destination_project), None)

if dest_project is not None:
# Step 4: Update workbook with new project id
if all_workbooks:
print("Old project: {}".format(all_workbooks[0].project_name))
all_workbooks[0].project_id = dest_project.id
target_workbook = server.workbooks.update(all_workbooks[0])
print("New project: {}".format(target_workbook.project_name))
import tableauserverclient as TSC


def main():

parser = argparse.ArgumentParser(description='Move one workbook from the default project to another.')
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('--workbook-name', '-w', required=True, help='name of workbook to move')
parser.add_argument('--destination-project', '-d', required=True, help='name of project to move workbook into')
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)

# Step 1: Sign in to server
tableau_auth = TSC.TableauAuth(args.username, password)
server = TSC.Server(args.server)

with server.auth.sign_in(tableau_auth):
# Step 2: Query workbook to move
req_option = TSC.RequestOptions()
req_option.filter.add(TSC.Filter(TSC.RequestOptions.Field.Name,
TSC.RequestOptions.Operator.Equals, args.workbook_name))
all_workbooks, pagination_item = server.workbooks.get(req_option)

# Step 3: Find destination project
all_projects, pagination_item = server.projects.get()
dest_project = next((project for project in all_projects if project.name == args.destination_project), None)

if dest_project is not None:
# Step 4: Update workbook with new project id
if all_workbooks:
print("Old project: {}".format(all_workbooks[0].project_name))
all_workbooks[0].project_id = dest_project.id
target_workbook = server.workbooks.update(all_workbooks[0])
print("New project: {}".format(target_workbook.project_name))
else:
error = "No workbook named {} found.".format(args.workbook_name)
raise LookupError(error)
else:
error = "No workbook named {} found.".format(args.workbook_name)
error = "No project named {} found.".format(args.destination_project)
raise LookupError(error)
else:
error = "No project named {} found.".format(args.destination_project)
raise LookupError(error)


if __name__ == '__main__':
main()
Loading