How to custom cell

I want to custom my own cell and implment it run logic. But I don’t know how to do it.
Could you give me a demo or a doc to implement it?

3 Likes

We don’t have very comprehensive docs for this right now. (Perhaps you could be the one to write a tutorial?)

Basically, you’ll need to write a JupyterLab extension that provides your custom cell class to the notebook. You can do this by providing an extension that overrides the default cell provider at https://github.com/jupyterlab/jupyterlab/blob/adf2f3f421906bfb61255fa74f783d33d6def7f6/packages/notebook-extension/src/index.ts#L277-L289. You’d probably inherit from the notebook panel content factory at https://github.com/jupyterlab/jupyterlab/blob/adf2f3f421906bfb61255fa74f783d33d6def7f6/packages/notebook/src/panel.ts#L294-L305 (which itself inherits from the notebook content factory at https://github.com/jupyterlab/jupyterlab/blob/adf2f3f421906bfb61255fa74f783d33d6def7f6/packages/notebook/src/widget.ts#L774) and override the createCodeCell method.

We’re happy to answer questions you may have.

Is this mean I can change exist cell behavior rather than create a new cell ?
So, I can write a JupyterLab extension to find code cell then change it behavior?

The extension I mention above would actually be the one supplying new code cells to JupyterLab wherever they were needed.

Hey @jasongrout I want try and do this too and will try and follow your steps listed, but just wondering if there any tutorials or docs that have been created in the last few months? ( I see the original post was from 8 months ago)

Having sifted through all the jupyterlab documents in the past 2 weeks, the most useful code examples so far for me has been https://github.com/jtpio/jupyterlab-extension-examples

Thanks
Raks

1 Like

Oh wow, that looks like a really nice resource!

I don’t know of other comprehensive tutorials covering this case that have been written.

1 Like

Any luck on this? I am trying to do the similar thing.

Hello, any tutorial about adding new cell type for jupter notebook?

I am also still looking for adding new Cell Type, any updates?

Maybe one of the core devs can comment, but as far as I can tell this is something that would be very challenging to do.

The cell types are relatively hard coded (as ‘code’, ‘markdown’ and ‘raw’) at this point, so it would require a lot of work to loosen this up.

For example, the context menu in which you select a given cell type is hard coded:

Similarly the method on the Notebook class that actually creates the Cell widget (Notebook._insertCell) only works for the three cell types:

As this is a private method, at present it would be hard to create a subclass of Notebook to override this behaviour.

To make this easy I think would take a fairly significant overhaul of the way the Notebook interacts with the contentFactory:

And would be very hard to achieve with an extension.


I think depending on what you want, you might be able to achieve something similar by adding a custom mimetype and renderer:

Or if your widget doesn’t need to actually be part of the notebook file, there is a way to insert new widgets into the NotebookPanel:

https://jupyterlab.readthedocs.io/en/stable/extension/notebook.html#adding-a-widget-to-the-notebook-header