Skip to content

Instantly share code, notes, and snippets.

@divinorum-webb
Last active November 2, 2021 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divinorum-webb/f32a0ddb233b7cc8cf91ad7e8a215dd1 to your computer and use it in GitHub Desktop.
Save divinorum-webb/f32a0ddb233b7cc8cf91ad7e8a215dd1 to your computer and use it in GitHub Desktop.
tableau-api-lib-create-subscription
from tableau_api_lib import TableauServerConnection
from tableau_api_lib.utils.querying import get_schedules_dataframe, get_users_dataframe, get_views_dataframe
SAMPLE_SCHEDULE_ID = <YOUR_SCHEDULE_ID>
SAMPLE_USER_ID = <USER_ID_FOR_USER_SUBSCRIBED>
SAMPLE_CONTENT_ID = <VIEW_OR_WORKBOOK_ID_FOR_SUBSCRIPTION>
SAMPLE_CONTENT_TYPE = <VIEW_OR_WORKBOOK> # this value can 'view' or 'workbook' and is not case sensitive
tableau_server_config = {
'my_env': {
'server': 'https://YourTableauServer.com',
'api_version': '<YOUR_API_VERSION>',
'username': '<YOUR_USERNAME>',
'password': '<YOUR_PASSWORD>',
'site_name': '<YOUR_SITE_NAME>',
'site_url': '<YOUR_SITE_CONTENT_URL>'
}
}
conn = TableauServerConnection(tableau_server_config, env='my_env')
conn.sign_in()
print(help(conn.create_subscription)) # this demos the useful info contained within each tableau-api-lib method's docstring
response = conn.create_subscription(schedule_id=SAMPLE_SCHEDULE_ID,
user_id=SAMPLE_USER_ID,
content_type='view',
content_id=SAMPLE_CONTENT_ID,
subscription_subject='Sample subscription subject',
attach_pdf_flag=True,
attach_image_flag=True)
print(response.json())
conn.sign_out()
@SriRayala
Copy link

How can I use this code to move 700 + subscriptions from one server to another? I have the list of subscriptions in a CSV.

@divinorum-webb
Copy link
Author

Cloning subscriptions from one site to another can be messy, and there is not an out-of-the box solution.

The code above demonstrates how to create a subscription using the REST API, and depending on what your CSV contains you may need to also get the following:

  1. user IDs for all users on the new server who are getting a subscription
  2. view IDs for the views you are subscribing your users to (assuming the subscriptions are to views on Tableau Server)

If you know your way around the Pandas library, then you can combine the data from your CSV (which I assume is a dump of subscriptions from your origin server) with data from the new server to provide the relevant user IDs and view IDs. You can use the Tableau Server REST API to pull the user details and the view details from the new server, and a library like tableau-api-lib can help you pull those directly into Pandas DataFrames. When all your data is in Pandas DataFrames, then if you know a bit of Python you can combine all the info you need. Your users and views will not have the same IDs on both servers, so you will need to figure out how you are mapping your users (username? email?) and how you're mapping your views. If they aren't named the same, things will be messy for you.

Once you have the info from your subscriptions CSV combined with the info from your new server (user IDs, view IDs) then you can loop through each line of the combined Pandas DataFrame and reference the code above on how to create a subscription for each user / view in each row of that DataFrame.

Hope that helps.

@SriRayala
Copy link

Yes, This helps a lot. I'm planning to get the user subscription list using python code and then load it into CSV. Once the data is verified, then the plan is to use this logic to loop through those records in CSV to create subscriptions. There is no proper documentation regarding how to create subscriptions without using a manual process. The actual plan is to use Alteryx to do this, but there is very limited documentation to do the admin work using it. So came across folks using Python to do the tableau admin work.

Here is a link that I found which serves my purpose on Alteryx.
https://community.tableau.com/s/question/0D54T00000C5Oon/making-post-api-calls-to-a-tableau-server-using-alteryx

Let me know if you would like to add anything that I can do to make the above solution easier.

Thanks

@LaxmanThakur
Copy link

I am creating automatic subscribtion for tableu server using this script but getting issue while running script
IMG_20211102_214051__01

@LaxmanThakur
Copy link

@divinorum-webb can you please help me out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment