diff --git a/python-toolbox/marvin_python_toolbox/management/notebook.py b/python-toolbox/marvin_python_toolbox/management/notebook.py index 3e748d2a..8dd118c0 100644 --- a/python-toolbox/marvin_python_toolbox/management/notebook.py +++ b/python-toolbox/marvin_python_toolbox/management/notebook.py @@ -29,15 +29,15 @@ def cli(): @cli.command('notebook', help='Start the Jupyter notebook server.') @click.option('--port', '-p', default=8888, help='Jupyter server port') +@click.option('--base-url', default='/', help='Jupyter server base url') @click.option('--enable-security', is_flag=True, help='Enable jupyter notebook token security.') @click.option('--spark-conf', '-c', envvar='SPARK_CONF_DIR', type=click.Path(exists=True), help='Spark configuration folder path to be used in this session') @click.option('--allow-root', is_flag=True, help='Run notebook from root user.') @click.pass_context -def notebook_cli(ctx, port, enable_security, spark_conf, allow_root): - notebook(ctx, port, enable_security, spark_conf, allow_root) +def notebook_cli(ctx, port, base_url, enable_security, spark_conf, allow_root): + notebook(ctx, port, base_url, enable_security, spark_conf, allow_root) - -def notebook(ctx, port, enable_security, spark_conf, allow_root): +def notebook(ctx, port, base_url, enable_security, spark_conf, allow_root): notebookdir = os.path.join(ctx.obj['base_path'], 'notebooks') command = [ "SPARK_CONF_DIR={0} YARN_CONF_DIR={0}".format(spark_conf if spark_conf else os.path.join(os.environ["SPARK_HOME"], "conf")), @@ -49,6 +49,7 @@ def notebook(ctx, port, enable_security, spark_conf, allow_root): '--config', os.path.join(os.environ["MARVIN_TOOLBOX_PATH"], 'extras', 'notebook_extensions', 'jupyter_notebook_config.py') ] + command.append("--NotebookApp.base_url=" + base_url) command.append("--NotebookApp.token=") if not enable_security else None command.append("--allow-root") if allow_root else None @@ -58,14 +59,15 @@ def notebook(ctx, port, enable_security, spark_conf, allow_root): @cli.command('lab', help='Start the JupyterLab server.') @click.option('--port', '-p', default=8888, help='JupyterLab server port') +@click.option('--base-url', default='/', help='Jupyter server base url') @click.option('--enable-security', is_flag=True, help='Enable jupyterlab token security.') @click.option('--spark-conf', '-c', envvar='SPARK_CONF_DIR', type=click.Path(exists=True), help='Spark configuration folder path to be used in this session') @click.pass_context -def lab_cli(ctx, port, enable_security, spark_conf): - lab(ctx, port, enable_security, spark_conf) +def lab_cli(ctx, port, base_url, enable_security, spark_conf): + lab(ctx, port, base_url, enable_security, spark_conf) -def lab(ctx, port, enable_security, spark_conf): +def lab(ctx, port, base_url, enable_security, spark_conf): notebookdir = os.path.join(ctx.obj['base_path'], 'notebooks') command = [ "SPARK_CONF_DIR={0} YARN_CONF_DIR={0}".format(spark_conf if spark_conf else os.path.join(os.environ["SPARK_HOME"], "conf")), @@ -76,7 +78,8 @@ def lab(ctx, port, enable_security, spark_conf): '--no-browser', ] + command.append("--NotebookApp.base_url=" + base_url) command.append("--NotebookApp.token=") if not enable_security else None ret = os.system(' '.join(command)) - sys.exit(ret) \ No newline at end of file + sys.exit(ret) diff --git a/python-toolbox/tests/management/test_notebook.py b/python-toolbox/tests/management/test_notebook.py index 0c66eb7d..adf62f41 100644 --- a/python-toolbox/tests/management/test_notebook.py +++ b/python-toolbox/tests/management/test_notebook.py @@ -40,7 +40,7 @@ def test_notebook(system_mocked, sys_mocked): spark_conf = '/opt/spark/conf' system_mocked.return_value = 1 - notebook(ctx, port, enable_security, spark_conf, allow_root) + notebook(ctx, port, '/', enable_security, spark_conf, allow_root) system_mocked.assert_called_once_with('SPARK_CONF_DIR=/opt/spark/conf YARN_CONF_DIR=/opt/spark/conf jupyter notebook --notebook-dir /tmp/notebooks --ip 0.0.0.0 --port 8888 --no-browser --config ' + os.environ["MARVIN_ENGINE_PATH"] + '/marvin_python_toolbox/extras/notebook_extensions/jupyter_notebook_config.py --NotebookApp.token=') @@ -55,7 +55,7 @@ def test_notebook_with_security(system_mocked, sys_mocked): spark_conf = '/opt/spark/conf' system_mocked.return_value = 1 - notebook(ctx, port, enable_security, spark_conf, allow_root) + notebook(ctx, port, '/', enable_security, spark_conf, allow_root) system_mocked.assert_called_once_with('SPARK_CONF_DIR=/opt/spark/conf YARN_CONF_DIR=/opt/spark/conf jupyter notebook --notebook-dir /tmp/notebooks --ip 0.0.0.0 --port 8888 --no-browser --config ' + os.environ["MARVIN_ENGINE_PATH"] + '/marvin_python_toolbox/extras/notebook_extensions/jupyter_notebook_config.py') @@ -69,7 +69,7 @@ def test_jupyter_lab(system_mocked, sys_mocked): spark_conf = '/opt/spark/conf' system_mocked.return_value = 1 - lab(ctx, port, enable_security, spark_conf) + lab(ctx, port, '/', enable_security, spark_conf) system_mocked.assert_called_once_with('SPARK_CONF_DIR=/opt/spark/conf YARN_CONF_DIR=/opt/spark/conf jupyter-lab --notebook-dir /tmp/notebooks --ip 0.0.0.0 --port 8888 --no-browser --NotebookApp.token=') @@ -83,6 +83,6 @@ def test_jupyter_lab_with_security(system_mocked, sys_mocked): spark_conf = '/opt/spark/conf' system_mocked.return_value = 1 - lab(ctx, port, enable_security, spark_conf) + lab(ctx, port, '/', enable_security, spark_conf) system_mocked.assert_called_once_with('SPARK_CONF_DIR=/opt/spark/conf YARN_CONF_DIR=/opt/spark/conf jupyter-lab --notebook-dir /tmp/notebooks --ip 0.0.0.0 --port 8888 --no-browser')