<?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: Programmatically create Databricks Notebook in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/programmatically-create-databricks-notebook/m-p/86938#M37358</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/118362"&gt;@wesg2&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;One needs to be very precise when building this.&lt;BR /&gt;&lt;BR /&gt;The below code WORKS:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Define the content of the .py file with cell separators (Works!)
notebook_content = """# Databricks notebook source
# This is the header of the notebook
# You can add imports or setup code here

# COMMAND ----------
# This is the first code cell
print('Hello from the first cell!')

# COMMAND ----------
# This is the second code cell
print('This is another cell.')
"""

# Define the output path (update the path as needed)
output_path = "&amp;lt;your path&amp;gt;/notebook_script.py"

# Save the notebook content to the .py file
dbutils.fs.put(output_path, notebook_content, overwrite=True)

# Inform the user of the location of the saved file
print(f"Notebook saved as .py file at: {output_path}")&lt;/LI-CODE&gt;&lt;P&gt;The below code does NOT work (extra new line character):&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Define the content of the .py file with cell separators (New Line character - does not work!)
notebook_content = """
# Databricks notebook source
# This is the header of the notebook
# You can add imports or setup code here

# COMMAND ----------
# This is the first code cell
print('Hello from the first cell!')

# COMMAND ----------
# This is the second code cell
print('This is another cell.')
"""

# Define the output path (update the path as needed)
output_path = "&amp;lt;your path&amp;gt;/notebook_script.py"

# Save the notebook content to the .py file
dbutils.fs.put(output_path, notebook_content, overwrite=True)

# Inform the user of the location of the saved file
print(f"Notebook saved as .py file at: {output_path}")&lt;/LI-CODE&gt;&lt;P&gt;However, I have copied your&amp;nbsp;&lt;SPAN&gt;Notebook_Head, saved this in ADLS as .py file and then imported back to Databricks and it also works.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Have you tried with just Notebook_Head? If yes, do you followed the same steps as I or do you do it differently?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 30 Aug 2024 17:00:42 GMT</pubDate>
    <dc:creator>filipniziol</dc:creator>
    <dc:date>2024-08-30T17:00:42Z</dc:date>
    <item>
      <title>Programmatically create Databricks Notebook</title>
      <link>https://community.databricks.com/t5/data-engineering/programmatically-create-databricks-notebook/m-p/86873#M37351</link>
      <description>&lt;P&gt;I am creating a databricks notebook via string concats (sample below)&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Notebook_Head &lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt; &lt;SPAN&gt;"""&lt;/SPAN&gt;&lt;SPAN&gt;# Databricks notebook source&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;# from pyspark.sql.types import StringType&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;# from pyspark.sql.functions import split&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;# COMMAND ----------&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;"""&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Full_NB = Notebook_Head + Mid + Tail&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;I am using dbutils.fs.put() to write to a storage location.&lt;BR /&gt;When I then try to import that file as a NB in databricks it displays as one large text file, NOT as a Databricks Notebook.&amp;nbsp;&lt;BR /&gt;Line 1 = # Databricks notebook source&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;The notebook does not break up into command cells even though I have the proper # COMMAND ----------&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 30 Aug 2024 14:23:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/programmatically-create-databricks-notebook/m-p/86873#M37351</guid>
      <dc:creator>wesg2</dc:creator>
      <dc:date>2024-08-30T14:23:56Z</dc:date>
    </item>
    <item>
      <title>Re: Programmatically create Databricks Notebook</title>
      <link>https://community.databricks.com/t5/data-engineering/programmatically-create-databricks-notebook/m-p/86938#M37358</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/118362"&gt;@wesg2&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;One needs to be very precise when building this.&lt;BR /&gt;&lt;BR /&gt;The below code WORKS:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Define the content of the .py file with cell separators (Works!)
notebook_content = """# Databricks notebook source
# This is the header of the notebook
# You can add imports or setup code here

# COMMAND ----------
# This is the first code cell
print('Hello from the first cell!')

# COMMAND ----------
# This is the second code cell
print('This is another cell.')
"""

# Define the output path (update the path as needed)
output_path = "&amp;lt;your path&amp;gt;/notebook_script.py"

# Save the notebook content to the .py file
dbutils.fs.put(output_path, notebook_content, overwrite=True)

# Inform the user of the location of the saved file
print(f"Notebook saved as .py file at: {output_path}")&lt;/LI-CODE&gt;&lt;P&gt;The below code does NOT work (extra new line character):&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Define the content of the .py file with cell separators (New Line character - does not work!)
notebook_content = """
# Databricks notebook source
# This is the header of the notebook
# You can add imports or setup code here

# COMMAND ----------
# This is the first code cell
print('Hello from the first cell!')

# COMMAND ----------
# This is the second code cell
print('This is another cell.')
"""

# Define the output path (update the path as needed)
output_path = "&amp;lt;your path&amp;gt;/notebook_script.py"

# Save the notebook content to the .py file
dbutils.fs.put(output_path, notebook_content, overwrite=True)

# Inform the user of the location of the saved file
print(f"Notebook saved as .py file at: {output_path}")&lt;/LI-CODE&gt;&lt;P&gt;However, I have copied your&amp;nbsp;&lt;SPAN&gt;Notebook_Head, saved this in ADLS as .py file and then imported back to Databricks and it also works.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Have you tried with just Notebook_Head? If yes, do you followed the same steps as I or do you do it differently?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Aug 2024 17:00:42 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/programmatically-create-databricks-notebook/m-p/86938#M37358</guid>
      <dc:creator>filipniziol</dc:creator>
      <dc:date>2024-08-30T17:00:42Z</dc:date>
    </item>
  </channel>
</rss>

