<?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: dbutils.notebook API: pass data back to caller notebook in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/dbutils-notebook-api-pass-data-back-to-caller-notebook/m-p/105213#M42048</link>
    <description>&lt;P&gt;I can have a try and see if it's the same like exit(view_name) in that the view is created in global_temp_db and that the lifecycle is with the job compute.&lt;/P&gt;</description>
    <pubDate>Fri, 10 Jan 2025 16:55:19 GMT</pubDate>
    <dc:creator>lauraxyz</dc:creator>
    <dc:date>2025-01-10T16:55:19Z</dc:date>
    <item>
      <title>dbutils.notebook API: pass data back to caller notebook</title>
      <link>https://community.databricks.com/t5/data-engineering/dbutils-notebook-api-pass-data-back-to-caller-notebook/m-p/105096#M41991</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;according to &lt;A href="#%20Example 1 - returning data through temporary views. # You can only return one string using dbutils.notebook.exit(), but since called notebooks reside in the same JVM, you can # return a name referencing data stored in a temporary view.  ## In callee notebook spark.range(5).toDF(&amp;quot;value&amp;quot;).createOrReplaceGlobalTempView(&amp;quot;my_data&amp;quot;) dbutils.notebook.exit(&amp;quot;my_data&amp;quot;)  ## In caller notebook returned_table = dbutils.notebook.run(&amp;quot;LOCATION_OF_CALLEE_NOTEBOOK&amp;quot;, 60) global_temp_db = spark.conf.get(&amp;quot;spark.sql.globalTempDatabase&amp;quot;) display(table(global_temp_db + &amp;quot;.&amp;quot; + returned_table))  # Example 2 - returning data through DBFS. # For larger datasets, you can write the results to DBFS and then return the DBFS path of the stored data.  ## In callee notebook dbutils.fs.rm(&amp;quot;/tmp/results/my_data&amp;quot;, recurse=True) spark.range(5).toDF(&amp;quot;value&amp;quot;).write.format(&amp;quot;parquet&amp;quot;).save(&amp;quot;dbfs:/tmp/results/my_data&amp;quot;) dbutils.notebook.exit(&amp;quot;dbfs:/tmp/results/my_data&amp;quot;)  ## In caller notebook returned_table = dbutils.notebook.run(&amp;quot;LOCATION_OF_CALLEE_NOTEBOOK&amp;quot;, 60) display(spark.read.format(&amp;quot;parquet&amp;quot;).load(returned_table))  # Example 3 - returning JSON data. # To return multiple values, you can use standard JSON libraries to serialize and deserialize results.  ## In callee notebook import json dbutils.notebook.exit(json.dumps({   &amp;quot;status&amp;quot;: &amp;quot;OK&amp;quot;,   &amp;quot;table&amp;quot;: &amp;quot;my_data&amp;quot; }))  ## In caller notebook import json  result = dbutils.notebook.run(&amp;quot;LOCATION_OF_CALLEE_NOTEBOOK&amp;quot;, 60) print(json.loads(result))" target="_self"&gt;this&lt;/A&gt; doc, we can pass data back through temp views, DBFS, or JSON data.&lt;/P&gt;&lt;P&gt;However, in my case, i need to pass both a temp view, as well as some metadata in JSON.&amp;nbsp; is there a way to exit with BOTH a view AND json, something like&lt;/P&gt;&lt;P&gt;dbutils.notebook.exit("my_view_name", json.dumps({&lt;BR /&gt;&amp;nbsp; "status": "OK",&lt;BR /&gt;&amp;nbsp; "table": "my_data"&lt;BR /&gt;}))&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jan 2025 01:29:41 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/dbutils-notebook-api-pass-data-back-to-caller-notebook/m-p/105096#M41991</guid>
      <dc:creator>lauraxyz</dc:creator>
      <dc:date>2025-01-10T01:29:41Z</dc:date>
    </item>
    <item>
      <title>Re: dbutils.notebook API: pass data back to caller notebook</title>
      <link>https://community.databricks.com/t5/data-engineering/dbutils-notebook-api-pass-data-back-to-caller-notebook/m-p/105103#M41994</link>
      <description>&lt;P&gt;Can we try using a combination of &lt;CODE&gt;dbutils.notebook.exit()&lt;/CODE&gt; and temporary views? there is a drawback that&amp;nbsp;&lt;CODE&gt;dbutils.notebook.exit()&lt;/CODE&gt; can only return a single string.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;import json
from pyspark.sql import SparkSession

# Create a temporary view
data = spark.range(5).toDF("value")
data.createOrReplaceTempView("my_view_name")

# Create the metadata
metadata = {
    "status": "OK",
    "table": "my_view_name"
}

# Exit the notebook with the JSON string
dbutils.notebook.exit(json.dumps(metadata))&lt;/LI-CODE&gt;&lt;LI-CODE lang="python"&gt;import json
from pyspark.sql import SparkSession

data = spark.range(5).toDF("value")
data.createOrReplaceTempView("my_view_name")

metadata = {
    "status": "OK",
    "table": "my_view_name"
}

dbutils.notebook.exit(json.dumps(metadata))&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;use this notebook to call it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;import json

# Run the callee notebook and get the result
result = dbutils.notebook.run("path_to_callee_notebook", 60)

# Parse the JSON result
metadata = json.loads(result)

# Access the temporary view
global_temp_db = spark.conf.get("spark.sql.globalTempDatabase")
view_name = metadata["table"]
display(spark.sql(f"SELECT * FROM {global_temp_db}.{view_name}"))&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jan 2025 05:11:25 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/dbutils-notebook-api-pass-data-back-to-caller-notebook/m-p/105103#M41994</guid>
      <dc:creator>SparkJun</dc:creator>
      <dc:date>2025-01-10T05:11:25Z</dc:date>
    </item>
    <item>
      <title>Re: dbutils.notebook API: pass data back to caller notebook</title>
      <link>https://community.databricks.com/t5/data-engineering/dbutils-notebook-api-pass-data-back-to-caller-notebook/m-p/105213#M42048</link>
      <description>&lt;P&gt;I can have a try and see if it's the same like exit(view_name) in that the view is created in global_temp_db and that the lifecycle is with the job compute.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jan 2025 16:55:19 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/dbutils-notebook-api-pass-data-back-to-caller-notebook/m-p/105213#M42048</guid>
      <dc:creator>lauraxyz</dc:creator>
      <dc:date>2025-01-10T16:55:19Z</dc:date>
    </item>
  </channel>
</rss>

