jupyter-widgets / ipydatagrid

Fast Datagrid widget for the Jupyter Notebook and JupyterLab
BSD 3-Clause "New" or "Revised" License
579 stars 51 forks source link

Unable to render outside of a notebook context #279

Open cpsievert opened 2 years ago

cpsievert commented 2 years ago

The suggested way of getting standalone HTML from ipywidgets doesn't appear to work:

import pandas as pd
import numpy as np
import json

from ipydatagrid import DataGrid, TextRenderer, Expr

n = 50_000

df = pd.DataFrame(
    {
        "Value 1": np.random.randn(n),
        "Value 2": np.random.randn(n),
        "Value 3": np.random.choice([True, False], n),
        "Value 4": np.random.choice([True, False], n),
    }
)

# This returns the unicode value for specific font-awesome icons,
# check-out this link for more icons:
# https://fontawesome.com/v4.7.0/cheatsheet/
def bool_render_text1(cell):
    if cell.value > 0:
        return "\uf00c"  # Check
    else:
        return "\uf00d"  # Cross

def bool_render_text2(cell):
    if cell.value > 0:
        return "\uf111"  # Circle
    else:
        return " "

def bool_render_text3(cell):
    if cell.value:
        return "\uf164"  # Thumb up
    else:
        return "\uf165"  # Thumb down

def bool_render_text4(cell):
    if cell.value:
        return "\uf118"  # Smile
    else:
        return "\uf119"  # Frown

def bool_render_color(cell):
    if cell.value > 0:
        return "#2fbd34"
    else:
        return "#b82538"

common_args = {
    "font": "bold 14px fontawesome",
    "text_color": Expr(bool_render_color),
    "horizontal_alignment": "center",
}

renderers = {
    "Value 1": TextRenderer(text_value=Expr(bool_render_text1), **common_args),
    "Value 2": TextRenderer(text_value=Expr(bool_render_text2), **common_args),
    "Value 3": TextRenderer(text_value=Expr(bool_render_text3), **common_args),
    "Value 4": TextRenderer(text_value=Expr(bool_render_text4), **common_args),
}

#display(df)

w = DataGrid(df, base_row_size=30, base_column_size=150, renderers=renderers)

from ipywidgets.embed import embed_minimal_html

embed_minimal_html("index.html", w)
$ python3 -m http.server
Screen Shot 2021-12-03 at 11 12 15 AM
ibdafna commented 2 years ago

Hello šŸ‘‹ @cpsievert and welcome to the ipydatagrid community!

Looking at your error above, it seems like you have an issue with the ipytree package. Can you check again and let me know?

Thanks!

cpsievert commented 2 years ago

Whoops, I included the wrong screenshot, I just updated it to the correct one :)

ibdafna commented 2 years ago

@cpsievert I am going to take a look at this in the first week of January. Happy new year! šŸ˜„

joseberlines commented 2 years ago

Since ipytree was mentioned I allow myself to ask if ipytree is also a blommberg based create package? Thanks