I’ve been working on PRIK (Python Runtime Interop Kit), a tool for generating native Python bindings for Fortran and C.
I’ve recently added an IPython/Jupyter integration, so native code can be compiled directly from a notebook using a cell magic.
For example:
%load_ext prik.jupyter
%%fortran
module example
contains
real(8) function square(x)
real(8), intent(in) :: x
square = x * x
end function square
end module example
The generated module can then be used directly from Python:
import example
example.square(5.0)
The complete example can also be run online in Google Colab:
This made me wonder about Jupyter’s approach to multi-language workflows.
Jupyter already supports many languages through different kernels. In this case, however, the notebook remains a Python notebook: PRIK simply allows Fortran or C code to be compiled and loaded from within the Python kernel.
Is this a reasonable pattern for Jupyter, or is there an existing mechanism in the Jupyter ecosystem intended for this kind of workflow?
I’m also interested in whether this could eventually be integrated more deeply into JupyterLab, rather than remaining an IPython cell magic.
Hopefully, someone with more knowledge of the multi-language aspects will comment on your bigger questions.
Two minor things:
You have !pip install as the second code line. In modern Jupyter, it really should be %pip install. Please see here. Even Google added support for allowing the better all-round magic command version.
Colab is your demo offering, which is not current Jupyter in actuality these days. Since you are asking about the ‘Jupyter ecosystem’ in your post, you’d be more in line with that ecosystem using the Jupyter’s community MyBinder’s service to offer a demo. For example, some may wonder if your magic only works in current Colab. This way they can see it works in Jupyter already. You could still to continue to offer both ways to run the demo since you have it worked out.
Along the line of that last suggestion, your demo does already work on the MyBinder-system with some adjustment of the first code cell. To explore this, I took advantage of the fact I have a repo that already results in MyBinder-served with gfortran installed here. If you’d like to see: go there to that repo and click on the 'launch binder' badge you see. Or just click this link to launch a session directly. When that modern Jupyter session starts up, if you used my direct link, you’ll already be in JupyterLab and set to go with the drag-and-drop step below in order to upload your demo .ipynb file to the remote system. If you didn’t use my direct link and launched from the pymol-binder repo, it is easier to change to JupyterLab by clicking the ‘JupyterLab open’ icon to the right of the kernel information that will show up in the upper right side just above the notebook.
Then in the JupyterLab interface, toggle the file browser panel to be visible by clicking on the ‘folder’ icon on the left sidebar. Click-and-drag from your local system your quickstart.ipynb file into the file browser, and release your mouse button when the file browser panel shows a gray bar around to upload your demo .ipynb file to the remote system.
Obviously, the drag-and-drop step wouldn’t be necessary once you make a repo that will launch with the proper things installed because your notebook will be there, and you can use the URL to direct what notebook to open like you’ll see I do in other examples referenced below. I’m just suggesting this for a demo to prove all this can already be done in the MyBinder system, and you can use my configuration files as somewhat of a guide.
This is the first code cell as I ran your demo notebook quickstart.ipynb there just now to test things:
Pro-tip: for keeping it working with both MyBinder and Colab, it probably would be easier to put all the MyBinder-related configuration items in a specially-named binder sub-directory like I do here and here. See here for more about that.
You could always have two different demo notebooks if you are going to have both offerings.
Thanks! this is really helpful. I’m new to this so I wasn’t aware of the distinction between Colab and the broader Jupyter ecosystem, so the Binder suggestion is especially useful.