Skip to content

Instantly share code, notes, and snippets.

View divinorum-webb's full-sized avatar
🐍
python

Elliott divinorum-webb

🐍
python
  • Amsterdam
View GitHub Profile
@divinorum-webb
divinorum-webb / tableau-api-lib-combine-postgresql-metadata.py
Last active July 4, 2022 23:46
Template Python code for combining Tableau's PostgreSQL data with data from the Metadata API
from sqlalchemy import create_engine
import pandas as pd
from pandas.io.json import json_normalize
from tableau_api_lib import TableauServerConnection
from tableau_api_lib.utils import flatten_dict_column, flatten_dict_list_column
# using personal access tokens is preferred; otherwise, comment those details out and use username / password
@divinorum-webb
divinorum-webb / tableau-server-postgresql-view-interactions.sql
Last active February 2, 2023 09:26
A query that retrieves the last interaction date and the total view counts for all views from Tableau Server's internal PostgreSQL database
WITH events AS (
SELECT DISTINCT MAX(DATE_TRUNC('day', created_at)) AS last_interaction_date
, hist_target_site_id
, hist_view_id
, historical_event_type_id
FROM historical_events
GROUP BY 2, 3, 4
),
event_types AS (
SELECT DISTINCT type_id
@divinorum-webb
divinorum-webb / tableau-api-lib-flatten-metadata-api-json.py
Created April 27, 2020 21:43
Flattening JSON data returned by Tableau's Metadata API.
import pandas as pd
from pandas.io.json import json_normalize
from tableau_api_lib import TableauServerConnection
from tableau_api_lib.utils import flatten_dict_column, flatten_dict_list_column
# using personal access tokens is preferred; otherwise, comment those details out and use username / password
tableau_server_config = {
'my_env': {
@divinorum-webb
divinorum-webb / tableau-api-lib-query-workbook-metadata.py
Created April 24, 2020 23:36
Python code for querying Tableau visuals and their underlying database assets to build impact analysis reports (milestone1)
import pandas as pd
from pandas.io.json import json_normalize
from tableau_api_lib import TableauServerConnection
from tableau_api_lib.utils import flatten_dict_column, flatten_dict_list_column
# using personal access tokens is preferred; otherwise, comment those details out and use username / password
tableau_server_config = {
'my_env': {
@divinorum-webb
divinorum-webb / tableau-server-metadata-api-workbook-db-assets
Last active November 16, 2021 23:37
Tableau Server Metadata API GraphQL query for visuals and their relevant database assets.
# workbooks
{
workbooks {
workbook_name: name
workbook_id: luid
workbook_project: projectName
views {
view_type: __typename
view_name: name
import pandas as pd
from sqlalchemy import create_engine # make sure you also have the psycopg2 package installed via pip
pg_config = {
'host': '<YOUR_HOST_NAME>',
'port': '8060',
'database': 'workgroup',
'username': 'readonly',
'password': '<YOUR_PASSWORD>'
WITH events AS (
SELECT DISTINCT MAX(DATE_TRUNC('day', created_at)) AS last_interaction_date
, hist_target_site_id
, hist_workbook_id
, hist_datasource_id
, hist_flow_id
, historical_event_type_id
FROM historical_events
GROUP BY 2, 3, 4, 5, 6
),
@divinorum-webb
divinorum-webb / tableau-api-lib-create-webhooks.py
Created April 18, 2020 03:53
Creating Tableau Server webhooks using tableau-api-lib.
from tableau_api_lib import TableauServerConnection
from tableau_api_lib.utils.querying import get_webhooks_dataframe
tableau_server_config = {
'my_env': {
'server': 'https://YourTableauServer.com',
'api_version': '<YOUR_API_VERSION>',
'username': '<YOUR_USERNAME>',
@divinorum-webb
divinorum-webb / tableau-api-lib-trigger-extract-refreshes.py
Created April 15, 2020 23:25
Template code for triggering Tableau Server extract refreshes using Python and tableau-api-lib.
from tableau_api_lib import TableauServerConnection
from tableau_api_lib.utils.querying import get_workbooks_dataframe, get_datasources_dataframe
TS_CONFIG = {
'my_env': {
'server': 'https://YourTableauServer.com',
'api_version': '<YOUR_API_VERSION>',
'username': '<YOUR_USERNAME>',
'password': '<YOUR_PASSWORD>',
@divinorum-webb
divinorum-webb / tableau-api-lib-publish-workbook.py
Created April 13, 2020 22:58
Template code for publishing workbooks to Tableau Server using tableau-api-lib and Tableau's REST API.
from tableau_api_lib import TableauServerConnection
from tableau_api_lib.utils.querying import get_projects_dataframe
# Set the following constants according to your environment
YOUR_PROJECT_ID = 'ENTER_YOUR_TS_PROJECT_ID'
YOUR_WORKBOOK_FILE_PATH = 'ENTER_YOUR_WORKBOOK_FILE_PATH'
YOUR_WORKBOOK_NAME = 'ENTER_YOUR_DESIRED_WORKBOOK_NAME'
YOUR_TABLEAU_SERVER_ADDRESS = 'ENTER_YOUR_TABLEAU_SERVER_ADDRESS'
YOUR_DB_SERVER_ADDRESS = 'ENTER_YOUR_DB_SERVER_ADDRESS'