Example #1
0
def transient_html(code: str, lifetime: float = 1):
    """Attempts to display and then hide given HTML code.

    If the hiding is not possible (no ipywidgets) fall-backs to persistent display.

    Arguments:
        code: HTML code as a string
        lifetime: number of seconds to wait before closing
    """
    try:
        from ipywidgets import HTML
        element = HTML(code)

        def delayed_close():
            sleep(lifetime)
            element.close()

        display(element)
        element.on_displayed(delayed_close())

    except ImportError:
        element = HTML(code)
        display(element)