Tableau Hyper API for Python

0.0.24457 · active · verified Sat Apr 11

The Tableau Hyper API for Python allows developers to programmatically create, read, and update .hyper files, which are Tableau's high-performance data engine files. It provides direct interaction with the Hyper engine for efficient data management and integration with Tableau products. The current version is 0.0.24457, and it is actively maintained with frequent updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a HyperProcess, establish a connection to a .hyper file, define a table schema, create the table, and insert data into it. It emphasizes proper resource management using `with` statements to ensure connections and the Hyper process are correctly closed.

import os
from tableauhyperapi import HyperProcess, Connection, TableDefinition, TableName, SqlType, Inserter

# Define the path for the new Hyper file
hyper_file_path = 'my_first_hyper_file.hyper'

# Remove the file if it already exists to start fresh
if os.path.exists(hyper_file_path):
    os.remove(hyper_file_path)

# 1. Start the HyperProcess
with HyperProcess(telemetry_opt_out=True) as hyper:
    print(f"The HyperProcess has started on port {hyper.endpoint.port}.")

    # 2. Connect to the Hyper file (creates it if it doesn't exist)
    with Connection(endpoint=hyper.endpoint, database=hyper_file_path, create_mode=Connection.CreateMode.CREATE_AND_REPLACE) as connection:
        print("The connection to the Hyper file is open.")

        # 3. Define the table schema
        table_name = TableName('public', 'my_data_table')
        table_definition = TableDefinition(
            table_name,
            [
                TableDefinition.Column('id', SqlType.int()),
                TableDefinition.Column('name', SqlType.text()),
                TableDefinition.Column('value', SqlType.double()),
            ]
        )

        # 4. Create the table
        connection.catalog.create_table(table_definition)
        print(f"Table '{table_name}' created.")

        # 5. Insert data
        with Inserter(connection, table_definition) as inserter:
            inserter.add_row([1, 'Alpha', 10.5])
            inserter.add_row([2, 'Beta', 20.3])
            inserter.add_row([3, 'Gamma', 30.1])
            inserter.execute()
        print(f"{inserter.number_of_inserted_rows} rows inserted.")

    print("The connection to the Hyper file is closed.")
print("The HyperProcess is shut down.")

print(f"Hyper file '{hyper_file_path}' created successfully.")

view raw JSON →