Skip to content

Instantly share code, notes, and snippets.

@Fahad021
Forked from xiaolai/jupyter-as-a-desktop-app.md
Created September 26, 2021 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fahad021/9a6fe417a15e18752d458f551fc9750c to your computer and use it in GitHub Desktop.
Save Fahad021/9a6fe417a15e18752d458f551fc9750c 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

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/

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