Can't get data back from ipyaggrid after register_widget()

83 views Asked by At

I'm using ipyaggrid, which seems to be a great widget. It seems that I can render the widget, and even update the data from the python process. But I can't seem to get the state back.grid.grid_data_out() should return a dictionary with the data from the UI back to the python process.

import json
import pandas as pd
import numpy as np
import urllib.request as ur

from ipyaggrid import Grid
from shiny import App, ui
from shiny import module, ui, render, reactive, event, App
from shinywidgets import register_widget, output_widget, reactive_read

app_ui = ui.page_fluid(
    ui.tags.p("grid_data_out() is always empty!"),
    ui.row(
        ui.column(4, output_widget("grid")),
        ui.column(4, ui.output_text_verbatim("txt"))
    )
)

def server(input, output, session):

    url = 'https://raw.githubusercontent.com/bahamas10/css-color-names/master/css-color-names.json'
    with ur.urlopen(url) as res:
        cnames = json.loads(res.read().decode('utf-8'))
        
    colors = []
    for k in cnames.keys():
        colors.append({'color':k, 'value':cnames[k]})
        
    colors_ref = colors[:]
    
    css_rules="""
        .color-box{
          float: left;
          width: 10px;
          height: 10px;
          margin: 5px;
          border: 1px solid rgba(0, 0, 0, .2);
        }
    """    
    columnDefs = [
        {'headerName': 'Color', 'field':'color', 
         'pinned': True, 'editable': True},
        {'headerName': 'Code', 'field':'value', 'editable': False, 'cellRenderer': """
            function(params){
                return `<div><div style="background-color:${params.value}" class='color-box'></div><span>${params.value}</span></div>`
          }"""}
    ]
    
    gridOptions = {
        'columnDefs':columnDefs,
        'enableFilter':'true',
        'enableSorting':'true',
        'rowSelection':'multiple',
    }
    
    color_grid = Grid(
        width=400,
        height=250,
        grid_data=colors,
        css_rules=css_rules,
        grid_options=gridOptions,
        sync_on_edit=True,
        sync_grid=True, #default
        export_mode = 'auto'
    )
    
    register_widget("grid", color_grid)

    @output
    @render.text
    def txt():
        color_grid.get_grid()
        return(str(color_grid.grid_data_out.keys()))

app = App(app_ui, server)

I tried varying the arguments to the grid: sync_on_edit=True, sync_grid=True, export_mode = 'auto', but nothing seems to work. grid_data_out() just returns an empty dict.

0

There are 0 answers