Combine SQL cell output in a markdown cell in Databricks Notebook

41 views Asked by At

I would like to run a cell in SQL and then include a markdown cells that includes the cell output.

SQL cell : select count(*) from table

1500

1 row returned

Expected Output: Markdown cell : Row count of the table is 1500

1

There are 1 answers

0
Rakesh Govindula On BEST ANSWER

As per this ask, currently markdown cells don't support variables, or we can't pass any cell's output dynamically to the markdown cell. It's still a feature request.

As an alternative, you can try the below workaround where a python cell is used to generate the expected markdown result.

Give your SQL count code in the spark.sql() and get the required markdown like below.

%python

from IPython.display import display, Markdown
count = spark.sql('select  count(*) from table1').select("*").first()[0]
display(Markdown('## Row count of the table is '+str(count)))

enter image description here

NOTE: If it doesn't work, make sure the Databricks runtime is latest. Here, my runtime is 13.3 LTS (includes Apache Spark 3.4.1, Scala 2.12)