3636from traitlets .config .configurable import Configurable
3737
3838
39+ class SubConfigurable (Configurable ):
40+ subvalue = Int (0 , help = "The integer subvalue." ).tag (config = True )
41+
42+ def describe (self ):
43+ print ("I am SubConfigurable with:" )
44+ print (" subvalue =" , self .subvalue )
45+
46+
3947class Foo (Configurable ):
4048 """A class that has configurable, typed attributes."""
4149
@@ -44,9 +52,39 @@ class Foo(Configurable):
4452 name = Unicode ("Brian" , help = "First name." ).tag (config = True , shortname = "B" )
4553 mode = Enum (values = ["on" , "off" , "other" ], default_value = "on" ).tag (config = True )
4654
55+ def __init__ (self , ** kwargs ):
56+ super ().__init__ (** kwargs )
57+ # using parent=self allows configuration in the form c.Foo.SubConfigurable.subvalue=1
58+ # while c.SubConfigurable.subvalue=1 will still work, this allow to
59+ # target specific instances of SubConfigurables
60+ self .subconf = SubConfigurable (parent = self )
61+
62+ def describe (self ):
63+ print ("I am Foo with:" )
64+ print (" i =" , self .i )
65+ print (" j =" , self .j )
66+ print (" name =" , self .name )
67+ print (" mode =" , self .mode )
68+ self .subconf .describe ()
69+
4770
4871class Bar (Configurable ):
4972 enabled = Bool (True , help = "Enable bar." ).tag (config = True )
73+ mylist = List ([1 , 2 , 3 ], help = "Just a list." ).tag (config = True )
74+
75+ def describe (self ):
76+ print ("I am Bar with:" )
77+ print (" enabled = " , self .enabled )
78+ print (" mylist = " , self .mylist )
79+ self .subconf .describe ()
80+
81+ def __init__ (self , ** kwargs ):
82+ super ().__init__ (** kwargs )
83+ # here we do not use parent=self, so configuration in the form
84+ # c.Bar.SubConfigurable.subvalue=1 will not work. Only
85+ # c.SubConfigurable.subvalue=1 will work and affect all instances of
86+ # SubConfigurable
87+ self .subconf = SubConfigurable (config = self .config )
5088
5189
5290class MyApp (Application ):
@@ -76,8 +114,8 @@ class MyApp(Application):
76114 )
77115
78116 def init_foo (self ):
79- # Pass config to other classes for them to inherit the config.
80- self .foo = Foo (config = self . config )
117+ # You can pass self as parent to automatically propagate config.
118+ self .foo = Foo (parent = self )
81119
82120 def init_bar (self ):
83121 # Pass config to other classes for them to inherit the config.
@@ -87,19 +125,26 @@ def initialize(self, argv=None):
87125 self .parse_command_line (argv )
88126 if self .config_file :
89127 self .load_config_file (self .config_file )
128+ self .load_config_environ ()
90129 self .init_foo ()
91130 self .init_bar ()
92131
93132 def start (self ):
94133 print ("app.config:" )
95134 print (self .config )
135+ self .describe ()
96136 print ("try running with --help-all to see all available flags" )
97137 assert self .log is not None
98138 self .log .debug ("Debug Message" )
99139 self .log .info ("Info Message" )
100140 self .log .warning ("Warning Message" )
101141 self .log .critical ("Critical Message" )
102142
143+ def describe (self ):
144+ print ("I am MyApp with" , self .name , self .running , "and 2 sub configurables Foo and bar:" )
145+ self .foo .describe ()
146+ self .bar .describe ()
147+
103148
104149def main ():
105150 app = MyApp ()
0 commit comments