Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wildlarva/79e145268d579043640f59f066bce6f6 to your computer and use it in GitHub Desktop.
Save wildlarva/79e145268d579043640f59f066bce6f6 to your computer and use it in GitHub Desktop.
A shell script to open a web browser from WSL environment. This let you run Jupyter Notebook in WSL.
# Use webbrowser.sh as x-www-browser command
#
# Please follow the instructions below to install:
# 1. Modify the path to webbrowser.sh
# 2. Execute this command
sudo update-alternatives --install /usr/bin/x-www-browser x-www-browser /path-to/webbrowser.sh 1 # Edit this path to point your webbrowser.sh
# Use webbrowser.sh as a web browser for Jupyter Notebook
#
# Please follow the instructions below to install:
# 1. Add this snippet to ~/.jupyter/jupyter_notebook_config.py
# 2. Modify the path to webbrowser.sh
import webbrowser
webbrowser.register('default', None, webbrowser.GenericBrowser('/path-to/webbrowser.sh')) # Edit this path to point your webbrowser.sh
c.NotebookApp.browser = 'default'
#!/bin/bash
# A script to open a file in WSL in web browser
#
# Please put this file somewhere in your local
#
# Usage: /path-to/webbrowser.sh URL
# Environment variables:
# WEB_BROWSER: Path to a web browser(path in WSL environment). If not specificed, Google Chrome is used as a default browser.
# Save argument
readonly ARG_URL="$1"
# Constants
readonly CHROME_PATH="/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe"
# Check argument
if [[ -z $ARG_URL ]]; then
echo "Please pass a URL for the browser."
exit 1
fi
# Use chrome if WEB_BROWSER is not specified
if [[ -z $WEB_BROWSER ]]; then
readonly WEB_BROWSER="$CHROME_PATH"
fi
# Resolve a URL to pass to the browser
if [[ $ARG_URL == "https://"* || $ARG_URL == "http://"* ]]; then
# For https://, http://
# Pass the URL argument to the browser
readonly URL="$ARG_URL"
else
# Others
# Expect a file:// URL or a file path
readonly ORIGINAL_PATH="${ARG_URL/"file://"/}"
readonly RESOLVED_PATH="$(readlink -f "$ORIGINAL_PATH")"
readonly WINDOWS_PATH="$(wslpath -w "$RESOLVED_PATH")"
readonly URL="file:$WINDOWS_PATH"
fi
# Launch the browser with the URL
"$WEB_BROWSER" "$URL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment