Python Microsoft Fabric - Lakehouses

Fabric lakehouses - creating them, loading data into OneLake files and listing tables.

A lakehouse is Fabric's central data store - files and Delta tables in one item, backed by OneLake. Lakehouses are items inside a workspace, so the item API is how you create and manage them, while the OneLake data plane is how you move data in and out. You create a Fabric connection in the Dashboard and both are available to your services.

Listing lakehouses

conn.list_items with the Lakehouse type filter returns the lakehouses of a workspace.

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class ListLakehouses(Service):

    input = 'workspace_id'

    def handle(self):

        # Get the connection by its Dashboard name
        conn = self.microsoft.fabric['My Fabric']

        # List only the lakehouse items
        response = conn.list_items(self.request.input.workspace_id, 'Lakehouse')

        lakehouses = []
        for item in response['value']:
            lakehouses.append({
                'id': item['id'],
                'name': item['displayName'],
            })

        self.response.payload = {'lakehouses': lakehouses}

Creating a lakehouse

A lakehouse is created like any other item - with conn.create_item and the Lakehouse type.

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class CreateSalesLakehouse(Service):

    input = 'workspace_id'

    def handle(self):

        conn = self.microsoft.fabric['My Fabric']

        lakehouse = conn.create_item(
            self.request.input.workspace_id,
            'Sales data',
            'Lakehouse',
            description='Sales data for analytics and reporting',
        )

        self.response.payload = {'lakehouse_id': lakehouse['id']}

Loading data into a lakehouse

Files land in the lakehouse's Files section through the OneLake data plane - conn.onelake_write takes the workspace, the path and the bytes to write. Once a file is in place, conn.load_table turns it into a Delta table and conn.wait_for_operation waits until the load completes.

# -*- coding: utf-8 -*-

# stdlib
import csv
import io

# Zato
from zato.server.service import Service

class LoadDailySales(Service):

    input = 'workspace_id', 'lakehouse_id'

    def handle(self):

        conn = self.microsoft.fabric['My Fabric']

        # Build a CSV file out of today's sales
        buffer = io.StringIO()
        writer = csv.writer(buffer)
        writer.writerow(['order_id', 'amount'])
        writer.writerow(['ORD-001', '250.00'])
        writer.writerow(['ORD-002', '99.90'])

        data = buffer.getvalue().encode('utf-8')

        # Write it to the lakehouse's Files section ..
        path = '{}/Files/sales/daily.csv'.format(self.request.input.lakehouse_id)
        conn.onelake_write(self.request.input.workspace_id, path, data)

        # .. load it into the daily_sales table ..
        location = conn.load_table(
            self.request.input.workspace_id,
            self.request.input.lakehouse_id,
            'daily_sales',
            'Files/sales/daily.csv',
        )

        # .. and wait until the load completes.
        operation = conn.wait_for_operation(location)

        self.response.payload = {'status': operation['status']}

To write a list of dicts to a table in one call, use conn.write_table - the Tables page covers it together with bulk and folder loads.

Listing tables

conn.list_tables returns each table of a lakehouse with its name and format.

# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class ListLakehouseTables(Service):

    input = 'workspace_id', 'lakehouse_id'

    def handle(self):

        conn = self.microsoft.fabric['My Fabric']

        # List the tables of a lakehouse
        tables = conn.list_tables(self.request.input.workspace_id, self.request.input.lakehouse_id)

        names = [table['name'] for table in tables]

        self.response.payload = {'tables': names}

Querying lakehouse data with SQL

conn.query runs SQL against the lakehouse's tables and returns the rows as a list of dicts - the Tables page covers querying in full.

rows = conn.query(workspace_id, lakehouse_id, 'select order_id, amount from daily_sales')

See also

PageWhat it covers
TablesLoading rows and files into lakehouse tables and querying them with SQL
OneLakeShortcuts plus reading and writing files through the data plane

Learn more