Last active
May 6, 2020 22:06
-
-
Save divinorum-webb/450b8b81c7544629fa71ff682b1522e0 to your computer and use it in GitHub Desktop.
Template code for building a Tableau .hyper data extract from a Pandas DataFrame.
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
# this code snippet will not run by itself; this is a piece of a larger workflow. | |
# a Pandas DataFrame named 'combined_df' is the source of data in this example | |
PATH_TO_HYPER = 'workbooks_and_owners.hyper' | |
# Step 1: Start a new private local Hyper instance | |
with HyperProcess(Telemetry.SEND_USAGE_DATA_TO_TABLEAU, 'myapp' ) as hyper: | |
# Step 2: Create the the .hyper file, replace it if it already exists | |
with Connection(endpoint=hyper.endpoint, | |
create_mode=CreateMode.CREATE_AND_REPLACE, | |
database=PATH_TO_HYPER) as connection: | |
# Step 3: Create the schema | |
connection.catalog.create_schema('Extract') | |
# Step 4: Create the table definition | |
schema = TableDefinition(table_name=TableName('Extract','Extract'), | |
columns=[ | |
TableDefinition.Column('workbook_name', SqlType.text()), | |
TableDefinition.Column('workbook_id', SqlType.text()), | |
TableDefinition.Column('workbook_project', SqlType.text()), | |
TableDefinition.Column('view_type', SqlType.text()), | |
TableDefinition.Column('view_name', SqlType.text()), | |
TableDefinition.Column('view_id', SqlType.text()), | |
TableDefinition.Column('upstr_table_name', SqlType.text()), | |
TableDefinition.Column('upstr_table_id', SqlType.text()), | |
TableDefinition.Column('upstr_db_name', SqlType.text()), | |
TableDefinition.Column('upstr_db_type', SqlType.text()), | |
TableDefinition.Column('upstr_db_id', SqlType.text()), | |
TableDefinition.Column('upstr_db_isEmbedded', SqlType.bool()), | |
TableDefinition.Column('database_hostname', SqlType.text()), | |
TableDefinition.Column('database_port', SqlType.text()), | |
TableDefinition.Column('database_id', SqlType.text()), | |
TableDefinition.Column('upstr_ds_name', SqlType.text()), | |
TableDefinition.Column('upstr_ds_id', SqlType.text()), | |
TableDefinition.Column('upstr_ds_project', SqlType.text()), | |
TableDefinition.Column('emb_ds_name', SqlType.text()), | |
TableDefinition.Column('summary_date', SqlType.text()), | |
TableDefinition.Column('site_name', SqlType.text()), | |
TableDefinition.Column('last_interaction_date', SqlType.text()), | |
TableDefinition.Column('num_views', SqlType.double()) | |
]) | |
# Step 5: Create the table in the connection catalog | |
connection.catalog.create_table(schema) | |
with Inserter(connection, schema) as inserter: | |
for index, row in combined_df.iterrows(): | |
inserter.add_row(row) | |
inserter.execute() | |
print("The connection to the Hyper file is closed.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment