Skip to content

pixano.app.display

display_cli(url, port)

Display a Pixano app inside a command line interface.

Parameters:

Name Type Description Default
url str

Pixano app URL.

required
port int

Pixano app port.

required
Source code in pixano/app/display.py
def display_cli(url: str, port: int):
    """Display a Pixano app inside a command line interface.

    Args:
        url: Pixano app URL.
        port: Pixano app port.
    """
    print(f"Please visit {url}:{port} in a web browser.")

display_colab(url, port, height)

Display a Pixano app inside a Google Colab.

Parameters:

Name Type Description Default
url str

Pixano app URL.

required
port int

Pixano app port.

required
height int

Frame height.

required
Source code in pixano/app/display.py
def display_colab(url: str, port: int, height: int):
    """Display a Pixano app inside a Google Colab.

    Args:
        url: Pixano app URL.
        port: Pixano app port.
        height: Frame height.
    """
    # Define frame template
    shell = """
        (() => {
            const url = new URL(%URL%);
            const port = %PORT%;
            if (port) {
                url.port = port;
            }
            const iframe = document.createElement('iframe');
            iframe.src = url;
            iframe.setAttribute('width', '100%');
            iframe.setAttribute('height', '%HEIGHT%');
            iframe.setAttribute('frameborder', 0);
            document.body.appendChild(iframe);
        })();
    """

    # Replace variables in template
    replacements = [
        ("%HEIGHT%", f"{height}"),
        ("%PORT%", f"{port}"),
        ("%URL%", json.dumps(url)),
    ]
    for k, v in replacements:
        shell = shell.replace(k, v)

    # Display frame
    script = IPython.display.Javascript(shell)
    IPython.display.display(script)

display_ipython(url, port, height)

Display a Pixano app inside a Jupyter notebook.

Parameters:

Name Type Description Default
url str

Pixano app URL.

required
port int

Pixano app port.

required
height int

Frame height.

required
Source code in pixano/app/display.py
def display_ipython(url: str, port: int, height: int):
    """Display a Pixano app inside a Jupyter notebook.

    Args:
        url: Pixano app URL.
        port: Pixano app port.
        height: Frame height.
    """
    # Define frame template
    shell = """
        <iframe id="%HTML_ID%" width="100%" height="%HEIGHT%" frameborder="0">
        </iframe>
        <script>
            (function() {
                const frame = document.getElementById(%JSON_ID%);
                const url = new URL(%URL%, window.location);
                const port = %PORT%;
                if (port) {
                    url.port = port;
                }
                frame.src = url;
            })();
        </script>
    """

    # Replace variables in template
    frame_id = f"frame-{shortuuid.uuid()}"
    replacements = [
        ("%HTML_ID%", html.escape(frame_id, quote=True)),
        ("%JSON_ID%", json.dumps(frame_id)),
        ("%HEIGHT%", f"{height}"),
        ("%PORT%", f"{port}"),
        ("%URL%", json.dumps(url)),
    ]
    for k, v in replacements:
        shell = shell.replace(k, v)

    # Display frame
    iframe = IPython.display.HTML(shell)
    IPython.display.display(iframe)