Skip to content

Instantly share code, notes, and snippets.

@davidmashburn
Created March 11, 2018 16:53
Show Gist options
  • Save davidmashburn/6fd2d5be5965ac8e3d621b21897503a9 to your computer and use it in GitHub Desktop.
Save davidmashburn/6fd2d5be5965ac8e3d621b21897503a9 to your computer and use it in GitHub Desktop.
Generate .desktop-style launchers for Jupyter that allow you to one-click into a fresh shell + other Jupyter config
# I miss the simplicity of launching a fresh shell when using Jupyter
# This is my wonderfully weird way of fixing that (at least on Linux)
# Config and desktop integration:
jupyter notebook --generate-config
echo "
import os
from subprocess import check_call
def post_save(model, os_path, contents_manager):
'''post-save hook for converting notebooks to .py and .html files.'''
if model['type'] != 'notebook':
return # only do this for notebooks
d, fname = os.path.split(os_path)
check_call(['jupyter', 'nbconvert', '--to', 'script', fname], cwd=d)
check_call(['jupyter', 'nbconvert', '--to', 'html', fname], cwd=d)
c.FileContentsManager.post_save_hook = post_save
" >> ~/.jupyter/jupyter_notebook_config.py
# Came from: https://gist.github.com/jbwhit/881bdeeaae3e4128947c
# Also see: http://jupyter-notebook.readthedocs.io/en/stable/extending/savehooks.html
mkdir ~/.jupyter/custom/
echo '.container { width:100% !important; }' > ~/.jupyter/custom/custom.css
mkdir -p ~/.local/bin/
mkdir -p ~/.local/share/applications
mkdir -p ~/.config/JuPyteR
echo '/usr/local/bin/jupyter notebook --notebook-dir="~" --no-browser --browser="google-chrome --user-data-dir=.config/JuPyteR --class=JuPyteR %s" "$@"
' > ~/.local/bin/launch_jupyter.sh
chmod +x ~/.local/bin/launch_jupyter.sh
echo '{"cells": [], "metadata": {}, "nbformat": 4, "nbformat_minor": 2}' > ~/.local/bin/ipynb.template
echo 'cp ~/.local/bin/ipynb.template ~/"Blank - this file will be automatically overwritten.ipynb"
/usr/local/bin/jupyter notebook --notebook-dir="~" --browser="google-chrome --user-data-dir=.config/JuPyteR --class=JuPyteR %s" ~/"Blank - this file will be automatically overwritten.ipynb"
' > ~/.local/bin/launch_new_jupyter.sh
chmod +x ~/.local/bin/launch_new_jupyter.sh
echo "[Desktop Entry]
Name=JuPyteR
Exec=/home/${USER}/.local/bin/launch_jupyter.sh %f
Type=Application
Terminal=false
Icon=/media/home/Git/dnm_system_install/icons/JuPyteR.png
StartupNotify=true
StartupWMClass=JuPyteR" > ~/.local/share/applications/JuPyteR.desktop
chmod +x ~/.local/share/applications/JuPyteR.desktop
echo "[Desktop Entry]
Name=New JuPyteR
Exec=/home/${USER}/.local/bin/launch_new_jupyter.sh %f
Type=Application
Terminal=false
Icon=/media/home/Git/dnm_system_install/icons/JuPyteR_New.png
StartupNotify=true" > ~/.local/share/applications/NewJuPyteR.desktop
chmod +x ~/.local/share/applications/NewJuPyteR.desktop
echo '<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
<mime-type type="application/x-ipynb+json">
<comment>IPython Notebook</comment>
<glob pattern="*.ipynb"/>
</mime-type>
</mime-info>
' | sudo tee /usr/share/mime/packages/ipynb.xml
<mime-type xmlns="http://www.freedesktop.org/standards/shared-mime-info" type="application/scad">
<glob pattern="*.scad"/>
</mime-type>
sudo update-mime-database /usr/share/mime
xdg-mime default /usr/local/share/applications/JuPyteR.desktop application/ipynb
# Manual stuff:
# Add to launcher at positions 3 and 4 respectively
nautilus ~/.local/share/applications/ &
# Launch both and mark as trusted
# Go into applications and look for "New" to add it manually
# Find an ipynb and set "JuPyteR" manually in nautilus
nautilus ~/Git/dnmPrivate/ &
# The one downside to this approach is leaving a ton of random jupyter servers running all the time
# Just run:
# kill $(pgrep jupyter)
# to fix it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment