3-manifolds / Sage_macOS

SageMath as a macOS application bundle.
152 stars 15 forks source link

Can't launch Jupyterlab successfully with `sage -n jupyterlab` #59

Closed Deeperand closed 9 months ago

Deeperand commented 1 year ago

When I first time run sage -n jupyterlab, I was suggested to run %pip install jupyterlab in Sage REPL to install Jupyter Lab for Sage. After I run that command, and run sage -n jupyterlab again, I found I didn't enter the usual page of Jupyter Lab but met a "JupyterLab Error" page, and it suggested me "Please run jupyter lab build or use a different app directory".

In fact, there are two problems.

  1. Since I've installed Jupyter Lab globally, if possible to use global version Jupyter Lab when call sage -n jupyterlab?
  2. I do know jupyter lab build is necessary to install Jupyter Lab. However, when I direct run it in command line, it is related to global Jupyter Lab, not the Jupyter Lab for Sage. How can I run jupyter lab build for Sage's Jupyter Lab? I tried to run %jupyter lab build in Sage REPL, but it seems such syntax is invalid.
culler commented 1 year ago

I am assuming you are using the Sage 10.1 version of the app.

Sorry, those instructions about installing Jupyter Lab should have been removed from the sage command line script. The 10.1 app includes its own installation of Jupyter Lab. Also. it seems that we need to update how the -n option is handled by the command line script.

If you have installed your own Jupyter Lab it should offer you the option of using a Sage kernel. However, in order for that to work you need to have installed the optional package which is included with the app. The package adds links to the sage kernel in the /uar/local directory where Jupyter Lab looks for kernels. You will not be able to start your Jupyter Lab with the -n option, however.

Note that you can open the built-in Jupyter Lab from the launch gui after clicking the app icon.

culler commented 1 year ago

Looking a bit more closely, I find that the message about running jupyterlab build is coming from Jupyter Lab itself, not from sage. It is inappropriate for this situation, since the assets do exist within the sage app, but apparently not where the jupyterlab expects to find them. I think this can be corrected by setting an environment variable, which should happen automatically but can probably be done manually as well. I will look into this.

Deeperand commented 1 year ago

Looking a bit more closely, I find that the message about running jupyterlab build is coming from Jupyter Lab itself, not from sage. It is inappropriate for this situation, since the assets do exist within the sage app, but apparently not where the jupyterlab expects to find them. I think this can be corrected by setting an environment variable, which should happen automatically but can probably be done manually as well. I will look into this.

Thanks for your check! I think (perhaps) the full error message can provide some help

"JupyterLab Error JupyterLab application assets not found in "/Applications/SageMath-10-> 1.app/Contents/Frameworks/Sage.framework/Versions/10.1/local/var/lib/sage/venv-python3.11.1/share/jupyter/lab" Please run jupyter lab build or use a different app directory"

Besides, I can launch Jupyter (not lab) correctly by sage -n or sage -n jupyter, but I'm not very sure if it is the SageMath built-in version or my global version Jupyter. On the other hand, I can always launch SageMath built-in Jupyter Lab from app GUI.

culler commented 1 year ago

If you are using the Sage 10.1 app then you can fix this by editing /usr/local/bin/sage, e.g. with sudo nano /usr/local/bin/sage. You will need to add one line to the file. The new line could go immediately above the second line from the end, which currently is: "$SYMLINK"/venv/bin/sage "$@" The new line should be: export JUPYTERLAB_DIR="$SYMLINK"/notebook_venv/share/jupyter/lab

It might be wise to also uninstall jupyterlab with pip (or sage -pip) by reversing what you did for the install. Having redundant copies of jupyterlab will not be useful and could lead to confusion, not to mention wasting disk space.

Deeperand commented 1 year ago

If you are using the Sage 10.1 app then you can fix this by editing /usr/local/bin/sage, e.g. with sudo nano /usr/local/bin/sage. You will need to add one line to the file. The new line could go immediately above the second line from the end, which currently is: "$SYMLINK"/venv/bin/sage "$@" The new line should be: export JUPYTERLAB_DIR="$SYMLINK"/notebook_venv/share/jupyter/lab

It might be wise to also uninstall jupyterlab with pip (or sage -pip) by reversing what you did for the install. Having redundant copies of jupyterlab will not be useful and could lead to confusion, not to mention wasting disk space.

I've tried this, but it looks still can't find Jupyter Lab. Now this is my /usr/local/bin/sage

#! /bin/bash
VERSION=10.1
DASH_VERSION=10-1
SYMLINK=/var/tmp/sage-$VERSION-current
APP=SageMath-$DASH_VERSION.app
USE_MINIMAL_ENV=no
for arg do
  shift
  if [ "$arg" = "--minenv" ]; then
      USE_MINIMAL_ENV="yes"
      continue;
  fi
  set -- "$@" "$arg"
done
if [ ! -e $SYMLINK ]; then
    CURRENT=Contents/Frameworks/Sage.framework/Versions/Current
    LSREGISTER=/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister
    DUMP=$($LSREGISTER -apps ul -dump | grep $APP | grep -v Volumes | grep -v Trash)
    for ITEM in $DUMP; do
    if [ $(basename $ITEM) == $APP ]; then
        ln -s $ITEM/$CURRENT $SYMLINK;
        break;
    fi
    done
fi
if [ ! -e $SYMLINK ]; then
    echo Could not find $APP
    exit 1
fi
if [ "$USE_MINIMAL_ENV" == "yes" ]; then
    MIN_ENV="USER=$USER LOGNAME=$LOGNAME SHELL=$SHELL HOME=$HOME TERM=$TERM LANG=$LANG"
    /usr/bin/env - PATH="$PATH" $MIN_ENV "$SYMLINK"/venv/bin/sage "$@"
else
    export JUPYTERLAB_DIR="$SYMLINK"/notebook_venv/share/jupyter/lab
    "$SYMLINK"/venv/bin/sage "$@"
fi

and this is the error message when I run sage -n jupyterlab (of course, I've uninstalled Jupyter Lab install by %pip install jupyterlab)

$ sage -n jupyterlab
Traceback (most recent call last):
  File "/private/var/tmp/sage-10.1-current/local/var/lib/sage/venv-python3.11.1/bin/sage-notebook", line 57, in __init__
    from jupyterlab.labapp import main
ModuleNotFoundError: No module named 'jupyterlab'
Jupyterlab is not installed (at least not in this Sage installation).
You can install it by running
  sage -i jupyterlab
Alternatively, you can follow the instructions at
  https://doc.sagemath.org/html/en/installation/launching.html#setting-up-sagemath-as-a-jupyter-kernel-in-an-existing-jupyter-notebook-or-jupyterlab-installation
to use Sage with an existing Jupyterlab installation.
culler commented 12 months ago

Sorry! That was my fault. After advising you to uninstall jupyterlab it turned out that I had done a user install of jupyterlab on the laptop that I am using here. That was why it worked for me.

You should be able to make it work by adding one more line to /usr/local/bin/sage: export PYTHONPATH="$SYMLINK"/notebook_venv/lib/python3.11/site-packages It could go directly above the other export line that you added.

This is not an ideal solution. Many people object to using PYTHONPATH. I will do something more robust in the next release. But I think it will work for you in the short term.

Deeperand commented 12 months ago

Sorry! That was my fault. After advising you to uninstall jupyterlab it turned out that I had done a user install of jupyterlab on the laptop that I am using here. That was why it worked for me.

You should be able to make it work by adding one more line to /usr/local/bin/sage: export PYTHONPATH="$SYMLINK"/notebook_venv/lib/python3.11/site-packages It could go directly above the other export line that you added.

This is not an ideal solution. Many people object to using PYTHONPATH. I will do something more robust in the next release. But I think it will work for you in the short term.

Thanks, it works for me now! This is my current /usr/local/bin/sage

#! /bin/bash
VERSION=10.1
DASH_VERSION=10-1
SYMLINK=/var/tmp/sage-$VERSION-current
APP=SageMath-$DASH_VERSION.app
USE_MINIMAL_ENV=no
for arg do
  shift
  if [ "$arg" = "--minenv" ]; then
      USE_MINIMAL_ENV="yes"
      continue;
  fi
  set -- "$@" "$arg"
done
if [ ! -e $SYMLINK ]; then
    CURRENT=Contents/Frameworks/Sage.framework/Versions/Current
    LSREGISTER=/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister
    DUMP=$($LSREGISTER -apps ul -dump | grep $APP | grep -v Volumes | grep -v Trash)
    for ITEM in $DUMP; do
    if [ $(basename $ITEM) == $APP ]; then
        ln -s $ITEM/$CURRENT $SYMLINK;
        break;
    fi
    done
fi
if [ ! -e $SYMLINK ]; then
    echo Could not find $APP
    exit 1
fi
if [ "$USE_MINIMAL_ENV" == "yes" ]; then
    MIN_ENV="USER=$USER LOGNAME=$LOGNAME SHELL=$SHELL HOME=$HOME TERM=$TERM LANG=$LANG"
    /usr/bin/env - PATH="$PATH" $MIN_ENV "$SYMLINK"/venv/bin/sage "$@"
else
    export PYTHONPATH="$SYMLINK"/notebook_venv/lib/python3.11/site-packages
    export JUPYTERLAB_DIR="$SYMLINK"/notebook_venv/share/jupyter/lab
    "$SYMLINK"/venv/bin/sage "$@"
fi

But I also agree to change environment variable $PYTHONPATH globally is not a very good idea, so I think it'd better to close this issue until the next release robustly solving this problem.

culler commented 9 months ago

This has been fixed in the v2.2.0 release of Sage 10.2, so I am closing the ticket.

culler commented 9 months ago

closed