-
Notifications
You must be signed in to change notification settings - Fork 448
Export sample #263
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
Export sample #263
Changes from all commits
Commits
Show all changes
4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import argparse | ||
| import getpass | ||
| import logging | ||
|
|
||
| import tableauserverclient as TSC | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser(description='Export a view as an image, pdf, or csv') | ||
| 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('--site', '-S', default=None) | ||
| parser.add_argument('-p', default=None) | ||
|
|
||
| parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error', | ||
| help='desired logging level (set to error by default)') | ||
| group = parser.add_mutually_exclusive_group(required=True) | ||
| group.add_argument('--pdf', dest='type', action='store_const', const=('populate_pdf', 'PDFRequestOptions', 'pdf', | ||
| 'pdf')) | ||
| group.add_argument('--png', dest='type', action='store_const', const=('populate_image', 'ImageRequestOptions', | ||
| 'image', 'png')) | ||
| group.add_argument('--csv', dest='type', action='store_const', const=('populate_csv', 'CSVRequestOptions', 'csv', | ||
| 'csv')) | ||
|
|
||
| parser.add_argument('--file', '-f', help='filename to store the exported data') | ||
| parser.add_argument('--filter', '-vf', metavar='COLUMN:VALUE', | ||
| help='View filter to apply to the view') | ||
| parser.add_argument('resource_id', help='LUID for the view') | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| if args.p is None: | ||
| password = getpass.getpass("Password: ") | ||
| else: | ||
| password = args.p | ||
|
|
||
| # Set logging level based on user input, or error by default | ||
| logging_level = getattr(logging, args.logging_level.upper()) | ||
| logging.basicConfig(level=logging_level) | ||
|
|
||
| tableau_auth = TSC.TableauAuth(args.username, password, args.site) | ||
| server = TSC.Server(args.server, use_server_version=True) | ||
| with server.auth.sign_in(tableau_auth): | ||
| views = filter(lambda x: x.id == args.resource_id, | ||
| TSC.Pager(server.views.get)) | ||
| view = views.pop() | ||
|
|
||
| # We have a number of different types and functions for each different export type. | ||
| # We encode that information above in the const=(...) parameter to the add_argument function to make | ||
| # the code automatically adapt for the type of export the user is doing. | ||
| # We unroll that information into methods we can call, or objects we can create by using getattr() | ||
| (populate_func_name, option_factory_name, member_name, extension) = args.type | ||
| populate = getattr(server.views, populate_func_name) | ||
| option_factory = getattr(TSC, option_factory_name) | ||
|
|
||
| if args.filter: | ||
| options = option_factory().vf(*args.filter.split(':')) | ||
| else: | ||
| options = None | ||
| if args.file: | ||
| filename = args.file | ||
| else: | ||
| filename = 'out.{}'.format(extension) | ||
|
|
||
| populate(view, options) | ||
| with file(filename, 'wb') as f: | ||
| if member_name == 'csv': | ||
| f.writelines(getattr(view, member_name)) | ||
| else: | ||
| f.write(getattr(view, member_name)) | ||
|
|
||
|
|
||
| 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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Code is fine, and I'm not sure if this works so ignore it if not: But can we filter on ID natively yet?
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 views supports that right now.