Design plotting from Fortran
The original notebook from the Python prototype of LFortran has simple plotting as a proof of concept that it can be done. Now it is time to design it properly.
The best approach seems to be the one taken by xeus-cling and it is described here.
Using the same approach for Fortran would be:
- User will create a custom derived type
type(image)
(whereimage
is just an example) - Function can return it, or a variable of type
type(image)
is left to "display" it in the interpreter - The Fortran kernel looks into the module that defines
type(image)
and looks for a functionmime_bundle_repr
. If it exists, it will be called like this:json = mime_bundle_repr(x)
wherex
is of typetype(image)
. The user implements the correct JSON representation for the custom typetype(image)
in themime_bundle_repr
function. - Jupyter shows the correct representation
A sample mime_bundle_repr
function in C++ is:
nlohmann::json mime_bundle_repr(const image& i)
{
auto bundle = nlohmann::json::object();
bundle["image/png"] = xtl::base64encode(i.m_buffer.str());
return bundle;
}
The advantage of this approach is that there are no LFortran specific APIs here. This is all completely general and can be implemented by other Fortran compilers also.