Is there a hook or signal when the tab closes or goes out of focus?


I have a mime renderer that allows the user to modify the displayed data but all of the user’s changes are reverted when the user focuses on another tab and then refocuses on the data tab. I know this happens because renderModel is called whenever the tab comes into focus which re-reads the data from the file and reverts all of the changes.

I want to save the file when the user changes or closes the tab so that when they refocus the changes are persistent.

Is there an event when the user changes tabs or closes the tab that we can listen for in typescript? Or would it be better to implement saving on a timer, similar to a notebook?

Thanks in advance for your help.

Hi @makaylas, you can listen to the signal to know when a new tab is focused. I suppose you could also save the changes whenever the user edits the model as well (with possibly some throttling). Are you using the setData hook for saving the relevant changes?

Hi Ian,

Thanks for your response! I will definitely look at

Yes, I am using setData to save.

@ian-r-rose, I’m having trouble getting to work as shown in this application level plugin or finding another mime-renderer that uses it. I also looked into IMimeDocumentTracker but I can’t figure out how to use that either.

Could you point me to a mime-renderer example that uses or more beginner friendly documentation on it?


Oh, shoot, you’re absolutely right @makaylas, my mistake. The rendermime plugin interface is deliberately simplified to be an easier and more stable option for extension developers. As such, it doesn’t actually have access to the application shell.

Another way to accomplish this (which I probably should have suggested in the first place) is to hook into the onBeforeHide handler on your widget. This is called (as the name suggests), before the widget is hidden, and you can use it to do some extra work before a rerender occurs.

An example of this is in the PDF rendermime extension, which uses this to discard URL fragments:

@ian-r-rose Thanks!

I got the hook working but it seems that setData only makes a temporary instance of the file and that the user has to save from the JupyterLab interface, either by a shortcut or the drop-down menu.

Is that the intended behavior of setData or am I missing a step to actually save the file to disk?

Thanks again

setData is only intended to change the in-memory representation of the document (this is because not all mime renderers are actually backed by a file on disk, such as the outputs in a console). So it doesn’t actually save the data, nor does it provide a hook to do so.

Is there a reason that it is not good enough to change the data in memory, and then let the user/autosave save it whenever?

Thanks for clarifying what setData does.

It would be enough to save in memory, if it were also autosaving to disc but we aren’t observing that behavior.

How would we implement autosave? That could solve our problem.

Autosave is a configuration settable by the user. It should be available via the settings menu, as well as in the advanced settings editor. It should default to on, is it not doing that?