CommandLinker? Proper syntax for command arguments?

I can use the CommandLinker (How to drive jupyter lab from a notebook using a jupyter lab extension · Issue #5789 · jupyterlab/jupyterlab · GitHub and How to call three commands with one button?) to run commands from javascript+html in notebook cells. My problem is that I am seeing the following error when I try to pass arguments to the command:

Uncaught SyntaxError: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data
    _evtClick http://localhost:8889/static/lab/jlab_core.c1feae0690cd2229a88c.js?v=c1feae0690cd2229a88c:1
    handleEvent http://localhost:8889/static/lab/jlab_core.c1feae0690cd2229a88c.js?v=c1feae0690cd2229a88c:1
jlab_core.c1feae0690cd2229a88c.js:1:152248

Things I have tried:

data-commandlinker-args="{args:{tableID:\'it_1719608870053\'}}"
data-commandlinker-args="{tableID:\'it_1719608870053\'}"
data-commandlinker-args="{tableID:'it_1719608870053'}"

plus other variations on quotations around the arguments in the -args string. The last one seems to work with JSON.stringify(), but the error is the same for all of them. I am trying some workarounds for my use case (rewriting the offending command so that it does not need arguments), but would be happy to get the arguments working with the CommandLinker.

Any hints?

Thanks,
Jonathan

Additional related question. Is the issue that line 183 in commandlinker.ts is:

          args = JSON.parse(argsValue);

rather than:

          args = JSON.parse(JSON.stringify((argsValue));

?
If so is there a way to include arguments in the form of valid JSON when the html format requires “” around the value?

Thanks,
Jonathan

The arguments that you are passing are not a valid JSON. JSON is a very strict format, it requires double quotes for keys and string values and no trailing commas. The followign ones should work:

data-commandlinker-args='{"tableID": "it_1719608870053"}'
data-commandlinker-args="{\"tableID\": \"it_1719608870053\1"}"

It is JSON.parse because the value of data-commandlinker-args is a string already.

Thank you. I was able to get your first example working with careful escaping for my use case. My specific use case is passing a javascript string wrapping what I want in the python display(HTML(<html string>)) call to a code cell. The syntax that worked was:

stringtoinsert = 'display(HTML(\'<button 
data-commandlinker-command="LockLabels:jupyter-inputtable" 
data-commandlinker-args=\\\'{"tableID":"'+ID+'"}\\\'>
Button Text</button>\'))';

(Editted to fix missing \'s for escaping)

Your second example assuming \1" is actually \" does not work for me (at least in FireFox). However, counter-intuitively the following does (although I could not come up with an escape sequence allowing me to pass it from a javascript string):

data-commandlinker-args="{"tableID": "it_1719608870053"}"
1 Like