2

With IPython/Jupyter it's possible to output markdown using the IPython display module and its MarkDownclass.

Example markdown output

Question

How can I accomplish this with Azure Databricks?

What I tried

Databricks display

Tried using Databrick's display with the IPython Markdown class:

from IPython.display import Markdown
display(Markdown('*some markdown* test'))

but this results in the following error:

Exception: Cannot call display(<class 'IPython.core.display.Markdown'>)

IPython display

I then tried to use IPython's display:

from IPython.display import display, Markdown
display(Markdown('*some markdown* test'))

but this just displays the text:

<IPython.core.display.Markdown object>

Example failed ouput

IPython display_markdown

Tried using IPython's display_markdown:

from IPython.display import display_markdown
display_markdown('# Markdown is here!\n*some markdown*\n- and\n- some\n- more')

but this results in nothing showing up:

Failed display_markdown

Looking up documentation

Also tried checking Azure Databricks documentation. At first I visited https://www.databricks.com/databricks-documentation which leads me to https://learn.microsoft.com/en-ca/azure/databricks/ but I wasn't able to find anything via searching or clicking the links and I usually find Microsoft documentation quite good.

Checking Databrick's display source

As Saideep Arikontham mentioned in the comments, Databricks version 11 and above is using IPython kernel so I dug a bit deeper.

According to Databrick's source for the display function, it will readily render any object that implements _repr_html().

Databricks display

However I'm having a hard time being able to get the raw html output that I'm assuming IPython.display.Markdown should be able to output. I can only find _repr_markdown_() and _data_and_metadata() where the former just calls the latter and the output, at least in Databricks, is just the original raw markdown string.

3
  • Thanks for that @SaideepArikontham. As a result of your comment I dug a little deeper and updated my question. There must be a way to use one of IPython's module to generate the raw markdown html that I can wrap with an object and have that returned via _repr_html_(). Commented Aug 31, 2022 at 18:20
  • 1
    I couldn't find an IPython module so that we get the required output returned via _repr_html_() from Markdown class. However, I was able to get required output by writing my own Markdown class with help of another Python library. Will provide that as a solution if it will suffice your requirement. Commented Sep 1, 2022 at 10:32
  • @SaideepArikontham that would be awesome! Thanks! Commented Sep 1, 2022 at 10:33

2 Answers 2

4

Markdown and display_markdown are not giving desired output when used in Azure Databricks. I have done the following in Databricks 11.1 runtime.

  • Taking inputs from the question, I understood that when a class has _repr_html(), it is able to output the desired result. But when this method is absent in class, it is returning an object.
  • So, for Markdown to work, I have written my own Markdown class where I used Python's markdown library.
from IPython.display import DisplayObject, TextDisplayObject

class Markdown(TextDisplayObject):

    def __init__(self,TextDisplayObject):
        import markdown as md
        
        #converting markdown to html
        self.html = md.markdown(TextDisplayObject)
        
    
    def _repr_html_(self):
        return self.html
  • Now, this class is not completely same as the IPython.display.Markdown. I have formatted your sample markdown '# Markdown is here!\n*some markdown*\n- and\n- some\n- more' as following to get the desired result.
Markdown('''# Markdown is here!\n
*some markdown*\n
- and\n
- some\n
- more''')

enter image description here

NOTE:

  • For display_markdown() to display output, we must specify another argument raw as True (display_markdown(<markdown_str>, raw=True)). However, in Databricks it is returning undefined (NoneType).

  • Please do install markdown library first using %pip install markdown in Databricks cell.

Sign up to request clarification or add additional context in comments.

Comments

2

As of 2024, using a Databricks Runtime 15.4 LTS, it is possible to use IPython.display suggested in the original question.

from IPython.display import Markdown, display

# Generate welcome markdown text
welcome_text = '''
# Welcome to my notebook!

## Examples of titles and subtitles

### Title 1
This is an example of a title.

## Example of a table

| Column 1 | Column 2 | Column 3 |
|----------|----------|----------|
|   A      |    B     |    C     |
|   D      |    E     |    F     |
|   G      |    H     |    I     |
'''

# Display the welcome markdown text
display(Markdown(welcome_text))

Will successfully display the expected markdown
Databricks Markdown example

1 Comment

it also works if importing only from IPython.display import Markdown and the last line is only Markdown(welcome_text)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.