Created
March 24, 2020 21:07
-
-
Save divinorum-webb/aa64f5d727c7002514323d49573fe4ce to your computer and use it in GitHub Desktop.
This file contains 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
import io | |
from tableau_api_lib import TableauServerConnection | |
from tableau_api_lib.utils.querying import get_views_dataframe, get_view_data_dataframe | |
VIEW_ID = '<YOUR_VIEW_ID>' | |
CUSTOM_PARAMS = { | |
'region': 'vf_Region=East,West', | |
'ship_mode': 'vf_Ship Mode=First Class,Standard Class', | |
'param': 'vf_Superstore Sales | Column Parameter=Segment' | |
} | |
TABLEAU_SERVER_CONFIG = { | |
'my_env': { | |
'server': 'https://<YOUR_SERVER>.com', | |
'api_version': '<YOUR_API_VERSION>', | |
'username': '<YOUR_USERNAME>', | |
'password': '<YOUR_PASSWORD>', | |
# comment out username / password and uncomment the lines below to use personal access token authentication | |
# 'personal_access_token_name': '<YOUR_PERSONAL_ACCESS_TOKEN_NAME>', | |
# 'personal_access_token_secret': '<YOUR_PERSONAL_ACCESS_TOKEN_SECRET>', | |
'site_name': '<YOUR_SITE_NAME>', | |
'site_url': '<YOUR_SITE_URL>' | |
} | |
} | |
def replace_special_characters(text): | |
text = text.replace(' ', '%20') | |
text = text.replace('|', '%7C') | |
return text | |
def get_encoded_params(param_dict): | |
if isinstance(param_dict, dict): | |
encoded_dict = {} | |
for key in param_dict.keys(): | |
encoded_dict[key] = replace_special_characters(str(param_dict[key])) | |
return encoded_dict | |
else: | |
raise Exception(f"This function expects a dict but received a {type(param_dict)} instead.") | |
conn = TableauServerConnection(config_json=TABLEAU_SERVER_CONFIG, env='my_env') | |
conn.sign_in() | |
views_df = get_views_dataframe(conn) | |
print("views_df top rows:\n", views_df.head()) | |
param_dict = get_encoded_params(CUSTOM_PARAMS) | |
view_data_df = get_view_data_dataframe(conn_a, view_id=VIEW_ID, parameter_dict=param_dict) | |
print("view_data_df top rows:\n", view_data_df.head()) | |
modified_view_data_df = view_data_df.copy() | |
currency_columns = ['Sales', 'Profit'] | |
modified_view_data_df[currency_columns] = modified_view_data_df[currency_columns].\ | |
apply(lambda x: x.str.replace('$', '')).\ | |
apply(lambda x: x.str.replace(',', '')).\ | |
astype(float) | |
modified_view_data_df['profit_ratio'] = modified_view_data_df['Profit'] / modified_view_data_df['Sales'] | |
print("verify profit ratio exists:\n", modified_view_data_df.head()) | |
new_column_names = ['Sub-Category', 'Segment', 'Category', 'Discount', 'Profit', 'Quantity', 'Sales', 'Profit Ratio'] | |
modified_view_data_df.columns = new_column_names | |
print("verify column names have changed:\n", modified_view_data_df.head()) | |
modified_view_data_df.to_csv('your_view_data.csv', index=False) | |
conn.sign_out() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Elliot,
view_data_df = get_view_data_dataframe(conn_a, view_id=VIEW_ID, parameter_dict=param_dict)
while running this block-
I am getting error - EmptyDataError: No columns to parse from file.
I have added 2 filters and their name is same as in Tableau and I've also followed the encoding standards accordingly.
Any idea what might be the issue?