服务器搭建sage jupyter notebook

官方sagemath

运行下面指令,启用sagemath容器,自动下载镜像

1
docker run -p8888:8888 sagemath/sagemath sage-jupyter

-p8888:8888是端口映射,前一个是容器端口,后一个是主机端口

复制jupyter地址,直接关掉shell(shell已经卡死),完成

适配vscode代码高亮

在vscode中,用以上方法代码不会显示高亮,原因是sagemath的kernel.json里的language没有识别

临时修改

1
docker exec -it festive_noyce bash
1
2
cd /home/sage/sage/local/var/lib/sage/venv*/share/jupyter/kernels
cp -r sagemath sagemath-vscode

打开sagemath-vscode文件夹,里面有kernel.json文件,将里面的"language"后面改成"python”,将"display_name"后面改成"SageMath for VSCode"img

退出,回到vscode就可以发现jupyter的环境有SageMath for VSCode选项,选择它代码显示高亮

制作docker镜像

新建Dockerfile文件,按上述方法找到kernel所在目录,根据目录修改下面内容写入Dockerfile

1
2
3
4
5
FROM sagemath/sagemath
RUN sage --pip install pycryptodome
RUN cp -r /home/sage/sage/local/var/lib/sage/venv-python3.9.9/share/jupyter/kernels/sagemath /home/sage/sage/local/var/lib/sage/venv-python3.9.9/share/jupyter/kernels/sagemath-vscode
RUN echo '{"argv": ["/home/sage/sage/local/var/lib/sage/venv-python3.9.9/bin/sage", "--python", "-m", "sage.repl.ipython_kernel", "-f", "{connection_file}"], "display_name": "SageMath 9.5 for VSCode", "language": "python"}'>/home/sage/sage/local/var/lib/sage/venv-python3.9.9/share/jupyter/kernels/sagemath-vscode/kernel.json

构建镜像

1
docker build -t mysage .

官方sagemath的方法,创建容器