# Spawners¶

## Module: jupyterhub.spawner¶

Contains base Spawner class & default implementation

### Spawner¶

class jupyterhub.spawner.Spawner(**kwargs)

Base class for spawning single-user notebook servers.

Subclass this, and override the following methods:

• 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.

config c.Spawner.args = List()

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!

config c.Spawner.cmd = Command()

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!

config c.Spawner.consecutive_failure_limit = Int(0)

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.

config c.Spawner.cpu_guarantee = Float(None)

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.

config c.Spawner.cpu_limit = Float(None)

Maximum number of cpu-cores a single-user notebook server is allowed to use.

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.

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.

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.

config c.Spawner.debug = Bool(False)

Enable debug-logging of the single-user server

config c.Spawner.default_url = Unicode('')

The URL the single-user server should start in.

{username} will be expanded to the user’s 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.
• Start with /notebooks instead of /tree if default_url points to a notebook instead of a directory.
• You can set this to /lab to have JupyterLab start by default, rather than Jupyter Notebook.
config c.Spawner.disable_user_config = Bool(False)

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. config c.Spawner.env_keep = List() 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. config c.Spawner.environment = Dict() Extra environment variables to set for the single-user server’s process. Environment variables that end up in the single-user server’s process come from 3 sources: • 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! config c.Spawner.http_timeout = Int(30) 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. config c.Spawner.ip = Unicode('') 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. config c.Spawner.mem_guarantee = ByteSpecification(None) Minimum number of bytes a single-user notebook server is guaranteed to have available. Allows the following suffixes: • K -> Kilobytes • M -> Megabytes • G -> Gigabytes • T -> Terabytes 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. config c.Spawner.mem_limit = ByteSpecification(None) Maximum number of bytes a single-user notebook server is allowed to use. Allows the following suffixes: • K -> Kilobytes • M -> Megabytes • G -> Gigabytes • T -> Terabytes 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. 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. config c.Spawner.notebook_dir = Unicode('') 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. config c.Spawner.options_form = Union() An HTML form for options a user can specify on launching their server. The surrounding <form> element and the submit button are already provided. 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 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. config c.Spawner.poll_interval = Int(30) 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. config c.Spawner.port = Int(0) The port for single-user servers to listen on. Defaults to 0, which uses a randomly allocated port number each time. 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. config c.Spawner.post_stop_hook = Any(None) 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. config c.Spawner.pre_spawn_hook = Any(None) 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 can be set independent of any concrete spawner implementation. Example: 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  config c.Spawner.start_timeout = Int(60) 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. format_string(s) Render a Python format string Uses Spawner.template_namespace() to populate format namespace. Parameters: 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. When overriding in subclasses, subclasses must call super().get_env(), extend the returned dict and return it. Use this to access the env in Spawner.start to allow extension in subclasses. get_state() 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. Returns: state – a JSONable dict of state dict options_from_form(form_data) 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. Instances will receive this data on self.user_options, after passing through this function, prior to Spawner.start. poll() Check if the single-user process is running Returns: 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() Start the single-user server Returns: 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(now=False) 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. 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, }  Returns: namespace for string formatting. ns (dict) ### LocalProcessSpawner¶ class jupyterhub.spawner.LocalProcessSpawner(**kwargs) A Spawner that uses subprocess.Popen to start single-user servers as local processes. 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. config c.LocalProcessSpawner.args = List() 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! config c.LocalProcessSpawner.cmd = Command() 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! config c.LocalProcessSpawner.consecutive_failure_limit = Int(0) 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. config c.LocalProcessSpawner.cpu_guarantee = Float(None) 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. config c.LocalProcessSpawner.cpu_limit = Float(None) Maximum number of cpu-cores a single-user notebook server is allowed to use. 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. 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. 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. config c.LocalProcessSpawner.debug = Bool(False) Enable debug-logging of the single-user server config c.LocalProcessSpawner.default_url = Unicode('') The URL the single-user server should start in. {username} will be expanded to the user’s 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. • Start with /notebooks instead of /tree if default_url points to a notebook instead of a directory. • You can set this to /lab to have JupyterLab start by default, rather than Jupyter Notebook. config c.LocalProcessSpawner.disable_user_config = Bool(False) 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.

config c.LocalProcessSpawner.env_keep = List()

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.

config c.LocalProcessSpawner.environment = Dict()

Extra environment variables to set for the single-user server’s process.

Environment variables that end up in the single-user server’s process come from 3 sources:
• 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!

config c.LocalProcessSpawner.http_timeout = Int(30)

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.

config c.LocalProcessSpawner.interrupt_timeout = Int(10)

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.

config c.LocalProcessSpawner.ip = Unicode('')

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.

config c.LocalProcessSpawner.kill_timeout = Int(5)

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.

config c.LocalProcessSpawner.mem_guarantee = ByteSpecification(None)

Minimum number of bytes a single-user notebook server is guaranteed to have available.

Allows the following suffixes:
• K -> Kilobytes
• M -> Megabytes
• G -> Gigabytes
• T -> Terabytes

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.

config c.LocalProcessSpawner.mem_limit = ByteSpecification(None)

Maximum number of bytes a single-user notebook server is allowed to use.

Allows the following suffixes:
• K -> Kilobytes
• M -> Megabytes
• G -> Gigabytes
• T -> Terabytes

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.

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.

config c.LocalProcessSpawner.notebook_dir = Unicode('')

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.

config c.LocalProcessSpawner.options_form = Union()

An HTML form for options a user can specify on launching their server.

The surrounding <form> element and the submit button are already provided.

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

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.

config c.LocalProcessSpawner.poll_interval = Int(30)

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.

config c.LocalProcessSpawner.popen_kwargs = Dict()

Extra keyword arguments to pass to Popen

when spawning single-user servers.

For example:

popen_kwargs = dict(shell=True)

config c.LocalProcessSpawner.port = Int(0)

The port for single-user servers to listen on.

Defaults to 0, which uses a randomly allocated port number each time.

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.

config c.LocalProcessSpawner.post_stop_hook = Any(None)

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.

config c.LocalProcessSpawner.pre_spawn_hook = Any(None)

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 can be set independent of any concrete spawner implementation.

Example:

from subprocess import check_call
def my_hook(spawner):

c.Spawner.pre_spawn_hook = my_hook

config c.LocalProcessSpawner.shell_cmd = Command()

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.

For example:

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.

config c.LocalProcessSpawner.start_timeout = Int(60)

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.

config c.LocalProcessSpawner.term_timeout = Int(5)

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.