<?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 How to Copy the notebooks from one environment to another environment in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-copy-the-notebooks-from-one-environment-to-another/m-p/112770#M44320</link>
    <description>&lt;P&gt;I have one requirement to copy the notebooks from one environment to another environment by one notebook automatically , how can I achieve it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 17 Mar 2025 06:34:16 GMT</pubDate>
    <dc:creator>databicky</dc:creator>
    <dc:date>2025-03-17T06:34:16Z</dc:date>
    <item>
      <title>How to Copy the notebooks from one environment to another environment</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-copy-the-notebooks-from-one-environment-to-another/m-p/112770#M44320</link>
      <description>&lt;P&gt;I have one requirement to copy the notebooks from one environment to another environment by one notebook automatically , how can I achieve it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Mar 2025 06:34:16 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-copy-the-notebooks-from-one-environment-to-another/m-p/112770#M44320</guid>
      <dc:creator>databicky</dc:creator>
      <dc:date>2025-03-17T06:34:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to Copy the notebooks from one environment to another environment</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-copy-the-notebooks-from-one-environment-to-another/m-p/112847#M44350</link>
      <description>&lt;P&gt;Hey&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/27208"&gt;@databicky&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P class=""&gt;You can automate the process of copying notebooks from one Databricks environment to another using the &lt;SPAN class=""&gt;Databricks REST API&lt;/SPAN&gt; within a notebook. I show you the easiest way I found to do it&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import json
import requests
import base64

# ==============================
#  CONFIGURATION: Databricks Details
# ==============================

# Source Databricks details
SOURCE_DATABRICKS_HOST = "https://&amp;lt;source-workspace&amp;gt;.cloud.databricks.com"
SOURCE_TOKEN = "dapiXXXXXXXXXXXXXXXXXXXXXXXXX"
NOTEBOOK_PATH = "/Workspace/Users/&amp;lt;source-user&amp;gt;/notebook_name"

# Destination Databricks details
DEST_DATABRICKS_HOST = "https://&amp;lt;destination-workspace&amp;gt;.cloud.databricks.com"
DEST_TOKEN = "dapiYYYYYYYYYYYYYYYYYYYYYYYYY"
DEST_NOTEBOOK_PATH = "/Workspace/Users/&amp;lt;destination-user&amp;gt;/notebook_name"

# ==============================
#  STEP 1: EXPORT NOTEBOOK FROM SOURCE
# ==============================
export_url = f"{SOURCE_DATABRICKS_HOST}/api/2.0/workspace/export"
headers = {"Authorization": f"Bearer {SOURCE_TOKEN}"}
params = {"path": NOTEBOOK_PATH, "format": "SOURCE"}

response = requests.get(export_url, headers=headers, params=params)

if response.status_code == 200:
    notebook_data = response.json()
    
    # Decode Base64 content
    notebook_content = base64.b64decode(notebook_data["content"]).decode("utf-8")
    
    print("\nNotebook exported successfully!")
    
    # Save locally as a file for reference (optional)
    with open("exported_notebook.py", "w", encoding="utf-8") as f:
        f.write(notebook_content)

    print("Decoded content saved as 'exported_notebook.py'\n")

else:
    print(f"Failed to export: {response.status_code} - {response.text}")
    exit()

# ==============================
#  STEP 2: IMPORT NOTEBOOK TO DESTINATION
# ==============================

# Encode content in Base64 for importing
encoded_content = base64.b64encode(notebook_content.encode()).decode()

import_url = f"{DEST_DATABRICKS_HOST}/api/2.0/workspace/import"
headers = {"Authorization": f"Bearer {DEST_TOKEN}"}
data = {
    "path": DEST_NOTEBOOK_PATH,
    "format": "SOURCE",
    "content": encoded_content,
    "language": "PYTHON",
}

print(f"Sending request to import notebook to: {DEST_NOTEBOOK_PATH}")

response = requests.post(import_url, headers=headers, json=data)

if response.status_code == 200:
    print("\nNotebook imported successfully to the destination workspace!")
else:
    print(f"\nFailed to import: {response.status_code} - {response.text}")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;By the way you dont have to run it in a notebook, can run locally.&lt;BR /&gt;&lt;BR /&gt;I tested and works &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;Isi&lt;/P&gt;</description>
      <pubDate>Mon, 17 Mar 2025 20:59:38 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-copy-the-notebooks-from-one-environment-to-another/m-p/112847#M44350</guid>
      <dc:creator>Isi</dc:creator>
      <dc:date>2025-03-17T20:59:38Z</dc:date>
    </item>
  </channel>
</rss>

