pythonmarkdowndatabricks

Converting .py file to Databricks with Markdown


I wish to convert a python .py file into one that can be run on Databricks with multiple cells. I don't wish to do it in the GUI, as I'll eventually want an automated process for this. What is the minimal code to create a title cell and then have the rest of the code being Python code? I've tried this, but it doesn't work:

%md
# Title

%python
# My code goes here

I could copy some output from a Databricks notebook I created my self I suppose, but it's really not that minimal.


Solution

  • If you want a notebook, you should start your Python file with the line

    # Databricks notebook source
    

    For a header, as well as for any other markdown you write in your notebook, start with the following right after the first line.

    # MAGIC %md
    # MAGIC ...put your text here...
    

    Then, separate your cells with the following, including the empty lines before and after.

    
    # COMMAND ----------
    
    

    That's completely it. Any block of code that is Python code doesn't need the MAGIC command and doesn't need anything, as the code is Python by default due to your .py extension. This means that your exact case should be converted to the following:

    # Databricks notebook source
    # MAGIC %md
    # MAGIC # Title
    
    # COMMAND ----------
    
    print("Any Python code!")