Skip to main content

Retrieve workflow results

This recipe retrieves the results of a Databricks Workflows job run using the Databricks SDK for Python.

Code snippet

app.py
import reflex as rx
from databricks.sdk import WorkspaceClient

w = WorkspaceClient()

class RetrieveJobResultsState(rx.State):
run_id: str = ""
results: str = ""
is_loading: bool = False
error_message: str = ""

@rx.event(background=True)
async def get_results(self):
async with self:
self.is_loading = True
self.error_message = ""
self.results = ""

if not self.run_id:
yield rx.toast("Please specify a Run ID.", level="warning")
self.is_loading = False
return

try:
# Ensure ID is an integer
run_id = int(self.run_id)
run_output = w.jobs.get_run_output(run_id=run_id)

async with self:
self.results = str(run_output)
yield rx.toast("Results retrieved successfully.", level="success")
except ValueError:
async with self:
self.error_message = "Run ID must be a valid number."
yield rx.toast("Run ID must be a valid number.", level="error")
except Exception as e:
async with self:
self.error_message = str(e)
yield rx.toast(f"Error: {e}", level="error")
finally:
async with self:
self.is_loading = False

Resources

Permissions

Your app service principal needs the following permissions:

  • CAN VIEW permission on the job

See Control access to a job for more information.

Dependencies

requirements.txt
databricks-sdk
reflex