Adding a custom configuration parameter

85 views
Skip to first unread message

Steven Anton

unread,
Sep 26, 2016, 12:47:17 PM9/26/16
to Project Jupyter
Hi everyone,

I have a custom ContentsManager class that I'd like to configure using the standard Jupyter tools. In particular, I'm looking for something along the lines of adding a line like "c.MyContentsManager.parameter = 'value'" to the jupyter_notebook_config.py and reading that parameter in the custom ContentsManager. I'm hoping this is an easy task, but I can't seem to find the relevant functions in the jupyter source code. Maybe something like:

# source code for the custom ContentsManager

from
notebook.services.config import ConfigManager

cm
= ConfigManager()

cm
.get("MyContentsManager").get("parameter", "default")

Does anyone have ideas on how to accomplish this?

Thanks so much!
Steve

Matthias Bussonnier

unread,
Sep 26, 2016, 1:00:40 PM9/26/16
to jup...@googlegroups.com
Hi Steve,

Quickly handwaved:

```
from traitlets import Configurable, Unicode


class MyCustomContentManager(Configurable):



# the class attribute will be AutoMagically be made a instance one
and accessible via self.parameter.
parameter = Unicode('default', config=True, help='Frobulate
parameter as Unicode from the command line or config files.')

def __init__(self, ..., parent=None, config=None):
super().__init__(...,parent=parent, config=config) # important
...
```


You can set the value of parameter with
`c.MyCustomContentManager.parameter = 'Whatever'` in any config file.

This pattern is in many places of our codebase[1] and uses metaclasses.

If your content manager inherit from ours , then
`c.ContentManager.parameter = 'Whatever'` should work as well.

Does that make some sens ? There is a bit more magic involved if you
want advanced features, but I guess that might be already a lot to
digest.

Cheers,
--
M


[1]: https://github.com/jupyter/notebook/blob/master/notebook/services/contents/manager.py#L65-L95
> --
> You received this message because you are subscribed to the Google Groups
> "Project Jupyter" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jupyter+u...@googlegroups.com.
> To post to this group, send email to jup...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jupyter/193be8d9-3321-4dca-b058-6c9ce266355b%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Steven Anton

unread,
Sep 26, 2016, 1:05:32 PM9/26/16
to Project Jupyter
Hi Matthias, yes that makes perfect sense. Also, I just noticed this page, which has a really good explanation. I don't know why I couldn't find that before when I was searching. Thanks again!

Matthias Bussonnier

unread,
Sep 26, 2016, 1:06:35 PM9/26/16
to jup...@googlegroups.com
Great !

Looking FWD to see your custom content manager !
--
M
> https://groups.google.com/d/msgid/jupyter/244fcb2c-574a-4ad1-8e43-29f4bb19e4bf%40googlegroups.com.

Steven Anton

unread,
Sep 26, 2016, 1:29:05 PM9/26/16
to Project Jupyter
I'd actually love to commit it back to OS. We deal with a lot of PII so having plain-text notebook formats is very bad for us. To get around this, we have a contents manager that encrypts notebooks to gpg files. I imagine this is generally useful, but I don't know if we will ever be able to publish it because of security concerns.

Matthias Bussonnier

unread,
Sep 26, 2016, 1:33:18 PM9/26/16
to jup...@googlegroups.com
Hi Steve,

Thanks, this is completely understandable, even just your feedback on
API, various hooks and requests for clarification is helpful.
If there are some questions that you are afraid to ask on public
mailing list, feel free to contact any of us directly.

If it's security related, you can even contact secu...@ipython.org.

Thanks,
--
Matthias
> https://groups.google.com/d/msgid/jupyter/27341795-35d2-43b4-99f8-627af80634d7%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages