I am writing an extension where I have some binary data that I want to write to a file. What is the best approach for doing this? I would like for the file to output in the same directory where the opened Notebook is saved.
I can write a server extension and send the binary data to it and write it to disk, but I am guessing there is better way of doing this using the frontend API.
Searching through the code base, I found this:
It is my understanding that there is some way to write arbitrary data to the returned model, but it’s not clear to me if that is in fact the case and if this is the correct approach.
newUntitled approach is a relatively low-coupling way to do it, and will take care of some boilerplate for you.
To get your hands a littler dirtier, have a look at the
save method on that same instance of
You can have a look at it interactively
jupyter lab --expose-app-in-browser, where it will be available off
window.jupyterapp.serviceManager.contents.save. You’ll likely have to use some information from your existing document, and might have to do some encoding yourself.
An even more blunt-force way to do it is code directly against the Jupyter Contents API, but this probably isn’t worth it.
However: if you need this pipe to be particularly high-performance, you might end up wanting to do something custom with e.g. web sockets, as jupyter contents does entail a fair amount of overhead for marshalling data into and out of base64. Jupyter Widgets are actually a fairly good way to do this, as they have already have some techniques for binary buffers, used by e.g. ipywebrtc.