microsoft / vscode-notebook-renderers

MIT License
115 stars 36 forks source link

Type `application/vnd.dataresource+json` does not produce any visible output #189

Open Seally opened 10 months ago

Seally commented 10 months ago

I first discovered this issue while trying to use nodejs-polars in a Deno Jupyter kernel. Here's the example code for a notebook cell in Python:

from IPython.display import display

display(
    {
        "application/vnd.dataresource+json": {
            "data": [
                {"continent": "Africa", "total_pop_est": 993281878},
                {"continent": "Antarctica", "total_pop_es": 3802},
                {"continent": "Asia", "total_pop_es": 4085852698},
                {"continent": "Europe", "total_pop_es": 728131201},
                {"continent": "North America", "total_pop_es": 539350981},
                {"continent": "Oceania", "total_pop_es": 33519610},
                {"continent": "Seven seas (open ocean)", "total_pop_es": 140},
                {"continent": "South America", "total_pop_es": 394355478}
            ],
            "schema": {
                "fields": [
                    {"name": "continent", "type": "string"},
                    {"name": "total_pop_est", "type": "integer"}
                ]
            }
        },
        "text/html": "<table><thead><tr><th>continent</th><th>total_pop_est</th></tr></thead><tbody><tr><td>Africa</td><td>993281878</td></tr><tr><td>Antarctica</td><td>3802</td></tr><tr><td>Asia</td><td>4085852698</td></tr><tr><td>Europe</td><td>728131201</td></tr><tr><td>North America</td><td>539350981</td></tr><tr><td>Oceania</td><td>33519610</td></tr><tr><td>Seven seas (open ocean)</td><td>140</td></tr><tr><td>South America</td><td>394355478</td></tr></tbody></table>"
    },
    raw=True
)

Using the text/html presentation works fine. But the application/vnd.dataresource+json presentation does not, and produces basically nothing:

image

VS Code version: 1.85.1 Extension version: 1.0.17 OS: Windows 10

MasterPtato commented 9 months ago

Same issue here. Looking at the dev tools I see these errors image

VS Code: 1.85.1 OS: Ubuntu 20.04.1

scarf005 commented 9 months ago

@DonJayamanne since i've been pointed here, may i ask pointers on where to look for in order to fix this issue? I'm interested in opening a PR.

universalmind303 commented 2 months ago

bumping this. It seems like it's using an unmaintained package for handling the datatype.

@DonJayamanne Could we just drop support for this mimetype and let vscode-jupyter fall back to text/html

soonbro commented 1 month ago

Hello, I'm not sure if my comment will be helpful. I recently discovered Azure Data Studio, which is based on Microsoft's VS Code, and used it for a day. I noticed that the Notebook feature in Azure Data Studio outputs SQL query results in a grid format. When I open the resulting .ipynb file in VS Code, the vscode-notebook-renderers extension is set to render the application/vnd.dataresource+json mimetype. If I use the built-in notebook output renderer in VS Code to display it as text/html, it renders correctly. However, it doesn't look as nice as it does in Azure Data Studio.

in VS Code image

in Azure Data Studio in Azure Data Studio

So, I would like to see support for the application/vnd.dataresource+json mimetype in VS Code, similar to Azure Data Studio.

Thank you!

Seally commented 1 month ago

So, I would like to see support for the application/vnd.dataresource+json mimetype in VS Code, similar to Azure Data Studio.

@soonbro Technically, vscode-notebook-renderers claims to support application/vnd.dataresource+json on this line:

https://github.com/microsoft/vscode-notebook-renderers/blob/963366c00f3e33c1c4e80236841230e0fd8e7d8b/package.json#L88

The problem is that it doesn't work (last I checked). The fact that it lists this at all is why we're getting the blank output. Otherwise, rendering it wouldn't even be an option and only the text/html render will be listed.

I haven't looked too deeply into why it doesn't work. I know this plugin uses some ancient libraries which are no longer maintained. Some aren't even the latest versions of those libraries. Many of the libraries come from the nteract project. In this case, the package with the issue is @nteract/transform-dataresource. I will also add @nteract/data-explorer exists, which seems to be its replacement. It too looks abandoned, but it's still 3 years newer. Also, if I recall correctly, the renderer for application/vnd.dataresource+json in the nteract desktop application does work, which I assume is using data-explorer since the project has it as a dependency and imports it here:

https://github.com/nteract/nteract/blob/7516211c27bad1e5a415505b023e2221bc430d8a/applications/desktop/src/notebook/index.tsx#L24

SC-CTS commented 1 month ago

For Deno I use the following workaround display function:

// Strip `application/vnd.dataresource+json` from Jupyter display bundle.
// Workaround for: https://github.com/microsoft/vscode-notebook-renderers/issues/189
export const vscd = async (
  obj: unknown,
  options: { raw: boolean; update: boolean; display_id?: string } = { raw: false, update: false }
): Promise<void> => {
  const bundle = (await (obj as any)[Symbol.for('Jupyter.display')]?.()) ?? {};
  const { 'application/vnd.dataresource+json': _json, ...strippedBundle } = bundle;
  const message_type = options.update ? 'update_display_data' : 'display_data';
  const transient = options.display_id ? { display_id: options.display_id } : {};
  Deno.jupyter.broadcast(message_type, { data: strippedBundle, metadata: {}, transient });
  return;
};

and then wrap the object I want to display in vscd(obj). e.g. using nodejs-polars this will then output the HTML table instead.

I guess the same workaround can be done with other kernels as well.