Passing arguments using commands.execute

I am trying to address a specific use case:

Ask:
show a custom HTML in cell output for keyword.

Implementation:
created a custom React widget and bound it to python class similar to ipywidgets sample.
Lets call it example widget, so now I am able to call it by adding below steps in plugin’s execution method in frontend:
array = [‘sample1’, ‘sample2’]
INotebookTracker.activeCell.model.sharedModel.setSource(
from myExtension import exampleWidget exampleWidget(value=[${array}])
)
commands.execute(‘notebook:run-cell’)

The value of array is dynamically generated from a service call.
Initial Error:
exampleWidget(value=[sample1,sample2])
^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

question: For my use case, Is my approach appropriate? (any feedback on this solution is welcome)

That one lacks quotes? Is that what you actually run or do you run:

exampleWidget(value=['sample1','sample2'])

when you run it ‘manually’? They aren’t equivalent.

When I manually run the command and pass array of string as input it works.

Edit:
It might be a issue with my syntax itself, I need to test it. I’m still curious about question 2.

I’ll update the post once I test my theory and possibly solve questions 1

Edit2:
Thanks for responding @formightez
turns out, it was syntax error. Now only question I have is to get insights/feedback from Jupyter community on my solution to the use.

I meant by ‘They aren’t equivalent.’ that exampleWidget(value=[sample1,sample2]) you get when you try with the code and exampleWidget(value=['sample1','sample2']) that I assume you use when manual, aren’t equivalent.

Anyway, I guess you sorted the syntax error.

At this point, what is the solution? You fixed something and didn’t post it.

Good point, I’ll add it back to the POST, It might benefit any other lost soul.

for my fix:

It is syntax error, in ts when using template literals along with interpolation, we need to wrap objects or arrays in string

here array should be JSON.stringify([‘sample1’, ‘sample2’]),

INotebookTracker.activeCell.model.sharedModel.setSource(
from myExtension import exampleWidget exampleWidget(value=[${JSON.stringify(['sample1', 'sample2'])}])
)
This fixes the syntax error.

That being said,
I wanted to know if the approach I took is the right one. What I wanted to implement is a hook to cell input and directly manipulate the cell output region.

For example:

  1. we have cell A of type RAW, a custom Jupyterlab extension will check all the inputs user types in this cell
  2. user enters a text: ‘start example’
  3. Our extension will have a logic to render exampleWidget as this cell’s output for this text.

I couldn’t find a way to achieve point 3 directly through INotebookTracker or other classes, so I created a workaround which would

  1. create a new cell under this cell
  2. add the text from myExtension import exampleWidget exampleWidget(value=[${array}]) to the new cell’s input
  3. executes this cell to render ExampleWidget
  4. hide the new cell’s input area.
1 Like