Skip to content

Jupyter Server Proxy

Interactive Diagram

새 탭 ↗

With Jupyter Server Proxy, you can run arbitrary external processes, such as RStudio or VS Code, alongside a notebook server and let the notebook server handle authentication on their behalf.

After installing the jupyter-server-proxy extension in a Notebook or JupyterLab environment, you can expose those processes through proxy URLs and launch them directly from the browser UI.

See Jupyter Server Proxy Docs https://jupyter-server-proxy.readthedocs.io/en/latest/index.html

Running nonjupyter applications on JupyterHub with jupyter-server-proxy| JupyterCon 2020 https://www.youtube.com/watch?v=8tFLZWT0u2Y

Assume JupyterHub is running and that a process on the user’s notebook server is listening on port 8080. In that case, the service can be reached through the JupyterHub URL, for example, myhub.org/hub/{user-redirect}/proxy/8080.

JupyterHub redirects each authenticated user to the correct per-user URL. You can also use the default_url setting to land users directly in the proxied application. If a user is not logged in, authentication happens first and the browser is redirected afterward.

  • Architecture
PlantUML Diagram

The main customization work is deciding how to package applications such as code-server and RStudio so they run beside the notebook process and can be exposed through the Jupyter proxy.

  • Work covered here
    • Build a code-server base image on top of a Jupyter notebook image
    • Create and copy VS Code settings
    • Create and copy jupyter_notebook_config.py
    • Add a jupyter-code-server profile and KubeSpawner settings to the JupyterHub profile list

kloudbank Github Repository https://github.com/kloudbank/jupyter-k8s/tree/main/dev/jupyter-notebook/code-server

  • Dockerfile composition
    • Start from the official jupyter/minimal-notebook image
    • Install jupyter-server-proxy into the conda environment
    • Enable the server extension and lab extension
    • Install Linux code-server
    • Copy the VS Code and Jupyter configuration directories
# use the Jupyter Official Image
ARG BASE_CONTAINER=jupyter/minimal-notebook
FROM $BASE_CONTAINER
USER $NB_UID
# install Jupyter Server Proxy and enable the extension
RUN "${CONDA_DIR}/bin/pip" install jupyter-server-proxy \
&& jupyter serverextension enable --sys-prefix jupyter_server_proxy \
&& jupyter labextension install --no-build @jupyterlab/server-proxy
USER root
# install Linux Code Server
ENV CODESERVER_URL="https://github.com/cdr/code-server/releases/download/v3.4.0/code-server-3.4.0-linux-amd64.tar.gz" \
CODESERVER="code-server-3.4.0-linux-amd64"
RUN wget ${CODESERVER_URL} && \
tar -xvzf ${CODESERVER}.tar.gz && \
cp -r ${CODESERVER} /usr/lib/code-server && \
ln -s /usr/lib/code-server/bin/code-server /usr/bin/code-server && \
rm -rf code-server* && \
rm -rf /tmp/* && \
rm -rf $HOME/.cache && \
rm -rf $HOME/.node-gyp && \
fix-permissions $CONDA_DIR && \
fix-permissions $HOME
WORKDIR $HOME
USER $NB_UID
# copy the pre-created configuration directory
COPY .jupyter /home/$NB_USER/.jupyter
COPY .vscode /home/$NB_USER/.vscode
USER root
RUN fix-permissions "/home/${NB_USER}/.vscode"
USER $NB_UID
  • Create jupyter_notebook_config.py
    • Define the launch command, launcher entry, and URL mapping for the proxied application.
    • From JupyterHub’s point of view this is still a notebook server, so the notebook behavior is configured through NotebookApp while the proxy mapping itself is configured through ServerProxy.
import os
import shutil
# define the Linux Code Server execution script
def _get_code_server_cmd(port):
executable = "code-server"
if not shutil.which(executable):
raise FileNotFoundError("Can not find code-server in PATH")
# Start vscode in CODE_WORKINGDIR env variable if set
# If not, start in 'current directory', which is $REPO_DIR in mybinder
# but /home/jovyan (or equivalent) in JupyterHubs
working_dir = os.getenv("CODE_WORKINGDIR", ".")
extensions_dir = os.getenv("CODE_EXTENSIONSDIR", None)
extra_extensions_dir = os.getenv("CODE_EXTRA_EXTENSIONSDIR", None)
cmd = [
executable,
"--auth",
"none",
"--disable-telemetry",
"--port=" + str(port),
]
if extensions_dir:
cmd += ["--extensions-dir", extensions_dir]
if extra_extensions_dir:
cmd += ["--extra-extensions-dir", extra_extensions_dir]
cmd.append(working_dir)
return cmd
# Jupyter Notebook Config
c.NotebookApp.allow_root = True
c.NotebookApp.ip = '0.0.0.0'
c.NotebookApp.default_url = '/code-server/'
# Server Proxy Config
c.ServerProxy.servers = {
# when accessing via the /code-server url, the server proxy runs the command below
'code-server': {
'command': _get_code_server_cmd,
'timeout': 20,
'launcher_entry': {
'title': 'VS Code IDE',
'icon_path': os.path.join(
os.path.dirname(os.path.abspath(__file__)), "icons", "code-server.svg"),
}
}
}

Add a profile and kubespawner settings to the Jupyter Hub config.yaml

  • config.yaml composition
...
- display_name: "Jupyter Code Server"
description: "jupyter server proxy vscode"
kubespawner_override:
cpu_limit: 1
cpu_guarantee: 0.5
mem_limit: 2G
mem_guarantee: 512M
args:
- "--allow-root"
# jupyter notebook-based Code Server Base Image
image: kloudbank/jupyter-code-server:latest
image_pull_secrets: docker-hub-noisonnoiton
default_url: /code-server/
volume_mounts:
- name: jupyter-code-server
mountPath: /home/jovyan
volumes:
- name: jupyter-code-server
persistentVolumeClaim:
claimName: jupyter-code-server
...

Developing a Web App. in Jupyter Code Server

Section titled “Developing a Web App. in Jupyter Code Server”

Once the Jupyter-based code-server image is running, you can also expose a local web service through the proxy by referencing its port in the URL.

  • flask python code example
from flask import Flask
app = Flask(__name__)
@app.route('/')
def bonjour_world():
return 'Bonjour,,!!!'
if __name__ == '__main__':
app.run()
  • Run in Jupyter Code Server
Terminal window
$ python flask/bonjour.py
* Serving Flask app "bonjour" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

  • Specify port 5000 in the URL, and call it from the Web Browser through the Jupyter Server Proxy : ex> myhub.org/hub/{user-redirect}/proxy/5000

For RStudio, the open-source jupyter-rsession-proxy package already provides most of the required integration, so a separate notebook-level proxy configuration is not necessary.

  • Work covered here
    • Build an RStudio base image on top of a Jupyter notebook image
    • Install jupyter-rsession-proxy in that base image
    • Add an RStudio profile and KubeSpawner settings to the JupyterHub profile list

kloudbank Github Repository https://github.com/kloudbank/jupyter-k8s/tree/main/dev/jupyter-notebook/rstudio

  • Dockerfile composition
    • Based on the Jupyter minimal-notebook Official Image
    • Install the jupyter-rsession-proxy package in the Conda environment
    • Install R Studio
ARG BASE_CONTAINER=jupyter/minimal-notebook
FROM $BASE_CONTAINER
USER root
RUN "${CONDA_DIR}/bin/pip" install jupyter-rsession-proxy && \
jupyter labextension install --no-build @jupyterlab/server-proxy
ENV RSTUDIO_URL="https://download2.rstudio.org/server/trusty/amd64/rstudio-server-1.2.5019-amd64.deb"
ENV RSTUDIO="rstudio-server-1.2.5019-amd64.deb"
RUN echo "deb http://security.ubuntu.com/ubuntu xenial-security main" >> /etc/apt/sources.list
# install rstudio-server
RUN apt-get update && \
apt-get -y install libssl1.0.0 libssl-dev && \
cd /lib/x86_64-linux-gnu && ln -s libssl.so.1.0.0 libssl.so.10 && ln -s libcrypto.so.1.0.0 libcrypto.so.10 && \
cd /tmp/ && wget ${RSTUDIO_URL} --no-check-certificate && \
apt-get install -y /tmp/${RSTUDIO} && \
rm /tmp/${RSTUDIO} && \
apt-get clean && rm -rf /var/lib/apt/lists/*
ENV PATH=$PATH:/usr/lib/rstudio-server/bin
  • jupyter-rsession-proxy package notes

    • The package already defines the rstudio-server and rsession launch behavior required to run RStudio.
    • The ServerProxy configuration for /rstudio is included, so no additional notebook config is needed.
  • package setup.py

import setuptools
setuptools.setup(
name="jupyter-rsession-proxy",
version='1.2',
url="https://github.com/jupyterhub/jupyter-rsession-proxy",
author="Ryan Lovett & Yuvi Panda",
description="Jupyter extension to proxy RStudio",
packages=setuptools.find_packages(),
keywords=['Jupyter'],
classifiers=['Framework :: Jupyter'],
install_requires=[
'jupyter-server-proxy'
],
# package execution entrypoint
entry_points={
'jupyter_serverproxy_servers': [
# /rstudio handling and command mapping
'rstudio = jupyter_rsession_proxy:setup_rserver'
]
},
package_data={
'jupyter_rsession_proxy': ['icons/rstudio.svg'],
},
)

See jupyter-rsession-proxy Github Repository https://github.com/jupyterhub/jupyter-rsession-proxy

Add a profile and kubespawner settings to the Jupyter Hub config.yaml

  • config.yaml composition
...
- display_name: "Jupyter R Studio"
description: "jupyter server proxy rsession"
kubespawner_override:
cpu_limit: 1
cpu_guarantee: 0.5
mem_limit: 2G
mem_guarantee: 512M
args:
- "--allow-root"
image: kloudbank/jupyter-rstudio:latest
image_pull_secrets: docker-hub-noisonnoiton
default_url: /rstudio/
# jupyter notebook-based Code Server Base Image
volume_mounts:
- name: jupyter-rstudio
mountPath: /home/jovyan
volumes:
- name: jupyter-rstudio
persistentVolumeClaim:
claimName: jupyter-rstudio
...
  • Connection screen