<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Databricks Jobs API not returning notebook run results? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/databricks-jobs-api-not-returning-notebook-run-results/m-p/20917#M14168</link>
    <description>&lt;P&gt;Resolved this by using dbutils within the notebook being called from the API.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;# databricks notebook function
&amp;nbsp;
data = dbutils.widgets.get('data') # pulls base_parameters from API call
&amp;nbsp;
def add_test(i):
    result = i + ' COMPLETE'
    return result
&amp;nbsp;
dbutils.notebook.exit(str(add_test(data))&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Output from API call:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;COMPLETED&lt;/P&gt;&lt;P&gt;{'result': 'test COMPLETE', 'truncated': False}&lt;/P&gt;</description>
    <pubDate>Mon, 28 Nov 2022 19:30:59 GMT</pubDate>
    <dc:creator>apayne</dc:creator>
    <dc:date>2022-11-28T19:30:59Z</dc:date>
    <item>
      <title>Databricks Jobs API not returning notebook run results?</title>
      <link>https://community.databricks.com/t5/data-engineering/databricks-jobs-api-not-returning-notebook-run-results/m-p/20916#M14167</link>
      <description>&lt;P&gt;Calling a databricks notebook using the Rest API, can confirm that it is executing the notebook, but is not accepting my parameters or returning a notebook output. Any ideas on what I am doing wrong here?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My code and notebook function are below, trying to pass a string to the notebook function and have it return the results.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;# Databricks Rest API calls from IDE
&amp;nbsp;
import requests
import json
import time
&amp;nbsp;
job_payload = {
  "run_name": 'execute_test',
  "existing_cluster_id": '&amp;lt;cluster-id&amp;gt;',
  "notebook_task":
    {
      "notebook_path": '/Users/&amp;lt;user&amp;gt;/testing',
      "source": "WORKSPACE",
      "base_parameters": {"data": "test"}
    }
}
&amp;nbsp;
resp = requests.post('https://&amp;lt;url&amp;gt;/api/2.1/jobs/runs/submit', json=job_payload, headers={'Authorization': 'Bearer &amp;lt;access token&amp;gt;'})
&amp;nbsp;
run_id = json.loads(resp.text)['run_id']
run_results_payload = {
   "run_id": run_id
}
&amp;nbsp;
run_incomplete = True
while run_incomplete:
    resp = requests.get('https://&amp;lt;url&amp;gt;/api/2.1/jobs/runs/get', json=run_results_payload, headers={'Authorization': 'Bearer &amp;lt;access-token&amp;gt;'})
    state = json.loads(resp.text)['state']['life_cycle_state']
    if state == 'TERMINATED':
        print("COMPLETED: {}".format(run_id))
        resp = requests.get('https://&amp;lt;url&amp;gt;/api/2.1/jobs/runs/get-output', json=run_results_payload, headers={'Authorization': 'Bearer &amp;lt;access_token&amp;gt;'})
        output = json.loads(resp.text)['notebook_output']
        print(output)
        run_incomplete = False
    else:
        time.sleep(2)&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;# Databricks Notebook function
&amp;nbsp;
def print_number(data=None):
  return data&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Below are screenshots of the run and results in the UI&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="view"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/1123i40CF2D685D678010/image-size/large?v=v2&amp;amp;px=999" role="button" title="view" alt="view" /&gt;&lt;/span&gt;﻿&lt;span class="lia-inline-image-display-wrapper" image-alt="view2"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/1125iF8642D4061229A65/image-size/large?v=v2&amp;amp;px=999" role="button" title="view2" alt="view2" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;B&gt;EDIT:&lt;/B&gt; I was able to obtain notebook results by adding dbutils.notebook.exit("return_value") to the notebook being executed via the API call, but still struggling to get the notebook to accept the base_parameters provided in the API call&lt;/P&gt;</description>
      <pubDate>Wed, 23 Nov 2022 17:32:31 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/databricks-jobs-api-not-returning-notebook-run-results/m-p/20916#M14167</guid>
      <dc:creator>apayne</dc:creator>
      <dc:date>2022-11-23T17:32:31Z</dc:date>
    </item>
    <item>
      <title>Re: Databricks Jobs API not returning notebook run results?</title>
      <link>https://community.databricks.com/t5/data-engineering/databricks-jobs-api-not-returning-notebook-run-results/m-p/20917#M14168</link>
      <description>&lt;P&gt;Resolved this by using dbutils within the notebook being called from the API.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;# databricks notebook function
&amp;nbsp;
data = dbutils.widgets.get('data') # pulls base_parameters from API call
&amp;nbsp;
def add_test(i):
    result = i + ' COMPLETE'
    return result
&amp;nbsp;
dbutils.notebook.exit(str(add_test(data))&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Output from API call:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;COMPLETED&lt;/P&gt;&lt;P&gt;{'result': 'test COMPLETE', 'truncated': False}&lt;/P&gt;</description>
      <pubDate>Mon, 28 Nov 2022 19:30:59 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/databricks-jobs-api-not-returning-notebook-run-results/m-p/20917#M14168</guid>
      <dc:creator>apayne</dc:creator>
      <dc:date>2022-11-28T19:30:59Z</dc:date>
    </item>
  </channel>
</rss>

