From 647dd09f40f108450f6e581c1e32d67eebbf605d Mon Sep 17 00:00:00 2001 From: Min RK Date: Fri, 18 Dec 2015 11:16:40 +0100 Subject: [PATCH] add spawn-form example --- examples/spawn-form/jupyterhub_config.py | 46 ++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 examples/spawn-form/jupyterhub_config.py diff --git a/examples/spawn-form/jupyterhub_config.py b/examples/spawn-form/jupyterhub_config.py new file mode 100644 index 00000000..0ed1bf1e --- /dev/null +++ b/examples/spawn-form/jupyterhub_config.py @@ -0,0 +1,46 @@ +""" +Example JuptyerHub config allowing users to specify environment variables and notebook-server args +""" +import shlex + +from jupyterhub.spawner import LocalProcessSpawner + +class DemoFormSpawner(LocalProcessSpawner): + def _options_form_default(self): + default_env = "YOURNAME=%s\n" % self.user.name + return """ + + + + + """.format(env=default_env) + + def options_from_form(self, formdata): + options = {} + options['env'] = env = {} + + env_lines = formdata.get('env', ['']) + for line in env_lines[0].splitlines(): + if line: + key, value = line.split('=', 1) + env[key.strip()] = value.strip() + + arg_s = formdata.get('args', [''])[0].strip() + if arg_s: + options['argv'] = shlex.split(arg_s) + return options + + def get_args(self): + """Return arguments to pass to the notebook server""" + argv = super().get_args() + if self.user_options.get('argv'): + argv.extend(self.user_options['argv']) + return argv + + def get_env(self): + env = super().get_env() + if self.user_options.get('env'): + env.update(self.user_options['env']) + return env + +c.JupyterHub.spawner_class = DemoFormSpawner