kpeeters / cadabra2

A field-theory motivated approach to computer algebra.
https://cadabra.science/
GNU General Public License v3.0
228 stars 37 forks source link

Pure-Python Jupyter Kernel #207

Closed fjebaker closed 3 years ago

fjebaker commented 3 years ago

Requires

Still needs the cadabra::cdb2python_string wrapped with pybind11. I will highlight in the PR where in the python scripts this is used.

Details

Developed a rudimentary but fully functional Cadabra2 Jupyter Kernel purely in Python, mimmicking the behaviour of the current Xeus implementation, with a few added quality of life improvements, including

Included in this PR is a CMake file, which copies over relevant variables from the Cadabra2 buildchain, and inits the install script. When invoked, the installation uses the pip bundled in python 3.x to install the python package and jupyter kernel, so that it is immediately ready to use when starting a new notebook server. 😊

kpeeters commented 3 years ago

Ok, thanks! I am going to simplify this somewhat now, and write the pybind11 glue.

kpeeters commented 3 years ago

I have merged your code and exposed cdb2python on the cadabra2 module to glue it together. It is in the feature/python-jupyter-kernel branch.

I decided to remove the pip-based installation, as that really does not make sense in the context of the full source: we already install a python package (cadabra2 itself) using CMakeLists.txt, so the jupyter kernel should be installed the same way. Otherwise you potentially end up with two different installation directories (and things flat out break if you do sudo make install, which is what one would normally do on Linux to install this, because then it tries to run pip as root).

It installs fine now, and jupyter notebook can start the kernel, so it's almost there. The only thing that does not work yet is that it does not seem to run anything ;-) I messed up something in kernel.py obviously. If you spot it, let me know, I don't have time anymore tonight, but will look at it again tomorrow.

kpeeters commented 3 years ago

Ok, that was a silly one, I wrapped cdb2python, not cdb2python_string. It works now.

I'll merge this into master tomorrow when I have a fresh brain.

kpeeters commented 3 years ago

A few remaining things:

Other than that, nice work, simple but effective.

fjebaker commented 3 years ago
* Cadabra spits out `\discretionary` tokens in the TeX output which MathJax does not like, should make that configurable dynamically with a kernel option (right now it is a preprocessor thing in `DisplayTeX.cc`).

Adding a filter for such tokens is pretty straight forward -- after the merge, I'll develop the kernel class further and add jupyter-esque magic commands for kernel configuration with sensible defaults.

* Syntax colouring is weird because it uses Python rules even for Cadabra maths expressions (e.g. colouring `\lambda` in a special colour).

So far, I have left the highlighting untouched and just used the out-of-the-box python colouring, but I am going to prioritize updating the syntax highlighting and code completion so that the kernel feels familiar.

Thank you for doing the pybind11 wrapping, and I fully understand your point regarding pip; that was a convenience shortcut on my end! Thank you for tidying that up also 😊

kpeeters commented 3 years ago

I have merged this into master now. Cleaned up the CMakeLists.txt a bit and added your name as author to the kernel help blob and the web site. I have also added two quick filters to remove those \discretionary things from the output, as well as the ~ tie character, which MathJax doesn't like either.

The build process will install this kernel by default unless turned off, as it does not require anything else as build-dependency (and it is sufficiently small that no-one without Jupyter will care). Windows folks will love you for this, as they had no simple way to get a Jupyter kernel otherwise.