Imagining “JupyterLab Magic Tags”: Tag Based Code Cell Execution Modifiers in JupyterLab

In Pondering — Admonition Tags for JupyterLab Markdown Cells? I finally for round to describing a feature I’d like to see in JupyterLab, specifically the ability to tweak the execution of markdown cells based on cell tags. I’ve already managed to tweak the style of cells based on cell tags (a missing feature which was one of the blockers I have had on adopting JupyerLab for several years) so this represents the next step.

In this post, I thought I’d make a note of another tag based feature I’ve wanted for a couple a years: the ability to modify cell execution based on cell tag metadata.

This might sound a bit odd, but it’s really just another take on cell magics, at least as far as the user might be concerned.

I make use of IPython magics block cell heavily to modify the behaviour of code cells, often in context of using the magic to invoke a Python function that will execute a function over content contained in the cell. This essentially allos you to define a sort of custom code cell type. A good example is provided by the catherinedevlin/ipython-sql extension which installs a sql magic that lets you connect to a database and then run SQL queries directly from a magicked code cell:

This is fine insofar as it goes, but I’d quite like to be able to abstract that a bit further into the UI, and use a cell tag, rather than a magic, to modify the cell behaviour.

Tagging a cell with ipython-magic-sql would have the same effect as adding %%sql to the cell. (Yes, I know there may be an issue with handling magic parameters; that’s left as a on open question for now…) A tagstyle could automatically render the cell in a way that highlights it has been magicked, making it clear in the UI that modified behaviout on the cell is to be expected.

In terms of handling the modified execution, one approach I can imagine would be to define custom code cell handler, similar to the custom markdown cell handler defined in the agoose77/jupyterlab-imarkdown extension (see also this related (as yet unanswered) query about Creating a Custom JupyterLab Notebook Markdown Cell Renderer; maybe I should try Stack Overflow again…).

A jupyterlab-magic-tag-template extension might provide one way of helping end-users create their own magic tags. For a magic with out paramteers, we might image a simple definition file:

tag: SQL-MAGIC-TAG
block_magic: %%sql
package: ipython-sql

Note that in the case of the ipython-sql package, we would elsewhere have had to define the sql connection. Alternatively, this could be pre-loaded and enabled by the extension, possibly even in a separate kernel. If the extension supported user configuration settings, that might also provide a route for setting global magic parameters and having them added to the magic command whenever it is called.

Tagging a cell with SQL-MAGIC-TAG would then:

  • cause the cell to have slightly different style applied; this might be as simple as a coloured border, which could have a default value that is over-riden via a config setting;
  • when a cell is executed, the contents of the cell are prefixed by the magic invocation;
  • cell outputs are rendered as per normal.

It’s not hard to imagine tags also being used to support an element of polyglot language execution behaviour; for example, magic can be used to modify a cell to allow the execution of R code in an IPython notebook. But why not just let the user use a cell tag to invoke that behaviour?

PS I’m happy for folk to tell me why they think this is a ridiculous idea..! Feel free to post in the comments…

Author: Tony Hirst

I'm a Senior Lecturer at The Open University, with an interest in #opendata policy and practice, as well as general web tinkering...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: