jupyterhub.spawner
¶Contains base Spawner class & default implementation
Spawner
¶jupyterhub.spawner.
Spawner
(**kwargs)¶Base class for spawning single-user notebook servers.
Subclass this, and override the following methods:
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.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.
Note that this needs to be supported by your spawner for it to work.
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 needs to be supported by your spawner for it to work.
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:
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./notebooks
instead of /tree
if default_url
points to a notebook instead of a directory./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
configurableenv_keep
The enviornment
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.
This needs to be supported by your spawner for it to work.
config c.Spawner.mem_limit = ByteSpecification(None)
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.
This needs to be supported by your spawner for it to work.
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 = Unicode('')
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
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.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. |
---|---|
Returns: | Formatted string, rendered |
Return type: | 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 |
---|---|
Return type: | 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:
Design assumptions about when poll
may be called:
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
..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. |
---|---|
Return type: | (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. |
---|---|
Return type: | ns (dict) |
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.
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.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.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.
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.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.
Note that this needs to be supported by your spawner for it to work.
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 needs to be supported by your spawner for it to work.
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:
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./notebooks
instead of /tree
if default_url
points to a notebook instead of a directory./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
configurableenv_keep
The enviornment
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.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.mem_guarantee = ByteSpecification(None)
Minimum number of bytes a single-user notebook server is guaranteed to have available.
This needs to be supported by your spawner for it to work.
config c.LocalProcessSpawner.mem_limit = ByteSpecification(None)
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.
This needs to be supported by your spawner for it to work.
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 = Unicode('')
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
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.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.