Setting up Jupyter for Macaulay2

1,058 views
Skip to first unread message

Annie Giokas

unread,
Jan 20, 2022, 8:31:59 PM1/20/22
to Macaulay2
Hi everyone, 

I tried using Jupyter for Macaulay2 last year and it was really useful for presentations and making notes. 

Here are the instructions, tell me if it works for you:

Installing Jupyter

Install Anaconda on wsl which will manage python versions and jupyter. This is recommended since this will allow the browser to work correctly since using jupyter separately caused some issues when using the browser, such as notebook files not opening. You can install anacdonda by following the instructions here: https://mas-dse.github.io/startup/anaconda-ubuntu-install/ or here: https://docs.anaconda.com/anaconda/install/linux/ https://jupyter.org/install

After you install anacdonda on wsl server:

conda install -c conda-forge notebook

Update your package manager:

$ sudo apt update

Then install pip3:

$ sudo apt install python3-pip

Install jupyter using apt again:

$ sudo apt install jupyter

Now we need to install the Macaulay2 kernel for jupter to recodnize the Macaulay2 code and make notebooks using that kernel available. Make sure you run this as root (Add sudo at the beginning), because it tries to put files in system diretories:

$ sudo pip3 install macaulay2-jupyter-kernel $ sudo python3 -m m2_kernel.install

Then to make a new notebook, type:

$ jupyter notebook

If you fixed the browser to work on wsl, it should redirect you to your chosen browser in Windows. Then you should be able to make an M2 file in jupyter.

You can also try using the VS code extension for jupyter: https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter

Youngsu

unread,
Mar 27, 2022, 2:37:38 PM3/27/22
to Macaulay2
Thank you for the information. I could add the kernel, but I get the "KERNEL ENFORCED TIMEOUT" message for some "large" computations. Do you know how to resolve this?

o8 = [KERNEL ENFORCED TIMEOUT]

It only happens in the JupyterLab/Notebook, but not in the terminal. 

Neil Ramsamooj

unread,
Dec 28, 2023, 8:13:27 AM12/28/23
to Macaulay2
From the following picture from the demo, it seems that timeout can be set:
Screenshot 2023-12-28 022732.png

Youngsu

unread,
Jan 17, 2024, 12:52:25 AM1/17/24
to Macaulay2
Thank you for pointing it out. The JupyterKernel is implemented by https://github.com/rz-c/Macaulay2-Jupyter-Kernel/. I tried to contact the author and ended up modifying the package myself. I wish I had known this magic. 

Thank you

mike stillman

unread,
Jan 29, 2025, 11:25:30 AMJan 29
to Macaulay2
How did you end of modifying the Macaulay2 jupyter kernel package?  We would like to get a Macaulay2 jupyter kernel back up and running.  

Thanks,

-Mike

Doug Torrance

unread,
Jan 29, 2025, 11:56:33 AMJan 29
to Macaulay2
The following works for me to get the M2 Jupyter kernel working with the latest versions of Jupyter:

Is just removes a few lines from the install script that were broken by the changes to extensions in Notebook 7.  This breaks syntax highlighting, but everything else seems to work.

Youngsu

unread,
Jan 29, 2025, 6:30:16 PMJan 29
to Macaulay2
Hi Doug and Mike,

The issue was the timeout parameter, which was set to 4 as default; https://github.com/rz137/Macaulay2-Jupyter-Kernel/issues/8. For classroom usage, it is not a problem but for research, it is inconvenient having to add the timeout magic. I ended up changing it to a large number, e.g., here. This is part of a Dockerfile available here. I haven't checked it for a while and feel free to let me know if you need anything modified. 

Thank you,
Youngsu 

Taylor Ball

unread,
Jan 29, 2025, 8:52:20 PMJan 29
to maca...@googlegroups.com
Would it be difficult to get syntax highlighting using the macaulay2.js file?

--
You received this message because you are subscribed to the Google Groups "Macaulay2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to macaulay2+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/macaulay2/0d9cc3ed-109e-46ce-a4ca-7f5a8627107bn%40googlegroups.com.

Youngsu

unread,
Jan 30, 2025, 4:43:53 PMJan 30
to Macaulay2
I'm no expert in this (and I mostly use Jupyter Lab instead of Notebook), so I cannot say for sure but there must be a way. Below is my understanding.

This package uses codemirror, a jupyter lab extension for highlighting. In Doug's modified code, it was removed. I could replace it with the following snippet, but it did not bring the feature back. I could look into the package more closely or try to write one from scratch.

#from notebook.nbextensions import install_nbextension
from jupyter_server.extension.application import ExtensionApp
...
...
...
    import subprocess

    subprocess.run([
        "jupyter", "server", "extension", "enable", "m2-mode",
        "--user"
        ], check=True)

mike stillman

unread,
Feb 4, 2025, 7:47:40 PMFeb 4
to Macaulay2
One problem I found with the newest notebook seemed to be the ability to stop line wrapping (and add a horizontal scrollbar) to output cells.  Arthur Ryman found the following that does the trick in jupyter lab, at least:  in the virtual env you use to create the jupyter extension, do:

```
pip install jupyterlab_linewrapcellouput
```

Here is the full snippet for creating a Jupyter kernel that works for me.  In a standard jupyter notebook or lab this doesn't do colorization yet.  However, using this in vscode seems to work and uses colorization from the macaulay2 extension (note: we need to update that) (second note:  the horizontal scrollbar and line wrapping was easy to fix in the vscode with jupyter case without adding in this last pip install line above.  Still, it works in vscode whether the jupyterlab_linewrapcellouput has been installed or not.

```
# this now seems to work for me, after Doug's modifications, and Arther's suggestion
python3 -m venv .
source bin/activate
git clone https://github.com/d-torrance/Macaulay2-Jupyter-Kernel -b notebook-7
cd Macaulay2-Jupyter-Kernel
pip install .    
python3 -m m2_kernel.install
pip install jupyterlab_linewrapcellouput
       
jupyter notebook
```

It could probably be done without doing git clone, using
```
```
But I haven't tried that yet.

Finally, Radoslav (who wrote the original version), is quite busy currently, but has expressed some interest in helping work on this, if he can find the time.

Best,

Mike
Reply all
Reply to author
Forward
0 new messages