jupyterhub.spawner
Contains base Spawner class & default implementation
Spawner
jupyterhub.spawner.
Base class for spawning single-user notebook servers.
Subclass this, and override the following methods:
load_state
get_state
start
stop
poll
As JupyterHub supports multiple users, an instance of the Spawner subclass is created for each user. If there are 20 JupyterHub users, there will be 20 instances of the subclass.
args
Extra arguments to be passed to the single-user server.
Some spawners allow shell-style expansion here, allowing you to use environment variables here. Most, including the default, do not. Consult the documentation for your spawner to verify!
auth_state_hook
An optional hook function that you can implement to pass auth_state to the spawner after it has been initialized but before it starts. The auth_state dictionary may be set by the .authenticate() method of the authenticator. This hook enables you to pass some or all of that information to your spawner.
auth_state
.authenticate()
Example:
def userdata_hook(spawner, auth_state): spawner.userdata = auth_state["userdata"] c.Spawner.auth_state_hook = userdata_hook
cmd
The command used for starting the single-user server.
Provide either a string or a list containing the path to the startup script command. Extra arguments, other than this path, should be provided via args.
This is usually set if you want to start the single-user server in a different python environment (with virtualenv/conda) than JupyterHub itself.
Some spawners allow shell-style expansion here, allowing you to use environment variables. Most, including the default, do not. Consult the documentation for your spawner to verify!
consecutive_failure_limit
Maximum number of consecutive failures to allow before shutting down JupyterHub.
This helps JupyterHub recover from a certain class of problem preventing launch in contexts where the Hub is automatically restarted (e.g. systemd, docker, kubernetes).
A limit of 0 means no limit and consecutive failures will not be tracked.
cpu_guarantee
Minimum number of cpu-cores a single-user notebook server is guaranteed to have available.
If this value is set to 0.5, allows use of 50% of one CPU. If this value is set to 2, allows use of up to 2 CPUs.
This is a configuration setting. Your spawner must implement support for the limit to work. The default spawner, LocalProcessSpawner, does not implement this support. A custom spawner must add support for this setting for it to be enforced.
LocalProcessSpawner
cpu_limit
Maximum number of cpu-cores a single-user notebook server is allowed to use.
The single-user notebook server will never be scheduled by the kernel to use more cpu-cores than this. There is no guarantee that it can access this many cpu-cores.
debug
Enable debug-logging of the single-user server
default_url
The URL the single-user server should start in.
{username} will be expanded to the user’s username
{username}
Example uses:
You can set notebook_dir to / and default_url to /tree/home/{username} to allow people to navigate the whole filesystem from their notebook server, but still start in their home directory.
notebook_dir
/
/tree/home/{username}
Start with /notebooks instead of /tree if default_url points to a notebook instead of a directory.
/notebooks
/tree
You can set this to /lab to have JupyterLab start by default, rather than Jupyter Notebook.
/lab
disable_user_config
Disable per-user configuration of single-user servers.
When starting the user’s single-user server, any config file found in the user’s $HOME directory will be ignored.
Note: a user could circumvent this if the user modifies their Python environment, such as when they have their own conda environments / virtualenvs / containers.
env_keep
Whitelist of environment variables for the single-user server to inherit from the JupyterHub process.
This whitelist is used to ensure that sensitive information in the JupyterHub process’s environment (such as CONFIGPROXY_AUTH_TOKEN) is not passed to the single-user server’s process.
CONFIGPROXY_AUTH_TOKEN
environment
Extra environment variables to set for the single-user server’s process.
This environment configurable
The JupyterHub process’ environment variables that are whitelisted in env_keep
Variables to establish contact between the single-user notebook and the hub (such as JUPYTERHUB_API_TOKEN)
The environment configurable should be set by JupyterHub administrators to add installation specific environment variables. It is a dict where the key is the name of the environment variable, and the value can be a string or a callable. If it is a callable, it will be called with one parameter (the spawner instance), and should return a string fairly quickly (no blocking operations please!).
Note that the spawner class’ interface is not guaranteed to be exactly same across upgrades, so if you are using the callable take care to verify it continues to work after upgrades!
http_timeout
Timeout (in seconds) before giving up on a spawned HTTP server
Once a server has successfully been spawned, this is the amount of time we wait before assuming that the server is unable to accept connections.
ip
The IP address (or hostname) the single-user server should listen on.
The JupyterHub proxy implementation should be able to send packets to this interface.
mem_guarantee
Minimum number of bytes a single-user notebook server is guaranteed to have available.
K -> Kilobytes
M -> Megabytes
G -> Gigabytes
T -> Terabytes
mem_limit
Maximum number of bytes a single-user notebook server is allowed to use.
If the single user server tries to allocate more memory than this, it will fail. There is no guarantee that the single-user notebook server will be able to allocate this much memory - only that it can not allocate more than this.
Path to the notebook directory for the single-user server.
The user sees a file listing of this directory when the notebook interface is started. The current interface does not easily allow browsing beyond the subdirectories in this directory’s tree.
~ will be expanded to the home directory of the user, and {username} will be replaced with the name of the user.
~
Note that this does not prevent users from accessing files outside of this path! They can do so with many other means.
options_form
An HTML form for options a user can specify on launching their server.
The surrounding <form> element and the submit button are already provided.
<form>
For example:
Set your key: <input name="key" val="default_key"></input> <br> Choose a letter: <select name="letter" multiple="true"> <option value="A">The letter A</option> <option value="B">The letter B</option> </select>
The data from this form submission will be passed on to your spawner in self.user_options
self.user_options
Instead of a form snippet string, this could also be a callable that takes as one parameter the current spawner instance and returns a string. The callable will be called asynchronously if it returns a future, rather than a str. Note that the interface of the spawner class is not deemed stable across versions, so using this functionality might cause your JupyterHub upgrades to break.
poll_interval
Interval (in seconds) on which to poll the spawner for single-user server’s status.
At every poll interval, each spawner’s .poll method is called, which checks if the single-user server is still running. If it isn’t running, then JupyterHub modifies its own state accordingly and removes appropriate routes from the configurable proxy.
.poll
port
The port for single-user servers to listen on.
Defaults to 0, which uses a randomly allocated port number each time.
0
If set to a non-zero value, all Spawners will use the same port, which only makes sense if each server is on a different address, e.g. in containers.
New in version 0.7.
post_stop_hook
An optional hook function that you can implement to do work after the spawner stops.
This can be set independent of any concrete spawner implementation.
pre_spawn_hook
An optional hook function that you can implement to do some bootstrapping work before the spawner starts. For example, create a directory for your user or load initial content.
This maybe a coroutine.
from subprocess import check_call def my_hook(spawner): username = spawner.user.name check_call(['./examples/bootstrap-script/bootstrap.sh', username]) c.Spawner.pre_spawn_hook = my_hook
ssl_alt_names
List of SSL alt names
May be set in config if all spawners should have the same value(s), or set at runtime by Spawner that know their names.
ssl_alt_names_include_local
Whether to include DNS:localhost, IP:127.0.0.1 in alt names
start_timeout
Timeout (in seconds) before giving up on starting of single-user server.
This is the timeout for start to return, not the timeout for the server to respond. Callers of spawner.start will assume that startup has failed if it takes longer than this. start should return when the server process is started and its location is known.
create_certs
Create and set ownership for the certs to be used for internal ssl
alt_names (list) – a list of alternative names to identify the
by, see (server) –
https – //en.wikipedia.org/wiki/Subject_Alternative_Name
override – override the default_names with the provided alt_names
Path to cert files and CA
dict
This method creates certs for use with the singleuser notebook. It enables SSL and ensures that the notebook can perform bi-directional SSL auth with the hub (verification based on CA).
If the singleuser host has a name or ip other than localhost, an appropriate alternative name(s) must be passed for ssl verification by the hub to work. For example, for Jupyter hosts with an IP of 10.10.10.10 or DNS name of jupyter.example.com, this would be:
alt_names=[“IP:10.10.10.10”] alt_names=[“DNS:jupyter.example.com”]
respectively. The list can contain both the IP and DNS names to refer to the host by either IP or DNS name (note the default_names below).
default_names
format_string
Render a Python format string
Uses Spawner.template_namespace() to populate format namespace.
Spawner.template_namespace()
s (str) – Python format-string to be formatted.
Formatted string, rendered
str
get_args
Return the arguments to be passed after self.cmd
Doesn’t expect shell expansion to happen.
get_env
Return the environment dict to use for the Spawner.
This applies things like env_keep, anything defined in Spawner.environment, and adds the API token to the env.
Spawner.environment
When overriding in subclasses, subclasses must call super().get_env(), extend the returned dict and return it.
super().get_env()
Use this to access the env in Spawner.start to allow extension in subclasses.
Save state of spawner into database.
A black box of extra state for custom spawners. The returned value of this is passed to load_state.
Subclasses should call super().get_state(), augment the state returned from there, and return that state.
super().get_state()
state – a JSONable dict of state
move_certs
Takes certificate paths and makes them available to the notebook server
paths (dict) – a list of paths for key, cert, and CA.
paths will be resolvable and readable by the Hub process, (These) –
not necessarily by the notebook server. (but) –
a list (potentially altered) of paths for key, cert, and CA. These paths should be resolvable and readable by the notebook server to be launched.
.move_certs is called after certs for the singleuser notebook have been created by create_certs.
.move_certs
By default, certs are created in a standard, central location defined by internal_certs_location. For a local, single-host deployment of JupyterHub, this should suffice. If, however, singleuser notebooks are spawned on other hosts, .move_certs should be overridden to move these files appropriately. This could mean using scp to copy them to another host, moving them to a volume mounted in a docker container, or exporting them as a secret in kubernetes.
internal_certs_location
scp
options_from_form
Interpret HTTP form data
Form data will always arrive as a dict of lists of strings. Override this function to understand single-values, numbers, etc.
This should coerce form data into the structure expected by self.user_options, which must be a dict, and should be JSON-serializeable, though it can contain bytes in addition to standard JSON data types.
This method should not have any side effects. Any handling of user_options should be done in .start() to ensure consistent behavior across servers spawned via the API and form submission page.
user_options
.start()
Instances will receive this data on self.user_options, after passing through this function, prior to Spawner.start.
Spawner.start
Changed in version 1.0: user_options are persisted in the JupyterHub database to be reused on subsequent spawns if no options are given. user_options is serialized to JSON as part of this persistence (with additional support for bytes in case of uploaded file data), and any non-bytes non-jsonable values will be replaced with None if the user_options are re-used.
Check if the single-user process is running
None if single-user process is running. Integer exit status (0 if unknown), if it is not running.
State transitions, behavior, and return response:
If the Spawner has not been initialized (neither loaded state, nor called start), it should behave as if it is not running (status=0).
If the Spawner has not finished starting, it should behave as if it is running (status=None).
Design assumptions about when poll may be called:
On Hub launch: poll may be called before start when state is loaded on Hub launch. poll should return exit status 0 (unknown) if the Spawner has not been initialized via load_state or start.
If .start() is async: poll may be called during any yielded portions of the start process. poll should return None when start is yielded, indicating that the start process has not yet completed.
Start the single-user server
the (ip, port) where the Hub can connect to the server.
(str, int)
Changed in version 0.7: Return ip, port instead of setting on self.user.server directly.
Stop the single-user server
If now is False (default), shutdown the server as gracefully as possible, e.g. starting with SIGINT, then SIGTERM, then SIGKILL. If now is True, terminate the server immediately.
now
The coroutine should return when the single-user server process is no longer running.
Must be a coroutine.
template_namespace
Return the template namespace for format-string formatting.
Currently used on default_url and notebook_dir.
Subclasses may add items to the available namespace.
The default implementation includes:
{ 'username': user.name, 'base_url': users_base_url, }
namespace for string formatting.
ns (dict)
A Spawner that uses subprocess.Popen to start single-user servers as local processes.
subprocess.Popen
Requires local UNIX users matching the authenticated users to exist. Does not work on Windows.
This is the default spawner for JupyterHub.
Note: This spawner does not implement CPU / memory guarantees and limits.
interrupt_timeout
Seconds to wait for single-user server process to halt after SIGINT.
If the process has not exited cleanly after this many seconds, a SIGTERM is sent.
kill_timeout
Seconds to wait for process to halt after SIGKILL before giving up.
If the process does not exit cleanly after this many seconds of SIGKILL, it becomes a zombie process. The hub process will log a warning and then give up.
popen_kwargs
Extra keyword arguments to pass to Popen
when spawning single-user servers.
popen_kwargs = dict(shell=True)
shell_cmd
Specify a shell command to launch.
The single-user command will be appended to this list, so it sould end with -c (for bash) or equivalent.
-c
c.LocalProcessSpawner.shell_cmd = ['bash', '-l', '-c']
to launch with a bash login shell, which would set up the user’s own complete environment.
Warning
Using shell_cmd gives users control over PATH, etc., which could change what the jupyterhub-singleuser launch command does. Only use this for trusted users.
term_timeout
Seconds to wait for single-user server process to halt after SIGTERM.
If the process does not exit cleanly after this many seconds of SIGTERM, a SIGKILL is sent.