Passing an arbitrary dict to a typescript extension


looking in the bokeh source code for a while now and after many attempts, I am still unable to have a working example with a dict parameter to be passed to a typescript extension. It always complain, no matter how !

No problem with cds, but my dict struct is key string and value array. Arrays are number or strings of various length depending on the key.

Minimal running code below (uncommenting comments does NOT work)

I would so super appreciate if somebody would know how to do it, I had all my extensions working in coffeescript times and trying to catch up… Thx !

TSCODE = """
import * as _ from "underscore"
import * as p from "core/properties"
import {Line, LineView} from "models/glyphs/line"
import {ColumnDataSource} from "models/sources/column_data_source"

export class MyLineView extends LineView {
  model: MyLine
  _map_data(): void {
        const cds =
        console.log('cds y',['y'])

        //const dic =

        const ynorm = 1.0
        const y = []
        for (var i = 0; i < this._y.length; ++i) { y.push(this._y[i]/ynorm) }
        [,] = this.renderer.coordinates.map_to_screen(this._x, y)

export namespace MyLine {
  export type Attrs = p.AttrsOf<Props>
  export type Props = Line.Props & {
    dummy_cds: p.Property<ColumnDataSource | null>
    //dummy_dic:     p.Property<{[key: string]: number[]}>

export interface MyLine extends MyLine.Attrs {}

export class MyLine extends Line {
    properties: MyLine.Props
    __view_type__: MyLineView
    constructor(attrs?: Partial<MyLine.Attrs>) {
    static init_MyLine(): void {
        this.prototype.default_view = MyLineView
        this.define<MyLine.Props>(({Ref,Nullable}) => ({
            dummy_cds:     [ Nullable(Ref(ColumnDataSource)), null ],
            //dummy_dic:     [ Dict(String), {} ],

from import Float,Bool,Instance,Enum,String,Dict,Any
from bokeh.models import Line
from bokeh.util.compiler import TypeScript
from bokeh.models import Line,ColumnDataSource
from bokeh.plotting import figure,curdoc

class MyLine(Line):
    __implementation__ = TypeScript(TSCODE)
    dummy_cds = Any()
    #dummy_dic = Any()

cds = ColumnDataSource(data=dict(x=[1,2,3],y=[1,2,3]))
p = figure()
l = MyLine(x='x', y='y', dummy_cds=cds)
#l = MyLine(x='x', y='y', dummy_cds=cds, dummy_dic=dic)
p.add_glyph(cds, l)

The relevant changes:

const dic =

const dic = this.model.dummy_dic

dummy_dic: p.Property<{[key: string]: number[]}>

dummy_dic: p.Property<{[key: string]: string[] | number[]}>

this.define<MyLine.Props>(({Ref,Nullable}) => ({
     dummy_dic:     [ Dict(String), {} ],
this.define<MyLine.Props>(({Dict,Or,Array,String,Number}) => ({
 dummy_dic:     [ Dict(Or(Array(String), Array(Number))), {} ],

dummy_dic = Any()

dummy_dic = Dict(String, Either(List(String), List(Float))

May need to replace number[] | string[] with (number | string)[] (in all three locations accordingly) if that’s the type you actually want.

well still the same error, somehow it seems that Dict wants two arguments in my extension (contrary in scripts in bokeh repository I took as example), one for item type ? frustrating !

error TS2554: Expected 2 arguments, but got 1.
49 dummy_dic: [ Dict(Or(Array(String), Array(Number))), {} ],

error TS2554: Expected 2 arguments, but got 1.

So, the code I showed is for bokehjs 2.3 (dev), but in 2.2 the API was different (it’s an experimental API). You can either use Struct(Or(Array(...)) or just Any (it doesn’t matter, there just won’t be any validation), e.g.:

this.define<MyLine.Props>(({Any}) => ({
     dummy_dic:     [ Any, {} ],