Skip to content

Altair charts in ipywidgets.Output #1948

@theeldermillenial

Description

@theeldermillenial

I'm trying to render an Altair chart in an ipywidget.Output widget. Altair charts properly render with Voila, but not nbconvert.

Here is a test notebook I created to replicate the issue.

# test.ipynb
import ipywidgets as widgets
import pandas as pd

import altair as alt

source = pd.DataFrame({"category": [1, 2, 3, 4, 5, 6], "value": [4, 6, 10, 3, 7, 8]})

output_donut = widgets.Output()

with output_donut:
    donut = alt.Chart(source).mark_arc(innerRadius=50).encode(
        theta=alt.Theta(field="value", type="quantitative"),
        color=alt.Color(field="category", type="nominal"),
    )
    donut.display()

output_donut

If I run the cell, the Altair chart displays properly.

Rendering with voila renders the chart:

voila test.ipynb

When I run nbconvert, it looks like the <div> along with the <script> gets rendered as text. I ran the following to try and somewhat replicate what Voila does.

jupyter nbconvert --to HTML --no-input --execute test.ipynb

I have read through a few related issues, including some weird display ordering. I haven't found anything that works properly. It looks like the other solutions might have worked in older versions of nbconvert. Here are the versions I'm using for this example.

altair==4.2.2
nbconvert==7.2.9
pandas==1.5.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions