Passing Interface Extension Settings via Command Line?

I’m looking for a way to pass some information from the command line invocation of jupyter lab to an extension. Is there a way to pass settings directly to the user settings other than overrides.json? I see that in the API there’s an option to pass –-LabApp.settings but I haven’t been able to set any user settings with this, and there’s –-LabApp.user_settings_dir but I don’t want to ignore any existing user settings.

Since I’m also setting up the ipython kernel from which this notebook is run, my current approach is to set a hidden variable in the state which I can query by sending an execution to the kernel from the extension, but this is pretty messy. Is the other option to procedurally edit the user’s overrides.json?

Thank you for the help!

In general, it’s not easy to pass an argument from the command line to the extension. It requires you to create the extension as a sever extension, and then pass the information from the python side to the JS side using a traitlet.

Basics of making a server app: extension-examples/server-extension at main · jupyterlab/extension-examples · GitHub

Set up the basic server app stuff:
__init__.py:

from ._version import __version__
from .server import MyApp


def _jupyter_labextension_paths():
    return [{"src": "labextension", "dest": "myapp"}]


def _jupyter_server_extension_points():
    return [
        {
            "module": "myapp",
            "app": MyApp,     # <- Note this is not quoted.  This is the module
            "name": "myapp",
        }
    ]

Replace the server app with what you are doing. In the start-up section of the app, you can assign a variable to be shared with the JS side:
server.py:

from jupyter_server.extension.application 
import ExtensionApp
from traitlets import Bool

class MyApp(ExtensionApp):    
    name = "myapp"
    myflag = Bool(False, help="my setting goes here").tag(config=True)

    def initialize_settings(self):
		super().initialize_settings()
        web_app = self.serverapp.web_app
        settings = web_app.settings
		page_config = settings.setdefault("page_config_data", {})
		page_config["my_app_my_flag"] = self.myflag
...

Then, in the JS/TS side of your extension, you can retrieve the value:

index.ts:

...

import { PageConfig } from '@jupyterlab/coreutils';

... (some more code)

const myflag = PageConfig.getOption('my_app_my_flag') === 'true';

...  (other code)

(which assumes your variable myflag is a boolean type. Adjust for something else)

Obviously, it’s not finished baking without some extra steps. You need to follow the rest of the server app example about the files required and where to deploy them, and which parts of the build steps are required.

When it’s complete, then arguments can be passed on the command line like:

jupyter lab --MyApp.myflag=True ...

4 Likes

Excellent! Thank you so much, this was exactly what I needed, but was having trouble piecing this all together from the different docs.

2 Likes

In case anyone stumbles upon this thread, I’ve since found some other ways of doing this that don’t work for my use-case but could be useful to others: https://stackoverflow.com/questions/37534440/passing-command-line-arguments-to-argv-in-jupyter-ipython-notebook

1 Like