Determining ipywidget context (lab vs. notebook vs. embedded)

I am implementing an ipywidget and would like it to be aware of the context in which it is running – that is, whether it is running in JupyterLab vs. Jupyter Notebook vs. embedded elsewhere.

It seems like this should be a simple task, but I haven’t yet found a way to reliably determine this on the client-side. For example:

function is_lab(): boolean {
    // Returns true if running in JupyterLab
}

function is_notebook(): boolean {
    // Returns true if running in Jupyter Notebook
}

In the classic Notebook interface, I know that I can check whether the Jupyter global variable exists, whether Jupyter.notebook exists and read Jupyter.version from it to get the version number, but I haven’t found an equivalent in JupyterLab.

Is there some way to do this that I am missing?