Programmatically execute command in kernel

I am building a JupyterLab extension to automate deployments of Jupyter Notebooks into a cloud environment. I would like to have a button in my panel extension that, when pressed, will execute a command in the running kernel of the active notebook.

The behavior would be the same as having a cell with !command, but run programmatically when the button is pressed. So I wish to either create and the run programmatically a new cell (and then delete it) or execute directly a command in the kernel. Is this possible? Thanks!

Yes, it’s possible. You can send code snippets for execution to the kernel, no need for a cell. If it’s an IPython kernel with ! support, you can send the snippet with a prefixed !. For other languages, you’ll have to wrap the shell command in suitable code that starts a shell for execution.