Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

The usage quota dashboard service is an optional Tornado web application that lets users view their current home storage usage and quota directly within JupyterHub.

Installation

Install the package with the service extra:

pip install "jupyterhub-usage-quotas[service]"

How it works

The service:

When dev_mode is enabled (via --dev-mode flag), the service can return randomly generated mock data, which is useful for development without a Prometheus instance. Mock data is only used when ALL three conditions are met: (1) dev_mode is True, AND (2) prometheus_url is the default (http://127.0.0.1:9090), AND (3) hub_namespace is empty. If either prometheus_url or hub_namespace is configured, the service will query Prometheus even when dev_mode is True.

JupyterHub configuration for local development

# jupyterhub_config.py

from jupyterhub_usage_quotas import setup_usage_quotas

c = get_config()  # noqa

# Register the service as a JupyterHub-managed subprocess
c.JupyterHub.services = [
    {
        "name": "usage-quota",
        "url": "http://localhost:9000",
        "display": False,  # Don't show the inbuilt service link since we show a standalone link in the navbar
        "oauth_no_confirm": True,
        "oauth_redirect_uri": "/services/usage-quota/oauth_callback",
        "command": [
            "python",
            "-m",
            "jupyterhub_usage_quotas.services.usage_viewer",
            "--config-files=jupyterhub_config.py",
            "--config-files=jupyterhub_config_secret.py",
        ],
    }
]

c.JupyterHub.load_roles = [
    {
        "name": "usage-quota-service",
        "scopes": ["read:users"],
        "services": ["usage-quota"],
    },
    {
        "name": "user",
        "scopes": ["access:services!service=usage-quota", "self"],
    },
]

# Set up common usage quotas config
setup_usage_quotas(c)
# jupyterhub_config_secret.py
# Example config file for secret data, e.g. credentials and keys.
# Make sure you ignore/encrypt this file if using with a version control system.

c.UsageConfig.prometheus_auth = {"username": "", "password": ""}

c.UsageViewer.session_secret_key = "use-a-secure-random-key-in-production"

When a command is provided, JupyterHub launches and manages the service process automatically, injecting the necessary JUPYTERHUB_* environment variables.

CLI Flags

The service is configured via CLI flags (preferred) or traitlet configuration:

CLI FlagConfig AttributeDefaultDescription
--prometheus-urlprometheus_urlhttp://127.0.0.1:9090Prometheus server endpoint
--hub-namespacehub_namespace""Kubernetes namespace of the JupyterHub deployment, used to filter metrics
--dev-modedev_modeFalseEnable development mode with mock data
--portservice_port9000Port to bind the service to
--hostservice_host0.0.0.0Host to bind the service to
--session-secret-keysession_secret_key(required)Secret key for session cookie encryption
--public-hub-urlpublic_hub_url(required)Public URL of the JupyterHub instance

Environment Variables

All configuration options can be set via environment variables as alternatives to CLI flags:

VariableDescription
JUPYTERHUB_PUBLIC_HUB_URLPublic URL of the JupyterHub instance (required)
JUPYTERHUB_USAGE_QUOTAS_SESSION_SECRET_KEYSecret key for session cookie encryption (required)
JUPYTERHUB_USAGE_QUOTAS_PROMETHEUS_URLPrometheus server endpoint (default: http://127.0.0.1:9090)
JUPYTERHUB_USAGE_QUOTAS_PROMETHEUS_USERNAME(Optional) Prometheus server username
JUPYTERHUB_USAGE_QUOTAS_PROMETHEUS_PASSWORD(Optional) Prometheus server password
JUPYTERHUB_USAGE_QUOTAS_HUB_NAMESPACEKubernetes namespace of the JupyterHub deployment, used to filter metrics (default: empty)