Skip to main content

cookbook-compute

Connect Databricks Apps to shared clusters or serverless compute using Databricks Connect. Use when the user needs to run Spark SQL or Python on a cluster from a Dash, Streamlit, or Reflex app.

Source facts

Repository
databricks-solutions/databricks-apps-cookbook
Last source activity
April 28, 2026 at 11:17
Detected SKILL.md language
English
Stars
187
Forks
124

Install options

The review-first prompt is selected by default. You can switch to a direct command or download a local copy.

Review the source files

Read SKILL.md and any companion files shown by SkillsMP before deciding whether to install.

Showing SKILL.md

SKILL.md
Source instructions · Read-only preview
name
cookbook-compute
description
Connect Databricks Apps to shared clusters or serverless compute using Databricks Connect. Use when the user needs to run Spark SQL or Python on a cluster from a Dash, Streamlit, or Reflex app.
# Connect to Compute Use [Databricks Connect](https://docs.databricks.com/en/dev-tools/databricks-connect/python/index.html) to execute Python or SQL code on a shared cluster or serverless compute from your Databricks App. ## Recipes ### Connect to a shared cluster #### Dash ```python import os from databricks.connect import DatabricksSession cluster_id = "0709-132523-cnhxf2p6" spark = DatabricksSession.builder.remote( host=os.getenv("DATABRICKS_HOST"), cluster_id=cluster_id ).getOrCreate() query = "SELECT 'hello' AS message" result = spark.sql(query).toPandas() print(result) ``` #### Streamlit ```python import os import streamlit as st from databricks.connect import DatabricksSession cluster_id = st.text_input("Cluster ID:", placeholder="0709-132523-cnhxf2p6") if cluster_id: spark = DatabricksSession.builder.remote( host=os.getenv("DATABRICKS_HOST"), cluster_id=cluster_id ).getOrCreate() result = spark.sql("SELECT 'hello' AS message").toPandas() st.dataframe(result) ``` #### Reflex ```python import os import asyncio import reflex as rx from databricks.connect import DatabricksSession def run_spark_workload(host: str, cluster_id: str): spark = DatabricksSession.builder.remote( host=host, cluster_id=cluster_id ).getOrCreate() return spark.sql("SELECT 'hello' AS message").toPandas().values.tolist() class ConnectClusterState(rx.State): cluster_id: str = "" result: list = [] @rx.event(background=True) async def connect_and_run(self): host = os.getenv("DATABRICKS_HOST") loop = asyncio.get_running_loop() data = await loop.run_in_executor( None, run_spark_workload, host, self.cluster_id ) async with self: self.result = data ``` ### Serverless compute You can also [connect to serverless compute](https://docs.databricks.com/aws/en/compute/serverless/) instead of a shared cluster. Replace the cluster connection with: ```python from databricks.connect import DatabricksSession spark = DatabricksSession.builder.serverless(True).getOrCreate() ``` ## Resources - [All-purpose compute](https://docs.databricks.com/aws/en/compute/use-compute) or [serverless compute](https://docs.databricks.com/aws/en/compute/serverless/) ## Permissions The app service principal needs: - `CAN ATTACH TO` on the cluster See [Compute permissions](https://docs.databricks.com/aws/en/compute/clusters-manage#compute-permissions). ## Dependencies ``` databricks-connect pandas ``` Plus your framework package (`dash`, `streamlit`, or `reflex`). ## Common Pitfalls - Reflex requires `@rx.event(background=True)` and `asyncio.get_running_loop().run_in_executor()` for blocking Spark calls - `DATABRICKS_HOST` is auto-injected when deployed; set it manually for local development - Shared clusters must be running; serverless compute starts on demand
View on GitHub