Skip to content

Commit

Permalink
Update samples for Python 3.x compatibility (#1479)
Browse files Browse the repository at this point in the history
* Replace obsolete env package with os.environ
* Python 2.x to 3.x updates
* Fix some comments
* Remove workbook data acceleration; feature was removed in 2022
* Remove switch_site() example which is confusing in this context of demonstrating login
  • Loading branch information
bcantoni authored Oct 22, 2024
1 parent 607fa8b commit 60dfd4d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 137 deletions.
12 changes: 3 additions & 9 deletions samples/extracts.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
####
# This script demonstrates how to use the Tableau Server Client
# to interact with workbooks. It explores the different
# functions that the Server API supports on workbooks.
#
# With no flags set, this sample will query all workbooks,
# pick one workbook and populate its connections/views, and update
# the workbook. Adding flags will demonstrate the specific feature
# on top of the general operations.
####
# This script demonstrates how to use the Tableau Server Client to interact with extracts.
# It explores the different functions that the REST API supports on extracts.
#####

import argparse
import logging
Expand Down
21 changes: 12 additions & 9 deletions samples/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
import argparse
import getpass
import logging
import os

import tableauserverclient as TSC
import env


def get_env(key):
if key in os.environ:
return os.environ[key]
return None


# If a sample has additional arguments, then it should copy this code and insert them after the call to
Expand All @@ -20,13 +26,13 @@ def set_up_and_log_in():
sample_define_common_options(parser)
args = parser.parse_args()
if not args.server:
args.server = env.server
args.server = get_env("SERVER")
if not args.site:
args.site = env.site
args.site = get_env("SITE")
if not args.token_name:
args.token_name = env.token_name
args.token_name = get_env("TOKEN_NAME")
if not args.token_value:
args.token_value = env.token_value
args.token_value = get_env("TOKEN_VALUE")
args.logging_level = "debug"

server = sample_connect_to_server(args)
Expand Down Expand Up @@ -79,10 +85,7 @@ def sample_connect_to_server(args):
# Make sure we use an updated version of the rest apis, and pass in our cert handling choice
server = TSC.Server(args.server, use_server_version=True, http_options={"verify": check_ssl_certificate})
server.auth.sign_in(tableau_auth)
server.version = "2.6"
new_site: TSC.SiteItem = TSC.SiteItem("cdnear", content_url=env.site)
server.auth.switch_site(new_site)
print("Logged in successfully")
server.version = "3.19"

return server

Expand Down
23 changes: 15 additions & 8 deletions samples/publish_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@
import argparse
import logging

import os
import tableauserverclient as TSC

import env
import tableauserverclient.datetime_helpers


def get_env(key):
if key in os.environ:
return os.environ[key]
return None


def main():
parser = argparse.ArgumentParser(description="Publish a datasource to server.")
# Common options; please keep those in sync across all samples
Expand All @@ -52,13 +57,13 @@ def main():

args = parser.parse_args()
if not args.server:
args.server = env.server
args.server = get_env("SERVER")
if not args.site:
args.site = env.site
args.site = get_env("SITE")
if not args.token_name:
args.token_name = env.token_name
args.token_name = get_env("TOKEN_NAME")
if not args.token_value:
args.token_value = env.token_value
args.token_value = get_env("TOKEN_VALUE")
args.logging = "debug"
args.file = "C:/dev/tab-samples/5M.tdsx"
args.async_ = True
Expand Down Expand Up @@ -118,8 +123,10 @@ def main():
new_datasource, args.file, publish_mode, connection_credentials=new_conn_creds
)
print(
"{}Datasource published. Datasource ID: {}".format(
new_datasource.id, tableauserverclient.datetime_helpers.timestamp()
(
"{}Datasource published. Datasource ID: {}".format(
new_datasource.id, tableauserverclient.datetime_helpers.timestamp()
)
)
)
print("\t\tClosing connection")
Expand Down
2 changes: 1 addition & 1 deletion samples/set_refresh_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def usage(args):

def make_filter(**kwargs):
options = TSC.RequestOptions()
for item, value in kwargs.items():
for item, value in list(kwargs.items()):
name = getattr(TSC.RequestOptions.Field, item)
options.filter.add(TSC.Filter(name, TSC.RequestOptions.Operator.Equals, value))
return options
Expand Down
2 changes: 1 addition & 1 deletion samples/update_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def main():
update_function = endpoint.update_connection
resource = endpoint.get_by_id(args.resource_id)
endpoint.populate_connections(resource)
connections = list(filter(lambda x: x.id == args.connection_id, resource.connections))
connections = list([x for x in resource.connections if x.id == args.connection_id])
assert len(connections) == 1
connection = connections[0]
connection.username = args.datasource_username
Expand Down
109 changes: 0 additions & 109 deletions samples/update_workbook_data_acceleration.py

This file was deleted.

0 comments on commit 60dfd4d

Please sign in to comment.