pythontableau-api

How to upload data to Tableau Server in Python


I need to run a python script and it should upload/update a data file on a tableau server, so the dashboard linked to the datafile on the server will be automatically updated every time I run my python script. I am not exactly sure how to do that, the file I need to send to the tableau server is in a csv format - but I think the server only works with tableau files? Also I am not sure what API to be using for this purpose


Solution

  • CSV to TDSX: https://www.tableautim.com/playlist-video/tableau-tds-and-tdsx-files

    Then, Use the tableau-api-lib tool for Python, https://github.com/divinorum-webb/tableau-api-lib . install with : pip install --upgrade tableau-api-lib, Then create a connection to your Tableau Server:

    from tableau_api_lib import TableauServerConnection
    from tableau_api_lib.utils.querying import get_projects_dataframe
    
    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()
    

    Where to publish:

    projects_df = get_projects_dataframe(conn)
    print(projects_df[['name', 'id']]
    

    Publish:

    response = conn.publish_data_source(
    datasource_file_path='superstore_extract.tdsx',
    datasource_name='superstore_extract',
    project_id=PROJECT_ID_TO_PUBLISH_TO)
    

    Inspect the outcome: print(response.json())

    Nicely described in this article from medium: https://medium.com/snake-charmer-python-and-analytics/how-to-publish-tableau-tdsx-data-source-files-using-python-ab576a9bef4c