简体   繁体   中英

Jupyter: how to implement image viewing functionality in new kernel

I'm currently playing with Cling(C++ interpreter) and it's jupyter kernel and I would want to display an image inside Jupyter notebook using some kind of C++ function similar to IPython.display. What would be the best approach for that?

Update

Thanks for replies, I'm currently digging into both options to check what has a somewhat stable release on Windows and will be back probably next week(a lot of other work at hand right now) with comments on each of it.

The feature need to be implemented at the kernel level, that is to say, if cling itself does not give you the escape hatch to do it, you likely cant.

Technically under the hood, when you display something, cling will send a display_data message, which contain a mimebundle (a mapping form mimetype to data), so you need to hook into the kernel itself to do so.

I don't believe it is in Cling yet, though the QuantStack folks did a demo recently at JupyterCon (video not online yet) where they showed widgets in their Xeus-Cling kernel.

I would thus strongly encourage you to try xeus-cling, and ask the developers there (open an issue on GitHub, or try the Gitter channel they are responsive).

Would this do what you need? From cling/tools/Jupyter/Kernel.cpp:

/// Push MIME stuff to Jupyter. To be called from user code.
///\param contentDict - dictionary of MIME type versus content. E.g.
/// {{"text/html", {"<div></div>", }}
///\returns `false` if the output could not be sent.
bool pushOutput(const std::map<std::string, MIMEDataRef> contentDict) {

I recommend you check out the rich mime type rendering feature of the xeus-cling kernel.

For any type, you can override mime_bundle_repr function. It is picked up through argument-dependent lookup and your object will be magically displayed inline in the Jupyter notebook.

在此处输入图片说明

xeus-cling also support Jupyter interactive widgets and quick documentation.

You can try it live online by clicking on the "Launch Binder" button at the top of the README page for the GitHub repository.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM