-
Notifications
You must be signed in to change notification settings - Fork 448
Cleaning up samples #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.