Skip to content

Instantly share code, notes, and snippets.

@xiaolai
Last active September 15, 2023 08:37
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save xiaolai/697ec3ea1607994440abf574c0f017e5 to your computer and use it in GitHub Desktop.
Save xiaolai/697ec3ea1607994440abf574c0f017e5 to your computer and use it in GitHub Desktop.
Run Jupterlab as an desktop app

How to run Jupyterlab as a desktop app on Mac OSX

In Sep, 2021, Jupyterlab Desktop App (electron) was released by Mehmet Bektas (github repo).

brew install --cask jupyterlab

jupyterlab-app-first-opened

Choose "Install Python environment using the bundled installer"... and then "Apply and restart"


[original post]

Tired of opening terminal to launch Jupyterlab, and having to leave it opened all the time? Try to run Jupyterlab as a desktop app:

nativefier-jupyterlab

One of a benefits is avoiding the annoying accident: "closed Jupyterlab when quitting the browser".

1. Install Miniconda for mac

brew instal miniconda

Homebrew will install latest version of miniconda, and later you can keep miniconda updated with

brew upgrade miniconda

After installation, you need to run a command to setup conda

conda init "$(basename "${SHELL}")"

2. Create a virtual environment for Jupyterlab

conda create -n jupyter python ipython jupyterlab nodejs
conda activate jupyter

You can verify installation with

python --version
node -v
which node
jupyter --version
which jupyter

Test jupyter:

jupyter lab

3. Generate jupter lab configure file

Run in Terminal

jupyter-lab --generate-config

Edit ~/.jupyter/jupyter_lab_config.py, and add lines below to the end of the file, or read explanations to decide configuration...

c.LabApp.open_browser = False
c.ServerApp.open_browser = False
c.ServerApp.password_required = False
c.ServerApp.allow_remote_access = False
c.ServerApp.root_dir = '~/'
c.ServerApp.token = ''

4. Create a shell script to launch jupyter as a service

Note: I'm using zsh (rather than bash)

#!/bin/zsh
PATH="/opt/homebrew/Caskroom/miniconda/base/bin:$PATH"
eval "$(conda 'shell.zsh' hook)"
conda activate jupyter
cd ~
/opt/homebrew/Caskroom/miniconda/base/envs/jupyter/bin/python -m jupyter lab

I save it as /opt/homebrew/bin/jupyterservice, and render it executable:

chmod +x /opt/homebrew/bin/jupyterservice

5. Run Jupyter Lab as a service

5.1

Create a plist file for launchctrl, and save it as ~/Library/LaunchAgents/com.jupyter.lab.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>local.job</string>
	<key>ProgramArguments</key>
	<array>
		<string>/opt/homebrew/bin/jupyterservice</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
	<key>StandardErrorPath</key>
	<string>/tmp/local.job.err</string>
	<key>StandardOutPath</key>
	<string>/tmp/local.job.out</string>
</dict>
</plist>

5.2

Run in terminal

launchctl load ~/Library/LaunchAgents/com.jupyter.lab.plist

This will launch jupter lab in background... you can visit jupyter in browser: `http://localhost:8888"

If you want to restart jupyter lab:

Run in Terminal

launchctl unload ~/Library/LaunchAgents/com.jupyter.lab.plist
launchctl load ~/Library/LaunchAgents/com.jupyter.lab.plist

5.3

... and here's a handy function you can save in .zshrc (for bash users: .bashrc or .bash_profile), so you can run lctl reload ~/Library/LaunchAgents/com.jupyter.lab.plist in terminal to reload in a single line, or make it an Alfred workflow:

function lctl {
    COMMAND=$1
    PLIST_FILE=$2
    if [ "$COMMAND" = "reload" ] && [ -n "$PLIST_FILE" ]
      then
        echo "reloading ${PLIST_FILE}.."
        launchctl unload ${PLIST_FILE}
        launchctl load ${PLIST_FILE}
      else
        echo "either command not specified or plist file is not defined"
    fi
}

or, you can set up aliases for convenience, save it at the end of .zshrc:

# for jupyter
alias jpu="launchctl unload ~/Library/LaunchAgents/com.jupyter.lab.plist"
alias jpl="launchctl load ~/Library/LaunchAgents/com.jupyter.lab.plist"
alias jpr="jpu && jpl"

6. Build Desktop App with Nativefier

Nativefier github: https://github.com/jiahaog/nativefier

Run in Terminal

# in case you didn't activate virtual environment `jupyter` set as above: 
conda activate jupyter
npm install nativefier -g 
cd ~/Applications
nativefier "http://localhost:8888"

nativefier-jupyterlab-icon

Supplement

Adding theme to Jupyter lab:

jupyter labextension install @arbennett/base16-gruvbox-dark
jupyter labextension install @arbennett/base16-gruvbox-light
jupyter labextension install @arbennett/base16-mexico-light
jupyter labextension install @arbennett/base16-monokai
jupyter labextension install @arbennett/base16-nord
jupyter labextension install @arbennett/base16-one-dark
jupyter labextension install @arbennett/base16-outrun
jupyter labextension install @arbennett/base16-solarized-dark
jupyter labextension install @arbennett/base16-solarized-light
jupyter labextension install @arbennett/base16-summerfruit-light

https://github.com/arbennett/jupyterlab-themes

Other useful commands:

jupyter --version
jupyter lab clean
jupyter lab build

ubuntu link

https://naysan.ca/2019/09/07/jupyter-notebook-as-a-service-on-ubuntu-18-04-with-python-3/

@leeduckgo
Copy link

bash /Download/Anaconda3-2018.12-MacOSX-x86_64.sh

->

bash ./Anaconda3-2018.12-MacOSX-x86_64.sh

@xiaolai
Copy link
Author

xiaolai commented Jan 26, 2020

bash /Download/Anaconda3-2018.12-MacOSX-x86_64.sh

->

bash ./Anaconda3-2018.12-MacOSX-x86_64.sh

rectified.

@JLansey
Copy link

JLansey commented Aug 30, 2020

This approach gets pretty close though doesn't include all the benefits listed above.
http://christopherroach.com/articles/jupyterlab-desktop-app/

@quarkquartet
Copy link

In this line of the plist file:
<string>/Users/your_username/anaconda3/bin/jupyter</string>
The path here should be the correct jupyter path on the laptop. Sometimes the path can be
~/opt/anaconda3/envs/..../bin/jupyter
Make sure check this part, otherwise the plist file does not work.

@quarkquartet
Copy link

I get error if I use launchctl to load jupyter server.

Error communicating with server extension. Consult the documentation for how to ensure that it is enabled.

Reason given:

Error: 500 (Internal Server Error)

If I run jupyter lab in terminal directly, everything is OK. But i cannot run it with launchd.

Any way to solve this issue?

@quarkquartet
Copy link

I get error if I use launchctl to load jupyter server.

Error communicating with server extension. Consult the documentation for how to ensure that it is enabled.

Reason given:

Error: 500 (Internal Server Error)

If I run jupyter lab in terminal directly, everything is OK. But i cannot run it with launchd.

Any way to solve this issue?

I debugged for a while. I found that nodes is not loaded when I start Jupyter service with launchd. But I have no way to solve that.
Any idea?

@dbbyres
Copy link

dbbyres commented May 22, 2022

Sweet Jesus...all this just to activate a conda environment and run jupyter lab as a desktop shortcut?

@stevemewqld
Copy link

This was super helpful. Many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment