简体   繁体   中英

How to import `displayHTML` in DataBricks?

I created a python package that relies on IPython to display HTML in notebooks in different environments like Jupyter or Google Collab.
While testing it with DataBricks I noticed it doesn't work at all.
Code example:

from IPython import display, HTML

my_html_str = '<img src="https://raw.githubusercontent.com/karolzak/boxdetect/master/images/checkboxes-details.jpg"/>'
display(HTML(my_html_str))

The above code would work everywhere but in DataBricks. For DataBricks, I need to run displayHTML(my_html_str) in one of the cells to make it display my HTML.
The problem is I need to run displayHTML from my python package code level and when I'm doing it there it raises an error saying that displayHTML doesn't exist. It does run correclt only if I run displayHTML from one of the code cells in DB notebook.
Code for displaying HTML inside my package:

if "DATABRICKS_RUNTIME_VERSION" in os.environ:
    displayHTML(html_viewer)
    return displayHTML(html)
else:
    from IPython import display, HTML
    display(HTML(html_viewer))
    return display(HTML(html))

With above code I get below error when trying to run in DataBricks: 在此处输入图像描述

The question is how should I import displayHTML inside my package code to make it work correctly for DB?

Here is an alternative way to import the required packages into your block.

from IPython.core.display import display, HTML
display(HTML("your content"))

In Databricks the display functions is coming from PythonShell

import PythonShell as pi
pi.PythonShell.display(temps)

This works in Databricks but this is not whitelisted for outside use.

I don't know if it is possible to import displayHTML in a standard way. Apparently, the function is defined inside the PythonShell class which is instantiated and executed in /databricks/python_shell/scripts/PythonShellImpl.py , providing no way to call the function from an external package such as yours.

To solve this problem, I used the inspect module (just like the plotly library when used inside databricks). Here is a code snippet that should work in your case (of course, you could make it prettier inside a class, with a dynamic attribute - see https://github.com/plotly/plotly.py/blob/master/packages/python/plotly/plotly/io/_base_renderers.py line 780 for inspiration) :

def display_html(html: str) -> None:
    """
    Use databricks displayHTML from an external package
    
    Args:
    - html : html document to display
    """
    import inspect
    for frame in inspect.getouterframes(inspect.currentframe()):
        global_names = set(frame.frame.f_globals)
        # Use multiple functions to reduce risk of mismatch
        if all(v in global_names for v in ["displayHTML", "display", "spark"]):
            return frame.frame.f_globals["displayHTML"](html)
    raise EnvironmentError(
        "Unable to detect displayHTML function"
    )

my_html_str = '<img src="https://raw.githubusercontent.com/karolzak/boxdetect/master/images/checkboxes-details.jpg"/>'
display_html(my_html_str)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM