Show More
@@ -1,566 +1,566 | |||||
1 | .. _config_overview: |
|
1 | .. _config_overview: | |
2 |
|
2 | |||
3 | ============================================ |
|
3 | ============================================ | |
4 | Overview of the IPython configuration system |
|
4 | Overview of the IPython configuration system | |
5 | ============================================ |
|
5 | ============================================ | |
6 |
|
6 | |||
7 | This section describes the IPython configuration system. |
|
7 | This section describes the IPython configuration system. | |
8 |
|
8 | |||
9 | The main concepts |
|
9 | The main concepts | |
10 | ================= |
|
10 | ================= | |
11 |
|
11 | |||
12 | There are a number of abstractions that the IPython configuration system uses. |
|
12 | There are a number of abstractions that the IPython configuration system uses. | |
13 | Each of these abstractions is represented by a Python class. |
|
13 | Each of these abstractions is represented by a Python class. | |
14 |
|
14 | |||
15 | Configuration object: :class:`~IPython.config.loader.Config` |
|
15 | Configuration object: :class:`~IPython.config.loader.Config` | |
16 | A configuration object is a simple dictionary-like class that holds |
|
16 | A configuration object is a simple dictionary-like class that holds | |
17 | configuration attributes and sub-configuration objects. These classes |
|
17 | configuration attributes and sub-configuration objects. These classes | |
18 | support dotted attribute style access (``cfg.Foo.bar``) in addition to the |
|
18 | support dotted attribute style access (``cfg.Foo.bar``) in addition to the | |
19 | regular dictionary style access (``cfg['Foo']['bar']``). |
|
19 | regular dictionary style access (``cfg['Foo']['bar']``). | |
20 | The Config object is a wrapper around a simple dictionary with some convenience methods, |
|
20 | The Config object is a wrapper around a simple dictionary with some convenience methods, | |
21 | such as merging and automatic section creation. |
|
21 | such as merging and automatic section creation. | |
22 |
|
22 | |||
23 | Application: :class:`~IPython.config.application.Application` |
|
23 | Application: :class:`~IPython.config.application.Application` | |
24 | An application is a process that does a specific job. The most obvious |
|
24 | An application is a process that does a specific job. The most obvious | |
25 | application is the :command:`ipython` command line program. Each |
|
25 | application is the :command:`ipython` command line program. Each | |
26 | application reads *one or more* configuration files and a single set of |
|
26 | application reads *one or more* configuration files and a single set of | |
27 | command line options |
|
27 | command line options | |
28 | and then produces a master configuration object for the application. This |
|
28 | and then produces a master configuration object for the application. This | |
29 | configuration object is then passed to the configurable objects that the |
|
29 | configuration object is then passed to the configurable objects that the | |
30 | application creates. These configurable objects implement the actual logic |
|
30 | application creates. These configurable objects implement the actual logic | |
31 | of the application and know how to configure themselves given the |
|
31 | of the application and know how to configure themselves given the | |
32 | configuration object. |
|
32 | configuration object. | |
33 |
|
33 | |||
34 | Applications always have a `log` attribute that is a configured Logger. |
|
34 | Applications always have a `log` attribute that is a configured Logger. | |
35 | This allows centralized logging configuration per-application. |
|
35 | This allows centralized logging configuration per-application. | |
36 |
|
36 | |||
37 | Configurable: :class:`~IPython.config.configurable.Configurable` |
|
37 | Configurable: :class:`~IPython.config.configurable.Configurable` | |
38 | A configurable is a regular Python class that serves as a base class for |
|
38 | A configurable is a regular Python class that serves as a base class for | |
39 | all main classes in an application. The |
|
39 | all main classes in an application. The | |
40 | :class:`~IPython.config.configurable.Configurable` base class is |
|
40 | :class:`~IPython.config.configurable.Configurable` base class is | |
41 | lightweight and only does one things. |
|
41 | lightweight and only does one things. | |
42 |
|
42 | |||
43 | This :class:`~IPython.config.configurable.Configurable` is a subclass |
|
43 | This :class:`~IPython.config.configurable.Configurable` is a subclass | |
44 | of :class:`~IPython.utils.traitlets.HasTraits` that knows how to configure |
|
44 | of :class:`~IPython.utils.traitlets.HasTraits` that knows how to configure | |
45 | itself. Class level traits with the metadata ``config=True`` become |
|
45 | itself. Class level traits with the metadata ``config=True`` become | |
46 | values that can be configured from the command line and configuration |
|
46 | values that can be configured from the command line and configuration | |
47 | files. |
|
47 | files. | |
48 |
|
48 | |||
49 | Developers create :class:`~IPython.config.configurable.Configurable` |
|
49 | Developers create :class:`~IPython.config.configurable.Configurable` | |
50 | subclasses that implement all of the logic in the application. Each of |
|
50 | subclasses that implement all of the logic in the application. Each of | |
51 | these subclasses has its own configuration information that controls how |
|
51 | these subclasses has its own configuration information that controls how | |
52 | instances are created. |
|
52 | instances are created. | |
53 |
|
53 | |||
54 | Singletons: :class:`~IPython.config.configurable.SingletonConfigurable` |
|
54 | Singletons: :class:`~IPython.config.configurable.SingletonConfigurable` | |
55 | Any object for which there is a single canonical instance. These are |
|
55 | Any object for which there is a single canonical instance. These are | |
56 | just like Configurables, except they have a class method |
|
56 | just like Configurables, except they have a class method | |
57 | :meth:`~IPython.config.configurable.SingletonConfigurable.instance`, |
|
57 | :meth:`~IPython.config.configurable.SingletonConfigurable.instance`, | |
58 | that returns the current active instance (or creates one if it |
|
58 | that returns the current active instance (or creates one if it | |
59 | does not exist). Examples of singletons include |
|
59 | does not exist). Examples of singletons include | |
60 | :class:`~IPython.config.application.Application`s and |
|
60 | :class:`~IPython.config.application.Application`s and | |
61 | :class:`~IPython.core.interactiveshell.InteractiveShell`. This lets |
|
61 | :class:`~IPython.core.interactiveshell.InteractiveShell`. This lets | |
62 | objects easily connect to the current running Application without passing |
|
62 | objects easily connect to the current running Application without passing | |
63 | objects around everywhere. For instance, to get the current running |
|
63 | objects around everywhere. For instance, to get the current running | |
64 | Application instance, simply do: ``app = Application.instance()``. |
|
64 | Application instance, simply do: ``app = Application.instance()``. | |
65 |
|
65 | |||
66 |
|
66 | |||
67 | .. note:: |
|
67 | .. note:: | |
68 |
|
68 | |||
69 | Singletons are not strictly enforced - you can have many instances |
|
69 | Singletons are not strictly enforced - you can have many instances | |
70 | of a given singleton class, but the :meth:`instance` method will always |
|
70 | of a given singleton class, but the :meth:`instance` method will always | |
71 | return the same one. |
|
71 | return the same one. | |
72 |
|
72 | |||
73 | Having described these main concepts, we can now state the main idea in our |
|
73 | Having described these main concepts, we can now state the main idea in our | |
74 | configuration system: *"configuration" allows the default values of class |
|
74 | configuration system: *"configuration" allows the default values of class | |
75 | attributes to be controlled on a class by class basis*. Thus all instances of |
|
75 | attributes to be controlled on a class by class basis*. Thus all instances of | |
76 | a given class are configured in the same way. Furthermore, if two instances |
|
76 | a given class are configured in the same way. Furthermore, if two instances | |
77 | need to be configured differently, they need to be instances of two different |
|
77 | need to be configured differently, they need to be instances of two different | |
78 | classes. While this model may seem a bit restrictive, we have found that it |
|
78 | classes. While this model may seem a bit restrictive, we have found that it | |
79 | expresses most things that need to be configured extremely well. However, it |
|
79 | expresses most things that need to be configured extremely well. However, it | |
80 | is possible to create two instances of the same class that have different |
|
80 | is possible to create two instances of the same class that have different | |
81 | trait values. This is done by overriding the configuration. |
|
81 | trait values. This is done by overriding the configuration. | |
82 |
|
82 | |||
83 | Now, we show what our configuration objects and files look like. |
|
83 | Now, we show what our configuration objects and files look like. | |
84 |
|
84 | |||
85 | Configuration objects and files |
|
85 | Configuration objects and files | |
86 | =============================== |
|
86 | =============================== | |
87 |
|
87 | |||
88 | A configuration object is little more than a wrapper around a dictionary. |
|
88 | A configuration object is little more than a wrapper around a dictionary. | |
89 | A configuration *file* is simply a mechanism for producing that object. |
|
89 | A configuration *file* is simply a mechanism for producing that object. | |
90 | The main IPython configuration file is a plain Python script, |
|
90 | The main IPython configuration file is a plain Python script, | |
91 | which can perform extensive logic to populate the config object. |
|
91 | which can perform extensive logic to populate the config object. | |
92 | IPython 2.0 introduces a JSON configuration file, |
|
92 | IPython 2.0 introduces a JSON configuration file, | |
93 | which is just a direct JSON serialization of the config dictionary, |
|
93 | which is just a direct JSON serialization of the config dictionary, | |
94 | which is easily processed by external software. |
|
94 | which is easily processed by external software. | |
95 |
|
95 | |||
96 | When both Python and JSON configuration file are present, both will be loaded, |
|
96 | When both Python and JSON configuration file are present, both will be loaded, | |
97 | with JSON configuration having higher priority. |
|
97 | with JSON configuration having higher priority. | |
98 |
|
98 | |||
99 | Python configuration Files |
|
99 | Python configuration Files | |
100 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
100 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
101 |
|
101 | |||
102 | A Python configuration file is a pure Python file that populates a configuration object. |
|
102 | A Python configuration file is a pure Python file that populates a configuration object. | |
103 | This configuration object is a :class:`~IPython.config.loader.Config` instance. |
|
103 | This configuration object is a :class:`~IPython.config.loader.Config` instance. | |
104 | While in a configuration file, to get a reference to this object, simply call the :func:`get_config` |
|
104 | While in a configuration file, to get a reference to this object, simply call the :func:`get_config` | |
105 | function, which is available in the global namespace of the script. |
|
105 | function, which is available in the global namespace of the script. | |
106 |
|
106 | |||
107 | Here is an example of a super simple configuration file that does nothing:: |
|
107 | Here is an example of a super simple configuration file that does nothing:: | |
108 |
|
108 | |||
109 | c = get_config() |
|
109 | c = get_config() | |
110 |
|
110 | |||
111 | Once you get a reference to the configuration object, you simply set |
|
111 | Once you get a reference to the configuration object, you simply set | |
112 | attributes on it. All you have to know is: |
|
112 | attributes on it. All you have to know is: | |
113 |
|
113 | |||
114 | * The name of the class to configure. |
|
114 | * The name of the class to configure. | |
115 | * The name of the attribute. |
|
115 | * The name of the attribute. | |
116 | * The type of each attribute. |
|
116 | * The type of each attribute. | |
117 |
|
117 | |||
118 | The answers to these questions are provided by the various |
|
118 | The answers to these questions are provided by the various | |
119 | :class:`~IPython.config.configurable.Configurable` subclasses that an |
|
119 | :class:`~IPython.config.configurable.Configurable` subclasses that an | |
120 | application uses. Let's look at how this would work for a simple configurable |
|
120 | application uses. Let's look at how this would work for a simple configurable | |
121 | subclass:: |
|
121 | subclass:: | |
122 |
|
122 | |||
123 | # Sample configurable: |
|
123 | # Sample configurable: | |
124 | from IPython.config.configurable import Configurable |
|
124 | from IPython.config.configurable import Configurable | |
125 | from IPython.utils.traitlets import Int, Float, Unicode, Bool |
|
125 | from IPython.utils.traitlets import Int, Float, Unicode, Bool | |
126 |
|
126 | |||
127 | class MyClass(Configurable): |
|
127 | class MyClass(Configurable): | |
128 | name = Unicode(u'defaultname', config=True) |
|
128 | name = Unicode(u'defaultname', config=True) | |
129 | ranking = Int(0, config=True) |
|
129 | ranking = Int(0, config=True) | |
130 | value = Float(99.0) |
|
130 | value = Float(99.0) | |
131 | # The rest of the class implementation would go here.. |
|
131 | # The rest of the class implementation would go here.. | |
132 |
|
132 | |||
133 | In this example, we see that :class:`MyClass` has three attributes, two |
|
133 | In this example, we see that :class:`MyClass` has three attributes, two | |
134 | of which (``name``, ``ranking``) can be configured. All of the attributes |
|
134 | of which (``name``, ``ranking``) can be configured. All of the attributes | |
135 | are given types and default values. If a :class:`MyClass` is instantiated, |
|
135 | are given types and default values. If a :class:`MyClass` is instantiated, | |
136 | but not configured, these default values will be used. But let's see how |
|
136 | but not configured, these default values will be used. But let's see how | |
137 | to configure this class in a configuration file:: |
|
137 | to configure this class in a configuration file:: | |
138 |
|
138 | |||
139 | # Sample config file |
|
139 | # Sample config file | |
140 | c = get_config() |
|
140 | c = get_config() | |
141 |
|
141 | |||
142 | c.MyClass.name = 'coolname' |
|
142 | c.MyClass.name = 'coolname' | |
143 | c.MyClass.ranking = 10 |
|
143 | c.MyClass.ranking = 10 | |
144 |
|
144 | |||
145 | After this configuration file is loaded, the values set in it will override |
|
145 | After this configuration file is loaded, the values set in it will override | |
146 | the class defaults anytime a :class:`MyClass` is created. Furthermore, |
|
146 | the class defaults anytime a :class:`MyClass` is created. Furthermore, | |
147 | these attributes will be type checked and validated anytime they are set. |
|
147 | these attributes will be type checked and validated anytime they are set. | |
148 | This type checking is handled by the :mod:`IPython.utils.traitlets` module, |
|
148 | This type checking is handled by the :mod:`IPython.utils.traitlets` module, | |
149 | which provides the :class:`Unicode`, :class:`Int` and :class:`Float` types. |
|
149 | which provides the :class:`Unicode`, :class:`Int` and :class:`Float` types. | |
150 | In addition to these traitlets, the :mod:`IPython.utils.traitlets` provides |
|
150 | In addition to these traitlets, the :mod:`IPython.utils.traitlets` provides | |
151 | traitlets for a number of other types. |
|
151 | traitlets for a number of other types. | |
152 |
|
152 | |||
153 | .. note:: |
|
153 | .. note:: | |
154 |
|
154 | |||
155 | Underneath the hood, the :class:`Configurable` base class is a subclass of |
|
155 | Underneath the hood, the :class:`Configurable` base class is a subclass of | |
156 | :class:`IPython.utils.traitlets.HasTraits`. The |
|
156 | :class:`IPython.utils.traitlets.HasTraits`. The | |
157 | :mod:`IPython.utils.traitlets` module is a lightweight version of |
|
157 | :mod:`IPython.utils.traitlets` module is a lightweight version of | |
158 | :mod:`enthought.traits`. Our implementation is a pure Python subset |
|
158 | :mod:`enthought.traits`. Our implementation is a pure Python subset | |
159 | (mostly API compatible) of :mod:`enthought.traits` that does not have any |
|
159 | (mostly API compatible) of :mod:`enthought.traits` that does not have any | |
160 | of the automatic GUI generation capabilities. Our plan is to achieve 100% |
|
160 | of the automatic GUI generation capabilities. Our plan is to achieve 100% | |
161 | API compatibility to enable the actual :mod:`enthought.traits` to |
|
161 | API compatibility to enable the actual :mod:`enthought.traits` to | |
162 | eventually be used instead. Currently, we cannot use |
|
162 | eventually be used instead. Currently, we cannot use | |
163 | :mod:`enthought.traits` as we are committed to the core of IPython being |
|
163 | :mod:`enthought.traits` as we are committed to the core of IPython being | |
164 | pure Python. |
|
164 | pure Python. | |
165 |
|
165 | |||
166 | It should be very clear at this point what the naming convention is for |
|
166 | It should be very clear at this point what the naming convention is for | |
167 | configuration attributes:: |
|
167 | configuration attributes:: | |
168 |
|
168 | |||
169 | c.ClassName.attribute_name = attribute_value |
|
169 | c.ClassName.attribute_name = attribute_value | |
170 |
|
170 | |||
171 | Here, ``ClassName`` is the name of the class whose configuration attribute you |
|
171 | Here, ``ClassName`` is the name of the class whose configuration attribute you | |
172 | want to set, ``attribute_name`` is the name of the attribute you want to set |
|
172 | want to set, ``attribute_name`` is the name of the attribute you want to set | |
173 | and ``attribute_value`` the the value you want it to have. The ``ClassName`` |
|
173 | and ``attribute_value`` the the value you want it to have. The ``ClassName`` | |
174 | attribute of ``c`` is not the actual class, but instead is another |
|
174 | attribute of ``c`` is not the actual class, but instead is another | |
175 | :class:`~IPython.config.loader.Config` instance. |
|
175 | :class:`~IPython.config.loader.Config` instance. | |
176 |
|
176 | |||
177 | .. note:: |
|
177 | .. note:: | |
178 |
|
178 | |||
179 | The careful reader may wonder how the ``ClassName`` (``MyClass`` in |
|
179 | The careful reader may wonder how the ``ClassName`` (``MyClass`` in | |
180 | the above example) attribute of the configuration object ``c`` gets |
|
180 | the above example) attribute of the configuration object ``c`` gets | |
181 | created. These attributes are created on the fly by the |
|
181 | created. These attributes are created on the fly by the | |
182 | :class:`~IPython.config.loader.Config` instance, using a simple naming |
|
182 | :class:`~IPython.config.loader.Config` instance, using a simple naming | |
183 | convention. Any attribute of a :class:`~IPython.config.loader.Config` |
|
183 | convention. Any attribute of a :class:`~IPython.config.loader.Config` | |
184 | instance whose name begins with an uppercase character is assumed to be a |
|
184 | instance whose name begins with an uppercase character is assumed to be a | |
185 | sub-configuration and a new empty :class:`~IPython.config.loader.Config` |
|
185 | sub-configuration and a new empty :class:`~IPython.config.loader.Config` | |
186 | instance is dynamically created for that attribute. This allows deeply |
|
186 | instance is dynamically created for that attribute. This allows deeply | |
187 | hierarchical information created easily (``c.Foo.Bar.value``) on the fly. |
|
187 | hierarchical information created easily (``c.Foo.Bar.value``) on the fly. | |
188 |
|
188 | |||
189 | JSON configuration Files |
|
189 | JSON configuration Files | |
190 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
|
190 | ~~~~~~~~~~~~~~~~~~~~~~~~ | |
191 |
|
191 | |||
192 | A JSON configuration file is simply a file that contains a |
|
192 | A JSON configuration file is simply a file that contains a | |
193 | :class:`~IPython.config.loader.Config` dictionary serialized to JSON. |
|
193 | :class:`~IPython.config.loader.Config` dictionary serialized to JSON. | |
194 | A JSON configuration file has the same base name as a Python configuration file, |
|
194 | A JSON configuration file has the same base name as a Python configuration file, | |
195 | but with a .json extension. |
|
195 | but with a .json extension. | |
196 |
|
196 | |||
197 | Configuration described in previous section could be written as follows in a |
|
197 | Configuration described in previous section could be written as follows in a | |
198 | JSON configuration file: |
|
198 | JSON configuration file: | |
199 |
|
199 | |||
200 | .. sourcecode:: json |
|
200 | .. sourcecode:: json | |
201 |
|
201 | |||
202 | { |
|
202 | { | |
203 | "version": "1.0", |
|
203 | "version": "1.0", | |
204 | "MyClass": { |
|
204 | "MyClass": { | |
205 | "name": "coolname", |
|
205 | "name": "coolname", | |
206 | "ranking": 10 |
|
206 | "ranking": 10 | |
207 | } |
|
207 | } | |
208 | } |
|
208 | } | |
209 |
|
209 | |||
210 | JSON configuration files can be more easily generated or processed by programs |
|
210 | JSON configuration files can be more easily generated or processed by programs | |
211 | or other languages. |
|
211 | or other languages. | |
212 |
|
212 | |||
213 |
|
213 | |||
214 | Configuration files inheritance |
|
214 | Configuration files inheritance | |
215 | =============================== |
|
215 | =============================== | |
216 |
|
216 | |||
217 | .. note:: |
|
217 | .. note:: | |
218 |
|
218 | |||
219 | This section only apply to Python configuration files. |
|
219 | This section only apply to Python configuration files. | |
220 |
|
220 | |||
221 | Let's say you want to have different configuration files for various purposes. |
|
221 | Let's say you want to have different configuration files for various purposes. | |
222 | Our configuration system makes it easy for one configuration file to inherit |
|
222 | Our configuration system makes it easy for one configuration file to inherit | |
223 | the information in another configuration file. The :func:`load_subconfig` |
|
223 | the information in another configuration file. The :func:`load_subconfig` | |
224 | command can be used in a configuration file for this purpose. Here is a simple |
|
224 | command can be used in a configuration file for this purpose. Here is a simple | |
225 | example that loads all of the values from the file :file:`base_config.py`:: |
|
225 | example that loads all of the values from the file :file:`base_config.py`:: | |
226 |
|
226 | |||
227 | # base_config.py |
|
227 | # base_config.py | |
228 | c = get_config() |
|
228 | c = get_config() | |
229 | c.MyClass.name = 'coolname' |
|
229 | c.MyClass.name = 'coolname' | |
230 | c.MyClass.ranking = 100 |
|
230 | c.MyClass.ranking = 100 | |
231 |
|
231 | |||
232 | into the configuration file :file:`main_config.py`:: |
|
232 | into the configuration file :file:`main_config.py`:: | |
233 |
|
233 | |||
234 | # main_config.py |
|
234 | # main_config.py | |
235 | c = get_config() |
|
235 | c = get_config() | |
236 |
|
236 | |||
237 | # Load everything from base_config.py |
|
237 | # Load everything from base_config.py | |
238 | load_subconfig('base_config.py') |
|
238 | load_subconfig('base_config.py') | |
239 |
|
239 | |||
240 | # Now override one of the values |
|
240 | # Now override one of the values | |
241 | c.MyClass.name = 'bettername' |
|
241 | c.MyClass.name = 'bettername' | |
242 |
|
242 | |||
243 | In a situation like this the :func:`load_subconfig` makes sure that the |
|
243 | In a situation like this the :func:`load_subconfig` makes sure that the | |
244 | search path for sub-configuration files is inherited from that of the parent. |
|
244 | search path for sub-configuration files is inherited from that of the parent. | |
245 | Thus, you can typically put the two in the same directory and everything will |
|
245 | Thus, you can typically put the two in the same directory and everything will | |
246 | just work. |
|
246 | just work. | |
247 |
|
247 | |||
248 | You can also load configuration files by profile, for instance: |
|
248 | You can also load configuration files by profile, for instance: | |
249 |
|
249 | |||
250 | .. sourcecode:: python |
|
250 | .. sourcecode:: python | |
251 |
|
251 | |||
252 | load_subconfig('ipython_config.py', profile='default') |
|
252 | load_subconfig('ipython_config.py', profile='default') | |
253 |
|
253 | |||
254 | to inherit your default configuration as a starting point. |
|
254 | to inherit your default configuration as a starting point. | |
255 |
|
255 | |||
256 |
|
256 | |||
257 | Class based configuration inheritance |
|
257 | Class based configuration inheritance | |
258 | ===================================== |
|
258 | ===================================== | |
259 |
|
259 | |||
260 | There is another aspect of configuration where inheritance comes into play. |
|
260 | There is another aspect of configuration where inheritance comes into play. | |
261 | Sometimes, your classes will have an inheritance hierarchy that you want |
|
261 | Sometimes, your classes will have an inheritance hierarchy that you want | |
262 | to be reflected in the configuration system. Here is a simple example:: |
|
262 | to be reflected in the configuration system. Here is a simple example:: | |
263 |
|
263 | |||
264 | from IPython.config.configurable import Configurable |
|
264 | from IPython.config.configurable import Configurable | |
265 | from IPython.utils.traitlets import Int, Float, Unicode, Bool |
|
265 | from IPython.utils.traitlets import Int, Float, Unicode, Bool | |
266 |
|
266 | |||
267 | class Foo(Configurable): |
|
267 | class Foo(Configurable): | |
268 | name = Unicode(u'fooname', config=True) |
|
268 | name = Unicode(u'fooname', config=True) | |
269 | value = Float(100.0, config=True) |
|
269 | value = Float(100.0, config=True) | |
270 |
|
270 | |||
271 | class Bar(Foo): |
|
271 | class Bar(Foo): | |
272 | name = Unicode(u'barname', config=True) |
|
272 | name = Unicode(u'barname', config=True) | |
273 | othervalue = Int(0, config=True) |
|
273 | othervalue = Int(0, config=True) | |
274 |
|
274 | |||
275 | Now, we can create a configuration file to configure instances of :class:`Foo` |
|
275 | Now, we can create a configuration file to configure instances of :class:`Foo` | |
276 | and :class:`Bar`:: |
|
276 | and :class:`Bar`:: | |
277 |
|
277 | |||
278 | # config file |
|
278 | # config file | |
279 | c = get_config() |
|
279 | c = get_config() | |
280 |
|
280 | |||
281 | c.Foo.name = u'bestname' |
|
281 | c.Foo.name = u'bestname' | |
282 | c.Bar.othervalue = 10 |
|
282 | c.Bar.othervalue = 10 | |
283 |
|
283 | |||
284 | This class hierarchy and configuration file accomplishes the following: |
|
284 | This class hierarchy and configuration file accomplishes the following: | |
285 |
|
285 | |||
286 | * The default value for :attr:`Foo.name` and :attr:`Bar.name` will be |
|
286 | * The default value for :attr:`Foo.name` and :attr:`Bar.name` will be | |
287 | 'bestname'. Because :class:`Bar` is a :class:`Foo` subclass it also |
|
287 | 'bestname'. Because :class:`Bar` is a :class:`Foo` subclass it also | |
288 | picks up the configuration information for :class:`Foo`. |
|
288 | picks up the configuration information for :class:`Foo`. | |
289 | * The default value for :attr:`Foo.value` and :attr:`Bar.value` will be |
|
289 | * The default value for :attr:`Foo.value` and :attr:`Bar.value` will be | |
290 | ``100.0``, which is the value specified as the class default. |
|
290 | ``100.0``, which is the value specified as the class default. | |
291 | * The default value for :attr:`Bar.othervalue` will be 10 as set in the |
|
291 | * The default value for :attr:`Bar.othervalue` will be 10 as set in the | |
292 | configuration file. Because :class:`Foo` is the parent of :class:`Bar` |
|
292 | configuration file. Because :class:`Foo` is the parent of :class:`Bar` | |
293 | it doesn't know anything about the :attr:`othervalue` attribute. |
|
293 | it doesn't know anything about the :attr:`othervalue` attribute. | |
294 |
|
294 | |||
295 |
|
295 | |||
296 | .. _ipython_dir: |
|
296 | .. _ipython_dir: | |
297 |
|
297 | |||
298 | Configuration file location |
|
298 | Configuration file location | |
299 | =========================== |
|
299 | =========================== | |
300 |
|
300 | |||
301 | So where should you put your configuration files? IPython uses "profiles" for |
|
301 | So where should you put your configuration files? IPython uses "profiles" for | |
302 | configuration, and by default, all profiles will be stored in the so called |
|
302 | configuration, and by default, all profiles will be stored in the so called | |
303 | "IPython directory". The location of this directory is determined by the |
|
303 | "IPython directory". The location of this directory is determined by the | |
304 | following algorithm: |
|
304 | following algorithm: | |
305 |
|
305 | |||
306 | * If the ``ipython-dir`` command line flag is given, its value is used. |
|
306 | * If the ``ipython-dir`` command line flag is given, its value is used. | |
307 |
|
307 | |||
308 | * If not, the value returned by :func:`IPython.utils.path.get_ipython_dir` |
|
308 | * If not, the value returned by :func:`IPython.utils.path.get_ipython_dir` | |
309 | is used. This function will first look at the :envvar:`IPYTHONDIR` |
|
309 | is used. This function will first look at the :envvar:`IPYTHONDIR` | |
310 | environment variable and then default to :file:`~/.ipython`. |
|
310 | environment variable and then default to :file:`~/.ipython`. | |
311 | Historical support for the :envvar:`IPYTHON_DIR` environment variable will |
|
311 | Historical support for the :envvar:`IPYTHON_DIR` environment variable will | |
312 | be removed in a future release. |
|
312 | be removed in a future release. | |
313 |
|
313 | |||
314 | For most users, the configuration directory will be :file:`~/.ipython`. |
|
314 | For most users, the configuration directory will be :file:`~/.ipython`. | |
315 |
|
315 | |||
316 | Previous versions of IPython on Linux would use the XDG config directory, |
|
316 | Previous versions of IPython on Linux would use the XDG config directory, | |
317 | creating :file:`~/.config/ipython` by default. We have decided to go |
|
317 | creating :file:`~/.config/ipython` by default. We have decided to go | |
318 | back to :file:`~/.ipython` for consistency among systems. IPython will |
|
318 | back to :file:`~/.ipython` for consistency among systems. IPython will | |
319 | issue a warning if it finds the XDG location, and will move it to the new |
|
319 | issue a warning if it finds the XDG location, and will move it to the new | |
320 | location if there isn't already a directory there. |
|
320 | location if there isn't already a directory there. | |
321 |
|
321 | |||
322 | Once the location of the IPython directory has been determined, you need to know |
|
322 | Once the location of the IPython directory has been determined, you need to know | |
323 | which profile you are using. For users with a single configuration, this will |
|
323 | which profile you are using. For users with a single configuration, this will | |
324 | simply be 'default', and will be located in |
|
324 | simply be 'default', and will be located in | |
325 | :file:`<IPYTHONDIR>/profile_default`. |
|
325 | :file:`<IPYTHONDIR>/profile_default`. | |
326 |
|
326 | |||
327 | The next thing you need to know is what to call your configuration file. The |
|
327 | The next thing you need to know is what to call your configuration file. The | |
328 | basic idea is that each application has its own default configuration filename. |
|
328 | basic idea is that each application has its own default configuration filename. | |
329 | The default named used by the :command:`ipython` command line program is |
|
329 | The default named used by the :command:`ipython` command line program is | |
330 | :file:`ipython_config.py`, and *all* IPython applications will use this file. |
|
330 | :file:`ipython_config.py`, and *all* IPython applications will use this file. | |
331 | Other applications, such as the parallel :command:`ipcluster` scripts or the |
|
331 | Other applications, such as the parallel :command:`ipcluster` scripts or the | |
332 | QtConsole will load their own config files *after* :file:`ipython_config.py`. To |
|
332 | QtConsole will load their own config files *after* :file:`ipython_config.py`. To | |
333 | load a particular configuration file instead of the default, the name can be |
|
333 | load a particular configuration file instead of the default, the name can be | |
334 | overridden by the ``config_file`` command line flag. |
|
334 | overridden by the ``config_file`` command line flag. | |
335 |
|
335 | |||
336 | To generate the default configuration files, do:: |
|
336 | To generate the default configuration files, do:: | |
337 |
|
337 | |||
338 | $ ipython profile create |
|
338 | $ ipython profile create | |
339 |
|
339 | |||
340 | and you will have a default :file:`ipython_config.py` in your IPython directory |
|
340 | and you will have a default :file:`ipython_config.py` in your IPython directory | |
341 | under :file:`profile_default`. If you want the default config files for the |
|
341 | under :file:`profile_default`. If you want the default config files for the | |
342 | :mod:`IPython.parallel` applications, add ``--parallel`` to the end of the |
|
342 | :mod:`IPython.parallel` applications, add ``--parallel`` to the end of the | |
343 | command-line args. |
|
343 | command-line args. | |
344 |
|
344 | |||
345 |
|
345 | |||
346 | Locating these files |
|
346 | Locating these files | |
347 | -------------------- |
|
347 | -------------------- | |
348 |
|
348 | |||
349 | From the command-line, you can quickly locate the IPYTHONDIR or a specific |
|
349 | From the command-line, you can quickly locate the IPYTHONDIR or a specific | |
350 | profile with: |
|
350 | profile with: | |
351 |
|
351 | |||
352 | .. sourcecode:: bash |
|
352 | .. sourcecode:: bash | |
353 |
|
353 | |||
354 | $ ipython locate |
|
354 | $ ipython locate | |
355 | /home/you/.ipython |
|
355 | /home/you/.ipython | |
356 |
|
356 | |||
357 | $ ipython locate profile foo |
|
357 | $ ipython locate profile foo | |
358 | /home/you/.ipython/profile_foo |
|
358 | /home/you/.ipython/profile_foo | |
359 |
|
359 | |||
360 | These map to the utility functions: :func:`IPython.utils.path.get_ipython_dir` |
|
360 | These map to the utility functions: :func:`IPython.utils.path.get_ipython_dir` | |
361 | and :func:`IPython.utils.path.locate_profile` respectively. |
|
361 | and :func:`IPython.utils.path.locate_profile` respectively. | |
362 |
|
362 | |||
363 |
|
363 | |||
364 | .. _profiles_dev: |
|
364 | .. _profiles_dev: | |
365 |
|
365 | |||
366 | Profiles |
|
366 | Profiles | |
367 | ======== |
|
367 | ======== | |
368 |
|
368 | |||
369 | A profile is a directory containing configuration and runtime files, such as |
|
369 | A profile is a directory containing configuration and runtime files, such as | |
370 | logs, connection info for the parallel apps, and your IPython command history. |
|
370 | logs, connection info for the parallel apps, and your IPython command history. | |
371 |
|
371 | |||
372 | The idea is that users often want to maintain a set of configuration files for |
|
372 | The idea is that users often want to maintain a set of configuration files for | |
373 | different purposes: one for doing numerical computing with NumPy and SciPy and |
|
373 | different purposes: one for doing numerical computing with NumPy and SciPy and | |
374 | another for doing symbolic computing with SymPy. Profiles make it easy to keep a |
|
374 | another for doing symbolic computing with SymPy. Profiles make it easy to keep a | |
375 | separate configuration files, logs, and histories for each of these purposes. |
|
375 | separate configuration files, logs, and histories for each of these purposes. | |
376 |
|
376 | |||
377 | Let's start by showing how a profile is used: |
|
377 | Let's start by showing how a profile is used: | |
378 |
|
378 | |||
379 | .. code-block:: bash |
|
379 | .. code-block:: bash | |
380 |
|
380 | |||
381 | $ ipython --profile=sympy |
|
381 | $ ipython --profile=sympy | |
382 |
|
382 | |||
383 | This tells the :command:`ipython` command line program to get its configuration |
|
383 | This tells the :command:`ipython` command line program to get its configuration | |
384 | from the "sympy" profile. The file names for various profiles do not change. The |
|
384 | from the "sympy" profile. The file names for various profiles do not change. The | |
385 | only difference is that profiles are named in a special way. In the case above, |
|
385 | only difference is that profiles are named in a special way. In the case above, | |
386 | the "sympy" profile means looking for :file:`ipython_config.py` in :file:`<IPYTHONDIR>/profile_sympy`. |
|
386 | the "sympy" profile means looking for :file:`ipython_config.py` in :file:`<IPYTHONDIR>/profile_sympy`. | |
387 |
|
387 | |||
388 | The general pattern is this: simply create a new profile with: |
|
388 | The general pattern is this: simply create a new profile with: | |
389 |
|
389 | |||
390 | .. code-block:: bash |
|
390 | .. code-block:: bash | |
391 |
|
391 | |||
392 | $ ipython profile create <name> |
|
392 | $ ipython profile create <name> | |
393 |
|
393 | |||
394 | which adds a directory called ``profile_<name>`` to your IPython directory. Then |
|
394 | which adds a directory called ``profile_<name>`` to your IPython directory. Then | |
395 | you can load this profile by adding ``--profile=<name>`` to your command line |
|
395 | you can load this profile by adding ``--profile=<name>`` to your command line | |
396 | options. Profiles are supported by all IPython applications. |
|
396 | options. Profiles are supported by all IPython applications. | |
397 |
|
397 | |||
398 | IPython ships with some sample profiles in :file:`IPython/config/profile`. If |
|
398 | IPython ships with some sample profiles in :file:`IPython/config/profile`. If | |
399 | you create profiles with the name of one of our shipped profiles, these config |
|
399 | you create profiles with the name of one of our shipped profiles, these config | |
400 | files will be copied over instead of starting with the automatically generated |
|
400 | files will be copied over instead of starting with the automatically generated | |
401 | config files. |
|
401 | config files. | |
402 |
|
402 | |||
403 | Security Files |
|
403 | Security Files | |
404 | -------------- |
|
404 | -------------- | |
405 |
|
405 | |||
406 | If you are using the notebook, qtconsole, or parallel code, IPython stores |
|
406 | If you are using the notebook, qtconsole, or parallel code, IPython stores | |
407 | connection information in small JSON files in the active profile's security |
|
407 | connection information in small JSON files in the active profile's security | |
408 | directory. This directory is made private, so only you can see the files inside. If |
|
408 | directory. This directory is made private, so only you can see the files inside. If | |
409 | you need to move connection files around to other computers, this is where they will |
|
409 | you need to move connection files around to other computers, this is where they will | |
410 | be. If you want your code to be able to open security files by name, we have a |
|
410 | be. If you want your code to be able to open security files by name, we have a | |
411 | convenience function :func:`IPython.utils.path.get_security_file`, which will return |
|
411 | convenience function :func:`IPython.utils.path.get_security_file`, which will return | |
412 | the absolute path to a security file from its filename and [optionally] profile |
|
412 | the absolute path to a security file from its filename and [optionally] profile | |
413 | name. |
|
413 | name. | |
414 |
|
414 | |||
415 | .. _startup_files: |
|
415 | .. _startup_files: | |
416 |
|
416 | |||
417 | Startup Files |
|
417 | Startup Files | |
418 | ------------- |
|
418 | ------------- | |
419 |
|
419 | |||
420 | If you want some code to be run at the beginning of every IPython session with |
|
420 | If you want some code to be run at the beginning of every IPython session with | |
421 | a particular profile, the easiest way is to add Python (``.py``) or |
|
421 | a particular profile, the easiest way is to add Python (``.py``) or | |
422 | IPython (``.ipy``) scripts to your :file:`<profile>/startup` directory. Files |
|
422 | IPython (``.ipy``) scripts to your :file:`<profile>/startup` directory. Files | |
423 | in this directory will always be executed as soon as the IPython shell is |
|
423 | in this directory will always be executed as soon as the IPython shell is | |
424 | constructed, and before any other code or scripts you have specified. If you |
|
424 | constructed, and before any other code or scripts you have specified. If you | |
425 | have multiple files in the startup directory, they will be run in |
|
425 | have multiple files in the startup directory, they will be run in | |
426 | lexicographical order, so you can control the ordering by adding a '00-' |
|
426 | lexicographical order, so you can control the ordering by adding a '00-' | |
427 | prefix. |
|
427 | prefix. | |
428 |
|
428 | |||
429 |
|
429 | |||
430 | .. _commandline: |
|
430 | .. _commandline: | |
431 |
|
431 | |||
432 | Command-line arguments |
|
432 | Command-line arguments | |
433 | ====================== |
|
433 | ====================== | |
434 |
|
434 | |||
435 | IPython exposes *all* configurable options on the command-line. The command-line |
|
435 | IPython exposes *all* configurable options on the command-line. The command-line | |
436 | arguments are generated from the Configurable traits of the classes associated |
|
436 | arguments are generated from the Configurable traits of the classes associated | |
437 | with a given Application. Configuring IPython from the command-line may look |
|
437 | with a given Application. Configuring IPython from the command-line may look | |
438 | very similar to an IPython config file |
|
438 | very similar to an IPython config file | |
439 |
|
439 | |||
440 | IPython applications use a parser called |
|
440 | IPython applications use a parser called | |
441 | :class:`~IPython.config.loader.KeyValueLoader` to load values into a Config |
|
441 | :class:`~IPython.config.loader.KeyValueLoader` to load values into a Config | |
442 | object. Values are assigned in much the same way as in a config file: |
|
442 | object. Values are assigned in much the same way as in a config file: | |
443 |
|
443 | |||
444 | .. code-block:: bash |
|
444 | .. code-block:: bash | |
445 |
|
445 | |||
446 | $ ipython --InteractiveShell.use_readline=False --BaseIPythonApplication.profile='myprofile' |
|
446 | $ ipython --InteractiveShell.use_readline=False --BaseIPythonApplication.profile='myprofile' | |
447 |
|
447 | |||
448 | Is the same as adding: |
|
448 | Is the same as adding: | |
449 |
|
449 | |||
450 | .. sourcecode:: python |
|
450 | .. sourcecode:: python | |
451 |
|
451 | |||
452 | c.InteractiveShell.use_readline=False |
|
452 | c.InteractiveShell.use_readline=False | |
453 | c.BaseIPythonApplication.profile='myprofile' |
|
453 | c.BaseIPythonApplication.profile='myprofile' | |
454 |
|
454 | |||
455 | to your config file. Key/Value arguments *always* take a value, separated by '=' |
|
455 | to your config file. Key/Value arguments *always* take a value, separated by '=' | |
456 | and no spaces. |
|
456 | and no spaces. | |
457 |
|
457 | |||
458 | Common Arguments |
|
458 | Common Arguments | |
459 | ---------------- |
|
459 | ---------------- | |
460 |
|
460 | |||
461 | Since the strictness and verbosity of the KVLoader above are not ideal for everyday |
|
461 | Since the strictness and verbosity of the KVLoader above are not ideal for everyday | |
462 | use, common arguments can be specified as flags_ or aliases_. |
|
462 | use, common arguments can be specified as flags_ or aliases_. | |
463 |
|
463 | |||
464 | Flags and Aliases are handled by :mod:`argparse` instead, allowing for more flexible |
|
464 | Flags and Aliases are handled by :mod:`argparse` instead, allowing for more flexible | |
465 | parsing. In general, flags and aliases are prefixed by ``--``, except for those |
|
465 | parsing. In general, flags and aliases are prefixed by ``--``, except for those | |
466 | that are single characters, in which case they can be specified with a single ``-``, e.g.: |
|
466 | that are single characters, in which case they can be specified with a single ``-``, e.g.: | |
467 |
|
467 | |||
468 | .. code-block:: bash |
|
468 | .. code-block:: bash | |
469 |
|
469 | |||
470 | $ ipython -i -c "import numpy; x=numpy.linspace(0,1)" --profile testing --colors=lightbg |
|
470 | $ ipython -i -c "import numpy; x=numpy.linspace(0,1)" --profile testing --colors=lightbg | |
471 |
|
471 | |||
472 | Aliases |
|
472 | Aliases | |
473 | ******* |
|
473 | ******* | |
474 |
|
474 | |||
475 | For convenience, applications have a mapping of commonly used traits, so you don't have |
|
475 | For convenience, applications have a mapping of commonly used traits, so you don't have | |
476 | to specify the whole class name: |
|
476 | to specify the whole class name: | |
477 |
|
477 | |||
478 | .. code-block:: bash |
|
478 | .. code-block:: bash | |
479 |
|
479 | |||
480 | $ ipython --profile myprofile |
|
480 | $ ipython --profile myprofile | |
481 | # and |
|
481 | # and | |
482 | $ ipython --profile='myprofile' |
|
482 | $ ipython --profile='myprofile' | |
483 | # are equivalent to |
|
483 | # are equivalent to | |
484 | $ ipython --BaseIPythonApplication.profile='myprofile' |
|
484 | $ ipython --BaseIPythonApplication.profile='myprofile' | |
485 |
|
485 | |||
486 | Flags |
|
486 | Flags | |
487 | ***** |
|
487 | ***** | |
488 |
|
488 | |||
489 | Applications can also be passed **flags**. Flags are options that take no |
|
489 | Applications can also be passed **flags**. Flags are options that take no | |
490 | arguments. They are simply wrappers for |
|
490 | arguments. They are simply wrappers for | |
491 | setting one or more configurables with predefined values, often True/False. |
|
491 | setting one or more configurables with predefined values, often True/False. | |
492 |
|
492 | |||
493 | For instance: |
|
493 | For instance: | |
494 |
|
494 | |||
495 | .. code-block:: bash |
|
495 | .. code-block:: bash | |
496 |
|
496 | |||
497 | $ ipcontroller --debug |
|
497 | $ ipcontroller --debug | |
498 | # is equivalent to |
|
498 | # is equivalent to | |
499 | $ ipcontroller --Application.log_level=DEBUG |
|
499 | $ ipcontroller --Application.log_level=DEBUG | |
500 | # and |
|
500 | # and | |
501 |
$ ipython --matplo |
|
501 | $ ipython --matplotlib | |
502 | # is equivalent to |
|
502 | # is equivalent to | |
503 | $ ipython --matplotlib auto |
|
503 | $ ipython --matplotlib auto | |
504 | # or |
|
504 | # or | |
505 | $ ipython --no-banner |
|
505 | $ ipython --no-banner | |
506 | # is equivalent to |
|
506 | # is equivalent to | |
507 | $ ipython --TerminalIPythonApp.display_banner=False |
|
507 | $ ipython --TerminalIPythonApp.display_banner=False | |
508 |
|
508 | |||
509 | Subcommands |
|
509 | Subcommands | |
510 | ----------- |
|
510 | ----------- | |
511 |
|
511 | |||
512 |
|
512 | |||
513 | Some IPython applications have **subcommands**. Subcommands are modeled after |
|
513 | Some IPython applications have **subcommands**. Subcommands are modeled after | |
514 | :command:`git`, and are called with the form :command:`command subcommand |
|
514 | :command:`git`, and are called with the form :command:`command subcommand | |
515 | [...args]`. Currently, the QtConsole is a subcommand of terminal IPython: |
|
515 | [...args]`. Currently, the QtConsole is a subcommand of terminal IPython: | |
516 |
|
516 | |||
517 | .. code-block:: bash |
|
517 | .. code-block:: bash | |
518 |
|
518 | |||
519 | $ ipython qtconsole --profile myprofile |
|
519 | $ ipython qtconsole --profile myprofile | |
520 |
|
520 | |||
521 | and :command:`ipcluster` is simply a wrapper for its various subcommands (start, |
|
521 | and :command:`ipcluster` is simply a wrapper for its various subcommands (start, | |
522 | stop, engines). |
|
522 | stop, engines). | |
523 |
|
523 | |||
524 | .. code-block:: bash |
|
524 | .. code-block:: bash | |
525 |
|
525 | |||
526 | $ ipcluster start --profile=myprofile -n 4 |
|
526 | $ ipcluster start --profile=myprofile -n 4 | |
527 |
|
527 | |||
528 |
|
528 | |||
529 | To see a list of the available aliases, flags, and subcommands for an IPython application, simply pass ``-h`` or ``--help``. And to see the full list of configurable options (*very* long), pass ``--help-all``. |
|
529 | To see a list of the available aliases, flags, and subcommands for an IPython application, simply pass ``-h`` or ``--help``. And to see the full list of configurable options (*very* long), pass ``--help-all``. | |
530 |
|
530 | |||
531 |
|
531 | |||
532 | Design requirements |
|
532 | Design requirements | |
533 | =================== |
|
533 | =================== | |
534 |
|
534 | |||
535 | Here are the main requirements we wanted our configuration system to have: |
|
535 | Here are the main requirements we wanted our configuration system to have: | |
536 |
|
536 | |||
537 | * Support for hierarchical configuration information. |
|
537 | * Support for hierarchical configuration information. | |
538 |
|
538 | |||
539 | * Full integration with command line option parsers. Often, you want to read |
|
539 | * Full integration with command line option parsers. Often, you want to read | |
540 | a configuration file, but then override some of the values with command line |
|
540 | a configuration file, but then override some of the values with command line | |
541 | options. Our configuration system automates this process and allows each |
|
541 | options. Our configuration system automates this process and allows each | |
542 | command line option to be linked to a particular attribute in the |
|
542 | command line option to be linked to a particular attribute in the | |
543 | configuration hierarchy that it will override. |
|
543 | configuration hierarchy that it will override. | |
544 |
|
544 | |||
545 | * Configuration files that are themselves valid Python code. This accomplishes |
|
545 | * Configuration files that are themselves valid Python code. This accomplishes | |
546 | many things. First, it becomes possible to put logic in your configuration |
|
546 | many things. First, it becomes possible to put logic in your configuration | |
547 | files that sets attributes based on your operating system, network setup, |
|
547 | files that sets attributes based on your operating system, network setup, | |
548 | Python version, etc. Second, Python has a super simple syntax for accessing |
|
548 | Python version, etc. Second, Python has a super simple syntax for accessing | |
549 | hierarchical data structures, namely regular attribute access |
|
549 | hierarchical data structures, namely regular attribute access | |
550 | (``Foo.Bar.Bam.name``). Third, using Python makes it easy for users to |
|
550 | (``Foo.Bar.Bam.name``). Third, using Python makes it easy for users to | |
551 | import configuration attributes from one configuration file to another. |
|
551 | import configuration attributes from one configuration file to another. | |
552 | Fourth, even though Python is dynamically typed, it does have types that can |
|
552 | Fourth, even though Python is dynamically typed, it does have types that can | |
553 | be checked at runtime. Thus, a ``1`` in a config file is the integer '1', |
|
553 | be checked at runtime. Thus, a ``1`` in a config file is the integer '1', | |
554 | while a ``'1'`` is a string. |
|
554 | while a ``'1'`` is a string. | |
555 |
|
555 | |||
556 | * A fully automated method for getting the configuration information to the |
|
556 | * A fully automated method for getting the configuration information to the | |
557 | classes that need it at runtime. Writing code that walks a configuration |
|
557 | classes that need it at runtime. Writing code that walks a configuration | |
558 | hierarchy to extract a particular attribute is painful. When you have |
|
558 | hierarchy to extract a particular attribute is painful. When you have | |
559 | complex configuration information with hundreds of attributes, this makes |
|
559 | complex configuration information with hundreds of attributes, this makes | |
560 | you want to cry. |
|
560 | you want to cry. | |
561 |
|
561 | |||
562 | * Type checking and validation that doesn't require the entire configuration |
|
562 | * Type checking and validation that doesn't require the entire configuration | |
563 | hierarchy to be specified statically before runtime. Python is a very |
|
563 | hierarchy to be specified statically before runtime. Python is a very | |
564 | dynamic language and you don't always know everything that needs to be |
|
564 | dynamic language and you don't always know everything that needs to be | |
565 | configured when a program starts. |
|
565 | configured when a program starts. | |
566 |
|
566 |
@@ -1,1156 +1,1156 | |||||
1 | .. _messaging: |
|
1 | .. _messaging: | |
2 |
|
2 | |||
3 | ====================== |
|
3 | ====================== | |
4 | Messaging in IPython |
|
4 | Messaging in IPython | |
5 | ====================== |
|
5 | ====================== | |
6 |
|
6 | |||
7 |
|
7 | |||
8 | Introduction |
|
8 | Introduction | |
9 | ============ |
|
9 | ============ | |
10 |
|
10 | |||
11 | This document explains the basic communications design and messaging |
|
11 | This document explains the basic communications design and messaging | |
12 | specification for how the various IPython objects interact over a network |
|
12 | specification for how the various IPython objects interact over a network | |
13 | transport. The current implementation uses the ZeroMQ_ library for messaging |
|
13 | transport. The current implementation uses the ZeroMQ_ library for messaging | |
14 | within and between hosts. |
|
14 | within and between hosts. | |
15 |
|
15 | |||
16 | .. Note:: |
|
16 | .. Note:: | |
17 |
|
17 | |||
18 | This document should be considered the authoritative description of the |
|
18 | This document should be considered the authoritative description of the | |
19 | IPython messaging protocol, and all developers are strongly encouraged to |
|
19 | IPython messaging protocol, and all developers are strongly encouraged to | |
20 | keep it updated as the implementation evolves, so that we have a single |
|
20 | keep it updated as the implementation evolves, so that we have a single | |
21 | common reference for all protocol details. |
|
21 | common reference for all protocol details. | |
22 |
|
22 | |||
23 | The basic design is explained in the following diagram: |
|
23 | The basic design is explained in the following diagram: | |
24 |
|
24 | |||
25 | .. image:: figs/frontend-kernel.png |
|
25 | .. image:: figs/frontend-kernel.png | |
26 | :width: 450px |
|
26 | :width: 450px | |
27 | :alt: IPython kernel/frontend messaging architecture. |
|
27 | :alt: IPython kernel/frontend messaging architecture. | |
28 | :align: center |
|
28 | :align: center | |
29 | :target: ../_images/frontend-kernel.png |
|
29 | :target: ../_images/frontend-kernel.png | |
30 |
|
30 | |||
31 | A single kernel can be simultaneously connected to one or more frontends. The |
|
31 | A single kernel can be simultaneously connected to one or more frontends. The | |
32 | kernel has three sockets that serve the following functions: |
|
32 | kernel has three sockets that serve the following functions: | |
33 |
|
33 | |||
34 | 1. stdin: this ROUTER socket is connected to all frontends, and it allows |
|
34 | 1. stdin: this ROUTER socket is connected to all frontends, and it allows | |
35 | the kernel to request input from the active frontend when :func:`raw_input` is called. |
|
35 | the kernel to request input from the active frontend when :func:`raw_input` is called. | |
36 | The frontend that executed the code has a DEALER socket that acts as a 'virtual keyboard' |
|
36 | The frontend that executed the code has a DEALER socket that acts as a 'virtual keyboard' | |
37 | for the kernel while this communication is happening (illustrated in the |
|
37 | for the kernel while this communication is happening (illustrated in the | |
38 | figure by the black outline around the central keyboard). In practice, |
|
38 | figure by the black outline around the central keyboard). In practice, | |
39 | frontends may display such kernel requests using a special input widget or |
|
39 | frontends may display such kernel requests using a special input widget or | |
40 | otherwise indicating that the user is to type input for the kernel instead |
|
40 | otherwise indicating that the user is to type input for the kernel instead | |
41 | of normal commands in the frontend. |
|
41 | of normal commands in the frontend. | |
42 |
|
42 | |||
43 | 2. Shell: this single ROUTER socket allows multiple incoming connections from |
|
43 | 2. Shell: this single ROUTER socket allows multiple incoming connections from | |
44 | frontends, and this is the socket where requests for code execution, object |
|
44 | frontends, and this is the socket where requests for code execution, object | |
45 | information, prompts, etc. are made to the kernel by any frontend. The |
|
45 | information, prompts, etc. are made to the kernel by any frontend. The | |
46 | communication on this socket is a sequence of request/reply actions from |
|
46 | communication on this socket is a sequence of request/reply actions from | |
47 | each frontend and the kernel. |
|
47 | each frontend and the kernel. | |
48 |
|
48 | |||
49 | 3. IOPub: this socket is the 'broadcast channel' where the kernel publishes all |
|
49 | 3. IOPub: this socket is the 'broadcast channel' where the kernel publishes all | |
50 | side effects (stdout, stderr, etc.) as well as the requests coming from any |
|
50 | side effects (stdout, stderr, etc.) as well as the requests coming from any | |
51 | client over the shell socket and its own requests on the stdin socket. There |
|
51 | client over the shell socket and its own requests on the stdin socket. There | |
52 | are a number of actions in Python which generate side effects: :func:`print` |
|
52 | are a number of actions in Python which generate side effects: :func:`print` | |
53 | writes to ``sys.stdout``, errors generate tracebacks, etc. Additionally, in |
|
53 | writes to ``sys.stdout``, errors generate tracebacks, etc. Additionally, in | |
54 | a multi-client scenario, we want all frontends to be able to know what each |
|
54 | a multi-client scenario, we want all frontends to be able to know what each | |
55 | other has sent to the kernel (this can be useful in collaborative scenarios, |
|
55 | other has sent to the kernel (this can be useful in collaborative scenarios, | |
56 | for example). This socket allows both side effects and the information |
|
56 | for example). This socket allows both side effects and the information | |
57 | about communications taking place with one client over the shell channel |
|
57 | about communications taking place with one client over the shell channel | |
58 | to be made available to all clients in a uniform manner. |
|
58 | to be made available to all clients in a uniform manner. | |
59 |
|
59 | |||
60 | All messages are tagged with enough information (details below) for clients |
|
60 | All messages are tagged with enough information (details below) for clients | |
61 | to know which messages come from their own interaction with the kernel and |
|
61 | to know which messages come from their own interaction with the kernel and | |
62 | which ones are from other clients, so they can display each type |
|
62 | which ones are from other clients, so they can display each type | |
63 | appropriately. |
|
63 | appropriately. | |
64 |
|
64 | |||
65 | The actual format of the messages allowed on each of these channels is |
|
65 | The actual format of the messages allowed on each of these channels is | |
66 | specified below. Messages are dicts of dicts with string keys and values that |
|
66 | specified below. Messages are dicts of dicts with string keys and values that | |
67 | are reasonably representable in JSON. Our current implementation uses JSON |
|
67 | are reasonably representable in JSON. Our current implementation uses JSON | |
68 | explicitly as its message format, but this shouldn't be considered a permanent |
|
68 | explicitly as its message format, but this shouldn't be considered a permanent | |
69 | feature. As we've discovered that JSON has non-trivial performance issues due |
|
69 | feature. As we've discovered that JSON has non-trivial performance issues due | |
70 | to excessive copying, we may in the future move to a pure pickle-based raw |
|
70 | to excessive copying, we may in the future move to a pure pickle-based raw | |
71 | message format. However, it should be possible to easily convert from the raw |
|
71 | message format. However, it should be possible to easily convert from the raw | |
72 | objects to JSON, since we may have non-python clients (e.g. a web frontend). |
|
72 | objects to JSON, since we may have non-python clients (e.g. a web frontend). | |
73 | As long as it's easy to make a JSON version of the objects that is a faithful |
|
73 | As long as it's easy to make a JSON version of the objects that is a faithful | |
74 | representation of all the data, we can communicate with such clients. |
|
74 | representation of all the data, we can communicate with such clients. | |
75 |
|
75 | |||
76 | .. Note:: |
|
76 | .. Note:: | |
77 |
|
77 | |||
78 | Not all of these have yet been fully fleshed out, but the key ones are, see |
|
78 | Not all of these have yet been fully fleshed out, but the key ones are, see | |
79 | kernel and frontend files for actual implementation details. |
|
79 | kernel and frontend files for actual implementation details. | |
80 |
|
80 | |||
81 | General Message Format |
|
81 | General Message Format | |
82 | ====================== |
|
82 | ====================== | |
83 |
|
83 | |||
84 | A message is defined by the following four-dictionary structure:: |
|
84 | A message is defined by the following four-dictionary structure:: | |
85 |
|
85 | |||
86 | { |
|
86 | { | |
87 | # The message header contains a pair of unique identifiers for the |
|
87 | # The message header contains a pair of unique identifiers for the | |
88 | # originating session and the actual message id, in addition to the |
|
88 | # originating session and the actual message id, in addition to the | |
89 | # username for the process that generated the message. This is useful in |
|
89 | # username for the process that generated the message. This is useful in | |
90 | # collaborative settings where multiple users may be interacting with the |
|
90 | # collaborative settings where multiple users may be interacting with the | |
91 | # same kernel simultaneously, so that frontends can label the various |
|
91 | # same kernel simultaneously, so that frontends can label the various | |
92 | # messages in a meaningful way. |
|
92 | # messages in a meaningful way. | |
93 | 'header' : { |
|
93 | 'header' : { | |
94 | 'msg_id' : uuid, |
|
94 | 'msg_id' : uuid, | |
95 | 'username' : str, |
|
95 | 'username' : str, | |
96 | 'session' : uuid, |
|
96 | 'session' : uuid, | |
97 | # All recognized message type strings are listed below. |
|
97 | # All recognized message type strings are listed below. | |
98 | 'msg_type' : str, |
|
98 | 'msg_type' : str, | |
99 | }, |
|
99 | }, | |
100 |
|
100 | |||
101 | # In a chain of messages, the header from the parent is copied so that |
|
101 | # In a chain of messages, the header from the parent is copied so that | |
102 | # clients can track where messages come from. |
|
102 | # clients can track where messages come from. | |
103 | 'parent_header' : dict, |
|
103 | 'parent_header' : dict, | |
104 |
|
104 | |||
105 | # Any metadata associated with the message. |
|
105 | # Any metadata associated with the message. | |
106 | 'metadata' : dict, |
|
106 | 'metadata' : dict, | |
107 |
|
107 | |||
108 | # The actual content of the message must be a dict, whose structure |
|
108 | # The actual content of the message must be a dict, whose structure | |
109 | # depends on the message type. |
|
109 | # depends on the message type. | |
110 | 'content' : dict, |
|
110 | 'content' : dict, | |
111 | } |
|
111 | } | |
112 |
|
112 | |||
113 | The Wire Protocol |
|
113 | The Wire Protocol | |
114 | ================= |
|
114 | ================= | |
115 |
|
115 | |||
116 |
|
116 | |||
117 | This message format exists at a high level, |
|
117 | This message format exists at a high level, | |
118 | but does not describe the actual *implementation* at the wire level in zeromq. |
|
118 | but does not describe the actual *implementation* at the wire level in zeromq. | |
119 | The canonical implementation of the message spec is our :class:`~IPython.kernel.zmq.session.Session` class. |
|
119 | The canonical implementation of the message spec is our :class:`~IPython.kernel.zmq.session.Session` class. | |
120 |
|
120 | |||
121 | .. note:: |
|
121 | .. note:: | |
122 |
|
122 | |||
123 | This section should only be relevant to non-Python consumers of the protocol. |
|
123 | This section should only be relevant to non-Python consumers of the protocol. | |
124 | Python consumers should simply import and use IPython's own implementation of the wire protocol |
|
124 | Python consumers should simply import and use IPython's own implementation of the wire protocol | |
125 | in the :class:`IPython.kernel.zmq.session.Session` object. |
|
125 | in the :class:`IPython.kernel.zmq.session.Session` object. | |
126 |
|
126 | |||
127 | Every message is serialized to a sequence of at least six blobs of bytes: |
|
127 | Every message is serialized to a sequence of at least six blobs of bytes: | |
128 |
|
128 | |||
129 | .. sourcecode:: python |
|
129 | .. sourcecode:: python | |
130 |
|
130 | |||
131 | [ |
|
131 | [ | |
132 | b'u-u-i-d', # zmq identity(ies) |
|
132 | b'u-u-i-d', # zmq identity(ies) | |
133 | b'<IDS|MSG>', # delimiter |
|
133 | b'<IDS|MSG>', # delimiter | |
134 | b'baddad42', # HMAC signature |
|
134 | b'baddad42', # HMAC signature | |
135 | b'{header}', # serialized header dict |
|
135 | b'{header}', # serialized header dict | |
136 | b'{parent_header}', # serialized parent header dict |
|
136 | b'{parent_header}', # serialized parent header dict | |
137 | b'{metadata}', # serialized metadata dict |
|
137 | b'{metadata}', # serialized metadata dict | |
138 | b'{content}, # serialized content dict |
|
138 | b'{content}, # serialized content dict | |
139 | b'blob', # extra raw data buffer(s) |
|
139 | b'blob', # extra raw data buffer(s) | |
140 | ... |
|
140 | ... | |
141 | ] |
|
141 | ] | |
142 |
|
142 | |||
143 | The front of the message is the ZeroMQ routing prefix, |
|
143 | The front of the message is the ZeroMQ routing prefix, | |
144 | which can be zero or more socket identities. |
|
144 | which can be zero or more socket identities. | |
145 | This is every piece of the message prior to the delimiter key ``<IDS|MSG>``. |
|
145 | This is every piece of the message prior to the delimiter key ``<IDS|MSG>``. | |
146 | In the case of IOPub, there should be just one prefix component, |
|
146 | In the case of IOPub, there should be just one prefix component, | |
147 | which is the topic for IOPub subscribers, e.g. ``pyout``, ``display_data``. |
|
147 | which is the topic for IOPub subscribers, e.g. ``pyout``, ``display_data``. | |
148 |
|
148 | |||
149 | .. note:: |
|
149 | .. note:: | |
150 |
|
150 | |||
151 | In most cases, the IOPub topics are irrelevant and completely ignored, |
|
151 | In most cases, the IOPub topics are irrelevant and completely ignored, | |
152 | because frontends just subscribe to all topics. |
|
152 | because frontends just subscribe to all topics. | |
153 | The convention used in the IPython kernel is to use the msg_type as the topic, |
|
153 | The convention used in the IPython kernel is to use the msg_type as the topic, | |
154 | and possibly extra information about the message, e.g. ``pyout`` or ``stream.stdout`` |
|
154 | and possibly extra information about the message, e.g. ``pyout`` or ``stream.stdout`` | |
155 |
|
155 | |||
156 | After the delimiter is the `HMAC`_ signature of the message, used for authentication. |
|
156 | After the delimiter is the `HMAC`_ signature of the message, used for authentication. | |
157 | If authentication is disabled, this should be an empty string. |
|
157 | If authentication is disabled, this should be an empty string. | |
158 | By default, the hashing function used for computing these signatures is sha256. |
|
158 | By default, the hashing function used for computing these signatures is sha256. | |
159 |
|
159 | |||
160 | .. _HMAC: http://en.wikipedia.org/wiki/HMAC |
|
160 | .. _HMAC: http://en.wikipedia.org/wiki/HMAC | |
161 |
|
161 | |||
162 | .. note:: |
|
162 | .. note:: | |
163 |
|
163 | |||
164 | To disable authentication and signature checking, |
|
164 | To disable authentication and signature checking, | |
165 | set the `key` field of a connection file to an empty string. |
|
165 | set the `key` field of a connection file to an empty string. | |
166 |
|
166 | |||
167 | The signature is the HMAC hex digest of the concatenation of: |
|
167 | The signature is the HMAC hex digest of the concatenation of: | |
168 |
|
168 | |||
169 | - A shared key (typically the ``key`` field of a connection file) |
|
169 | - A shared key (typically the ``key`` field of a connection file) | |
170 | - The serialized header dict |
|
170 | - The serialized header dict | |
171 | - The serialized parent header dict |
|
171 | - The serialized parent header dict | |
172 | - The serialized metadata dict |
|
172 | - The serialized metadata dict | |
173 | - The serialized content dict |
|
173 | - The serialized content dict | |
174 |
|
174 | |||
175 | In Python, this is implemented via: |
|
175 | In Python, this is implemented via: | |
176 |
|
176 | |||
177 | .. sourcecode:: python |
|
177 | .. sourcecode:: python | |
178 |
|
178 | |||
179 | # once: |
|
179 | # once: | |
180 | digester = HMAC(key, digestmod=hashlib.sha256) |
|
180 | digester = HMAC(key, digestmod=hashlib.sha256) | |
181 |
|
181 | |||
182 | # for each message |
|
182 | # for each message | |
183 | d = digester.copy() |
|
183 | d = digester.copy() | |
184 | for serialized_dict in (header, parent, metadata, content): |
|
184 | for serialized_dict in (header, parent, metadata, content): | |
185 | d.update(serialized_dict) |
|
185 | d.update(serialized_dict) | |
186 | signature = d.hexdigest() |
|
186 | signature = d.hexdigest() | |
187 |
|
187 | |||
188 | After the signature is the actual message, always in four frames of bytes. |
|
188 | After the signature is the actual message, always in four frames of bytes. | |
189 | The four dictionaries that compose a message are serialized separately, |
|
189 | The four dictionaries that compose a message are serialized separately, | |
190 | in the order of header, parent header, metadata, and content. |
|
190 | in the order of header, parent header, metadata, and content. | |
191 | These can be serialized by any function that turns a dict into bytes. |
|
191 | These can be serialized by any function that turns a dict into bytes. | |
192 | The default and most common serialization is JSON, but msgpack and pickle |
|
192 | The default and most common serialization is JSON, but msgpack and pickle | |
193 | are common alternatives. |
|
193 | are common alternatives. | |
194 |
|
194 | |||
195 | After the serialized dicts are zero to many raw data buffers, |
|
195 | After the serialized dicts are zero to many raw data buffers, | |
196 | which can be used by message types that support binary data (mainly apply and data_pub). |
|
196 | which can be used by message types that support binary data (mainly apply and data_pub). | |
197 |
|
197 | |||
198 |
|
198 | |||
199 | Python functional API |
|
199 | Python functional API | |
200 | ===================== |
|
200 | ===================== | |
201 |
|
201 | |||
202 | As messages are dicts, they map naturally to a ``func(**kw)`` call form. We |
|
202 | As messages are dicts, they map naturally to a ``func(**kw)`` call form. We | |
203 | should develop, at a few key points, functional forms of all the requests that |
|
203 | should develop, at a few key points, functional forms of all the requests that | |
204 | take arguments in this manner and automatically construct the necessary dict |
|
204 | take arguments in this manner and automatically construct the necessary dict | |
205 | for sending. |
|
205 | for sending. | |
206 |
|
206 | |||
207 | In addition, the Python implementation of the message specification extends |
|
207 | In addition, the Python implementation of the message specification extends | |
208 | messages upon deserialization to the following form for convenience:: |
|
208 | messages upon deserialization to the following form for convenience:: | |
209 |
|
209 | |||
210 | { |
|
210 | { | |
211 | 'header' : dict, |
|
211 | 'header' : dict, | |
212 | # The msg's unique identifier and type are always stored in the header, |
|
212 | # The msg's unique identifier and type are always stored in the header, | |
213 | # but the Python implementation copies them to the top level. |
|
213 | # but the Python implementation copies them to the top level. | |
214 | 'msg_id' : uuid, |
|
214 | 'msg_id' : uuid, | |
215 | 'msg_type' : str, |
|
215 | 'msg_type' : str, | |
216 | 'parent_header' : dict, |
|
216 | 'parent_header' : dict, | |
217 | 'content' : dict, |
|
217 | 'content' : dict, | |
218 | 'metadata' : dict, |
|
218 | 'metadata' : dict, | |
219 | } |
|
219 | } | |
220 |
|
220 | |||
221 | All messages sent to or received by any IPython process should have this |
|
221 | All messages sent to or received by any IPython process should have this | |
222 | extended structure. |
|
222 | extended structure. | |
223 |
|
223 | |||
224 |
|
224 | |||
225 | Messages on the shell ROUTER/DEALER sockets |
|
225 | Messages on the shell ROUTER/DEALER sockets | |
226 | =========================================== |
|
226 | =========================================== | |
227 |
|
227 | |||
228 | .. _execute: |
|
228 | .. _execute: | |
229 |
|
229 | |||
230 | Execute |
|
230 | Execute | |
231 | ------- |
|
231 | ------- | |
232 |
|
232 | |||
233 | This message type is used by frontends to ask the kernel to execute code on |
|
233 | This message type is used by frontends to ask the kernel to execute code on | |
234 | behalf of the user, in a namespace reserved to the user's variables (and thus |
|
234 | behalf of the user, in a namespace reserved to the user's variables (and thus | |
235 | separate from the kernel's own internal code and variables). |
|
235 | separate from the kernel's own internal code and variables). | |
236 |
|
236 | |||
237 | Message type: ``execute_request``:: |
|
237 | Message type: ``execute_request``:: | |
238 |
|
238 | |||
239 | content = { |
|
239 | content = { | |
240 | # Source code to be executed by the kernel, one or more lines. |
|
240 | # Source code to be executed by the kernel, one or more lines. | |
241 | 'code' : str, |
|
241 | 'code' : str, | |
242 |
|
242 | |||
243 | # A boolean flag which, if True, signals the kernel to execute |
|
243 | # A boolean flag which, if True, signals the kernel to execute | |
244 | # this code as quietly as possible. This means that the kernel |
|
244 | # this code as quietly as possible. This means that the kernel | |
245 | # will compile the code with 'exec' instead of 'single' (so |
|
245 | # will compile the code with 'exec' instead of 'single' (so | |
246 | # sys.displayhook will not fire), forces store_history to be False, |
|
246 | # sys.displayhook will not fire), forces store_history to be False, | |
247 | # and will *not*: |
|
247 | # and will *not*: | |
248 | # - broadcast exceptions on the PUB socket |
|
248 | # - broadcast exceptions on the PUB socket | |
249 | # - do any logging |
|
249 | # - do any logging | |
250 | # |
|
250 | # | |
251 | # The default is False. |
|
251 | # The default is False. | |
252 | 'silent' : bool, |
|
252 | 'silent' : bool, | |
253 |
|
253 | |||
254 | # A boolean flag which, if True, signals the kernel to populate history |
|
254 | # A boolean flag which, if True, signals the kernel to populate history | |
255 | # The default is True if silent is False. If silent is True, store_history |
|
255 | # The default is True if silent is False. If silent is True, store_history | |
256 | # is forced to be False. |
|
256 | # is forced to be False. | |
257 | 'store_history' : bool, |
|
257 | 'store_history' : bool, | |
258 |
|
258 | |||
259 | # A list of variable names from the user's namespace to be retrieved. |
|
259 | # A list of variable names from the user's namespace to be retrieved. | |
260 | # What returns is a rich representation of each variable (dict keyed by name). |
|
260 | # What returns is a rich representation of each variable (dict keyed by name). | |
261 | # See the display_data content for the structure of the representation data. |
|
261 | # See the display_data content for the structure of the representation data. | |
262 | 'user_variables' : list, |
|
262 | 'user_variables' : list, | |
263 |
|
263 | |||
264 | # Similarly, a dict mapping names to expressions to be evaluated in the |
|
264 | # Similarly, a dict mapping names to expressions to be evaluated in the | |
265 | # user's dict. |
|
265 | # user's dict. | |
266 | 'user_expressions' : dict, |
|
266 | 'user_expressions' : dict, | |
267 |
|
267 | |||
268 | # Some frontends (e.g. the Notebook) do not support stdin requests. If |
|
268 | # Some frontends (e.g. the Notebook) do not support stdin requests. If | |
269 | # raw_input is called from code executed from such a frontend, a |
|
269 | # raw_input is called from code executed from such a frontend, a | |
270 | # StdinNotImplementedError will be raised. |
|
270 | # StdinNotImplementedError will be raised. | |
271 | 'allow_stdin' : True, |
|
271 | 'allow_stdin' : True, | |
272 |
|
272 | |||
273 | } |
|
273 | } | |
274 |
|
274 | |||
275 | The ``code`` field contains a single string (possibly multiline). The kernel |
|
275 | The ``code`` field contains a single string (possibly multiline). The kernel | |
276 | is responsible for splitting this into one or more independent execution blocks |
|
276 | is responsible for splitting this into one or more independent execution blocks | |
277 | and deciding whether to compile these in 'single' or 'exec' mode (see below for |
|
277 | and deciding whether to compile these in 'single' or 'exec' mode (see below for | |
278 | detailed execution semantics). |
|
278 | detailed execution semantics). | |
279 |
|
279 | |||
280 | The ``user_`` fields deserve a detailed explanation. In the past, IPython had |
|
280 | The ``user_`` fields deserve a detailed explanation. In the past, IPython had | |
281 | the notion of a prompt string that allowed arbitrary code to be evaluated, and |
|
281 | the notion of a prompt string that allowed arbitrary code to be evaluated, and | |
282 | this was put to good use by many in creating prompts that displayed system |
|
282 | this was put to good use by many in creating prompts that displayed system | |
283 | status, path information, and even more esoteric uses like remote instrument |
|
283 | status, path information, and even more esoteric uses like remote instrument | |
284 | status acquired over the network. But now that IPython has a clean separation |
|
284 | status acquired over the network. But now that IPython has a clean separation | |
285 | between the kernel and the clients, the kernel has no prompt knowledge; prompts |
|
285 | between the kernel and the clients, the kernel has no prompt knowledge; prompts | |
286 | are a frontend-side feature, and it should be even possible for different |
|
286 | are a frontend-side feature, and it should be even possible for different | |
287 | frontends to display different prompts while interacting with the same kernel. |
|
287 | frontends to display different prompts while interacting with the same kernel. | |
288 |
|
288 | |||
289 | The kernel now provides the ability to retrieve data from the user's namespace |
|
289 | The kernel now provides the ability to retrieve data from the user's namespace | |
290 | after the execution of the main ``code``, thanks to two fields in the |
|
290 | after the execution of the main ``code``, thanks to two fields in the | |
291 | ``execute_request`` message: |
|
291 | ``execute_request`` message: | |
292 |
|
292 | |||
293 | - ``user_variables``: If only variables from the user's namespace are needed, a |
|
293 | - ``user_variables``: If only variables from the user's namespace are needed, a | |
294 | list of variable names can be passed and a dict with these names as keys and |
|
294 | list of variable names can be passed and a dict with these names as keys and | |
295 | their :func:`repr()` as values will be returned. |
|
295 | their :func:`repr()` as values will be returned. | |
296 |
|
296 | |||
297 | - ``user_expressions``: For more complex expressions that require function |
|
297 | - ``user_expressions``: For more complex expressions that require function | |
298 | evaluations, a dict can be provided with string keys and arbitrary python |
|
298 | evaluations, a dict can be provided with string keys and arbitrary python | |
299 | expressions as values. The return message will contain also a dict with the |
|
299 | expressions as values. The return message will contain also a dict with the | |
300 | same keys and the :func:`repr()` of the evaluated expressions as value. |
|
300 | same keys and the :func:`repr()` of the evaluated expressions as value. | |
301 |
|
301 | |||
302 | With this information, frontends can display any status information they wish |
|
302 | With this information, frontends can display any status information they wish | |
303 | in the form that best suits each frontend (a status line, a popup, inline for a |
|
303 | in the form that best suits each frontend (a status line, a popup, inline for a | |
304 | terminal, etc). |
|
304 | terminal, etc). | |
305 |
|
305 | |||
306 | .. Note:: |
|
306 | .. Note:: | |
307 |
|
307 | |||
308 | In order to obtain the current execution counter for the purposes of |
|
308 | In order to obtain the current execution counter for the purposes of | |
309 | displaying input prompts, frontends simply make an execution request with an |
|
309 | displaying input prompts, frontends simply make an execution request with an | |
310 | empty code string and ``silent=True``. |
|
310 | empty code string and ``silent=True``. | |
311 |
|
311 | |||
312 | Execution semantics |
|
312 | Execution semantics | |
313 | ~~~~~~~~~~~~~~~~~~~ |
|
313 | ~~~~~~~~~~~~~~~~~~~ | |
314 |
|
314 | |||
315 | When the silent flag is false, the execution of use code consists of the |
|
315 | When the silent flag is false, the execution of use code consists of the | |
316 | following phases (in silent mode, only the ``code`` field is executed): |
|
316 | following phases (in silent mode, only the ``code`` field is executed): | |
317 |
|
317 | |||
318 | 1. Run the ``pre_runcode_hook``. |
|
318 | 1. Run the ``pre_runcode_hook``. | |
319 |
|
319 | |||
320 | 2. Execute the ``code`` field, see below for details. |
|
320 | 2. Execute the ``code`` field, see below for details. | |
321 |
|
321 | |||
322 | 3. If #2 succeeds, compute ``user_variables`` and ``user_expressions`` are |
|
322 | 3. If #2 succeeds, compute ``user_variables`` and ``user_expressions`` are | |
323 | computed. This ensures that any error in the latter don't harm the main |
|
323 | computed. This ensures that any error in the latter don't harm the main | |
324 | code execution. |
|
324 | code execution. | |
325 |
|
325 | |||
326 | 4. Call any method registered with :meth:`register_post_execute`. |
|
326 | 4. Call any method registered with :meth:`register_post_execute`. | |
327 |
|
327 | |||
328 | .. warning:: |
|
328 | .. warning:: | |
329 |
|
329 | |||
330 | The API for running code before/after the main code block is likely to |
|
330 | The API for running code before/after the main code block is likely to | |
331 | change soon. Both the ``pre_runcode_hook`` and the |
|
331 | change soon. Both the ``pre_runcode_hook`` and the | |
332 | :meth:`register_post_execute` are susceptible to modification, as we find a |
|
332 | :meth:`register_post_execute` are susceptible to modification, as we find a | |
333 | consistent model for both. |
|
333 | consistent model for both. | |
334 |
|
334 | |||
335 | To understand how the ``code`` field is executed, one must know that Python |
|
335 | To understand how the ``code`` field is executed, one must know that Python | |
336 | code can be compiled in one of three modes (controlled by the ``mode`` argument |
|
336 | code can be compiled in one of three modes (controlled by the ``mode`` argument | |
337 | to the :func:`compile` builtin): |
|
337 | to the :func:`compile` builtin): | |
338 |
|
338 | |||
339 | *single* |
|
339 | *single* | |
340 | Valid for a single interactive statement (though the source can contain |
|
340 | Valid for a single interactive statement (though the source can contain | |
341 | multiple lines, such as a for loop). When compiled in this mode, the |
|
341 | multiple lines, such as a for loop). When compiled in this mode, the | |
342 | generated bytecode contains special instructions that trigger the calling of |
|
342 | generated bytecode contains special instructions that trigger the calling of | |
343 | :func:`sys.displayhook` for any expression in the block that returns a value. |
|
343 | :func:`sys.displayhook` for any expression in the block that returns a value. | |
344 | This means that a single statement can actually produce multiple calls to |
|
344 | This means that a single statement can actually produce multiple calls to | |
345 | :func:`sys.displayhook`, if for example it contains a loop where each |
|
345 | :func:`sys.displayhook`, if for example it contains a loop where each | |
346 | iteration computes an unassigned expression would generate 10 calls:: |
|
346 | iteration computes an unassigned expression would generate 10 calls:: | |
347 |
|
347 | |||
348 | for i in range(10): |
|
348 | for i in range(10): | |
349 | i**2 |
|
349 | i**2 | |
350 |
|
350 | |||
351 | *exec* |
|
351 | *exec* | |
352 | An arbitrary amount of source code, this is how modules are compiled. |
|
352 | An arbitrary amount of source code, this is how modules are compiled. | |
353 | :func:`sys.displayhook` is *never* implicitly called. |
|
353 | :func:`sys.displayhook` is *never* implicitly called. | |
354 |
|
354 | |||
355 | *eval* |
|
355 | *eval* | |
356 | A single expression that returns a value. :func:`sys.displayhook` is *never* |
|
356 | A single expression that returns a value. :func:`sys.displayhook` is *never* | |
357 | implicitly called. |
|
357 | implicitly called. | |
358 |
|
358 | |||
359 |
|
359 | |||
360 | The ``code`` field is split into individual blocks each of which is valid for |
|
360 | The ``code`` field is split into individual blocks each of which is valid for | |
361 | execution in 'single' mode, and then: |
|
361 | execution in 'single' mode, and then: | |
362 |
|
362 | |||
363 | - If there is only a single block: it is executed in 'single' mode. |
|
363 | - If there is only a single block: it is executed in 'single' mode. | |
364 |
|
364 | |||
365 | - If there is more than one block: |
|
365 | - If there is more than one block: | |
366 |
|
366 | |||
367 | * if the last one is a single line long, run all but the last in 'exec' mode |
|
367 | * if the last one is a single line long, run all but the last in 'exec' mode | |
368 | and the very last one in 'single' mode. This makes it easy to type simple |
|
368 | and the very last one in 'single' mode. This makes it easy to type simple | |
369 | expressions at the end to see computed values. |
|
369 | expressions at the end to see computed values. | |
370 |
|
370 | |||
371 | * if the last one is no more than two lines long, run all but the last in |
|
371 | * if the last one is no more than two lines long, run all but the last in | |
372 | 'exec' mode and the very last one in 'single' mode. This makes it easy to |
|
372 | 'exec' mode and the very last one in 'single' mode. This makes it easy to | |
373 | type simple expressions at the end to see computed values. - otherwise |
|
373 | type simple expressions at the end to see computed values. - otherwise | |
374 | (last one is also multiline), run all in 'exec' mode |
|
374 | (last one is also multiline), run all in 'exec' mode | |
375 |
|
375 | |||
376 | * otherwise (last one is also multiline), run all in 'exec' mode as a single |
|
376 | * otherwise (last one is also multiline), run all in 'exec' mode as a single | |
377 | unit. |
|
377 | unit. | |
378 |
|
378 | |||
379 | Any error in retrieving the ``user_variables`` or evaluating the |
|
379 | Any error in retrieving the ``user_variables`` or evaluating the | |
380 | ``user_expressions`` will result in a simple error message in the return fields |
|
380 | ``user_expressions`` will result in a simple error message in the return fields | |
381 | of the form:: |
|
381 | of the form:: | |
382 |
|
382 | |||
383 | [ERROR] ExceptionType: Exception message |
|
383 | [ERROR] ExceptionType: Exception message | |
384 |
|
384 | |||
385 | The user can simply send the same variable name or expression for evaluation to |
|
385 | The user can simply send the same variable name or expression for evaluation to | |
386 | see a regular traceback. |
|
386 | see a regular traceback. | |
387 |
|
387 | |||
388 | Errors in any registered post_execute functions are also reported similarly, |
|
388 | Errors in any registered post_execute functions are also reported similarly, | |
389 | and the failing function is removed from the post_execution set so that it does |
|
389 | and the failing function is removed from the post_execution set so that it does | |
390 | not continue triggering failures. |
|
390 | not continue triggering failures. | |
391 |
|
391 | |||
392 | Upon completion of the execution request, the kernel *always* sends a reply, |
|
392 | Upon completion of the execution request, the kernel *always* sends a reply, | |
393 | with a status code indicating what happened and additional data depending on |
|
393 | with a status code indicating what happened and additional data depending on | |
394 | the outcome. See :ref:`below <execution_results>` for the possible return |
|
394 | the outcome. See :ref:`below <execution_results>` for the possible return | |
395 | codes and associated data. |
|
395 | codes and associated data. | |
396 |
|
396 | |||
397 |
|
397 | |||
398 | Execution counter (old prompt number) |
|
398 | Execution counter (old prompt number) | |
399 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
399 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
400 |
|
400 | |||
401 | The kernel has a single, monotonically increasing counter of all execution |
|
401 | The kernel has a single, monotonically increasing counter of all execution | |
402 | requests that are made with ``store_history=True``. This counter is used to populate |
|
402 | requests that are made with ``store_history=True``. This counter is used to populate | |
403 | the ``In[n]``, ``Out[n]`` and ``_n`` variables, so clients will likely want to |
|
403 | the ``In[n]``, ``Out[n]`` and ``_n`` variables, so clients will likely want to | |
404 | display it in some form to the user, which will typically (but not necessarily) |
|
404 | display it in some form to the user, which will typically (but not necessarily) | |
405 | be done in the prompts. The value of this counter will be returned as the |
|
405 | be done in the prompts. The value of this counter will be returned as the | |
406 | ``execution_count`` field of all ``execute_reply`` messages. |
|
406 | ``execution_count`` field of all ``execute_reply`` messages. | |
407 |
|
407 | |||
408 | .. _execution_results: |
|
408 | .. _execution_results: | |
409 |
|
409 | |||
410 | Execution results |
|
410 | Execution results | |
411 | ~~~~~~~~~~~~~~~~~ |
|
411 | ~~~~~~~~~~~~~~~~~ | |
412 |
|
412 | |||
413 | Message type: ``execute_reply``:: |
|
413 | Message type: ``execute_reply``:: | |
414 |
|
414 | |||
415 | content = { |
|
415 | content = { | |
416 | # One of: 'ok' OR 'error' OR 'abort' |
|
416 | # One of: 'ok' OR 'error' OR 'abort' | |
417 | 'status' : str, |
|
417 | 'status' : str, | |
418 |
|
418 | |||
419 | # The global kernel counter that increases by one with each request that |
|
419 | # The global kernel counter that increases by one with each request that | |
420 | # stores history. This will typically be used by clients to display |
|
420 | # stores history. This will typically be used by clients to display | |
421 | # prompt numbers to the user. If the request did not store history, this will |
|
421 | # prompt numbers to the user. If the request did not store history, this will | |
422 | # be the current value of the counter in the kernel. |
|
422 | # be the current value of the counter in the kernel. | |
423 | 'execution_count' : int, |
|
423 | 'execution_count' : int, | |
424 | } |
|
424 | } | |
425 |
|
425 | |||
426 | When status is 'ok', the following extra fields are present:: |
|
426 | When status is 'ok', the following extra fields are present:: | |
427 |
|
427 | |||
428 | { |
|
428 | { | |
429 | # 'payload' will be a list of payload dicts. |
|
429 | # 'payload' will be a list of payload dicts. | |
430 | # Each execution payload is a dict with string keys that may have been |
|
430 | # Each execution payload is a dict with string keys that may have been | |
431 | # produced by the code being executed. It is retrieved by the kernel at |
|
431 | # produced by the code being executed. It is retrieved by the kernel at | |
432 | # the end of the execution and sent back to the front end, which can take |
|
432 | # the end of the execution and sent back to the front end, which can take | |
433 | # action on it as needed. |
|
433 | # action on it as needed. | |
434 | # The only requirement of each payload dict is that it have a 'source' key, |
|
434 | # The only requirement of each payload dict is that it have a 'source' key, | |
435 | # which is a string classifying the payload (e.g. 'pager'). |
|
435 | # which is a string classifying the payload (e.g. 'pager'). | |
436 | 'payload' : list(dict), |
|
436 | 'payload' : list(dict), | |
437 |
|
437 | |||
438 | # Results for the user_variables and user_expressions. |
|
438 | # Results for the user_variables and user_expressions. | |
439 | 'user_variables' : dict, |
|
439 | 'user_variables' : dict, | |
440 | 'user_expressions' : dict, |
|
440 | 'user_expressions' : dict, | |
441 | } |
|
441 | } | |
442 |
|
442 | |||
443 | .. admonition:: Execution payloads |
|
443 | .. admonition:: Execution payloads | |
444 |
|
444 | |||
445 | The notion of an 'execution payload' is different from a return value of a |
|
445 | The notion of an 'execution payload' is different from a return value of a | |
446 | given set of code, which normally is just displayed on the pyout stream |
|
446 | given set of code, which normally is just displayed on the pyout stream | |
447 | through the PUB socket. The idea of a payload is to allow special types of |
|
447 | through the PUB socket. The idea of a payload is to allow special types of | |
448 | code, typically magics, to populate a data container in the IPython kernel |
|
448 | code, typically magics, to populate a data container in the IPython kernel | |
449 | that will be shipped back to the caller via this channel. The kernel |
|
449 | that will be shipped back to the caller via this channel. The kernel | |
450 | has an API for this in the PayloadManager:: |
|
450 | has an API for this in the PayloadManager:: | |
451 |
|
451 | |||
452 | ip.payload_manager.write_payload(payload_dict) |
|
452 | ip.payload_manager.write_payload(payload_dict) | |
453 |
|
453 | |||
454 | which appends a dictionary to the list of payloads. |
|
454 | which appends a dictionary to the list of payloads. | |
455 |
|
455 | |||
456 | The payload API is not yet stabilized, |
|
456 | The payload API is not yet stabilized, | |
457 | and should probably not be supported by non-Python kernels at this time. |
|
457 | and should probably not be supported by non-Python kernels at this time. | |
458 | In such cases, the payload list should always be empty. |
|
458 | In such cases, the payload list should always be empty. | |
459 |
|
459 | |||
460 |
|
460 | |||
461 | When status is 'error', the following extra fields are present:: |
|
461 | When status is 'error', the following extra fields are present:: | |
462 |
|
462 | |||
463 | { |
|
463 | { | |
464 | 'ename' : str, # Exception name, as a string |
|
464 | 'ename' : str, # Exception name, as a string | |
465 | 'evalue' : str, # Exception value, as a string |
|
465 | 'evalue' : str, # Exception value, as a string | |
466 |
|
466 | |||
467 | # The traceback will contain a list of frames, represented each as a |
|
467 | # The traceback will contain a list of frames, represented each as a | |
468 | # string. For now we'll stick to the existing design of ultraTB, which |
|
468 | # string. For now we'll stick to the existing design of ultraTB, which | |
469 | # controls exception level of detail statefully. But eventually we'll |
|
469 | # controls exception level of detail statefully. But eventually we'll | |
470 | # want to grow into a model where more information is collected and |
|
470 | # want to grow into a model where more information is collected and | |
471 | # packed into the traceback object, with clients deciding how little or |
|
471 | # packed into the traceback object, with clients deciding how little or | |
472 | # how much of it to unpack. But for now, let's start with a simple list |
|
472 | # how much of it to unpack. But for now, let's start with a simple list | |
473 | # of strings, since that requires only minimal changes to ultratb as |
|
473 | # of strings, since that requires only minimal changes to ultratb as | |
474 | # written. |
|
474 | # written. | |
475 | 'traceback' : list, |
|
475 | 'traceback' : list, | |
476 | } |
|
476 | } | |
477 |
|
477 | |||
478 |
|
478 | |||
479 | When status is 'abort', there are for now no additional data fields. This |
|
479 | When status is 'abort', there are for now no additional data fields. This | |
480 | happens when the kernel was interrupted by a signal. |
|
480 | happens when the kernel was interrupted by a signal. | |
481 |
|
481 | |||
482 |
|
482 | |||
483 | Object information |
|
483 | Object information | |
484 | ------------------ |
|
484 | ------------------ | |
485 |
|
485 | |||
486 | One of IPython's most used capabilities is the introspection of Python objects |
|
486 | One of IPython's most used capabilities is the introspection of Python objects | |
487 | in the user's namespace, typically invoked via the ``?`` and ``??`` characters |
|
487 | in the user's namespace, typically invoked via the ``?`` and ``??`` characters | |
488 | (which in reality are shorthands for the ``%pinfo`` magic). This is used often |
|
488 | (which in reality are shorthands for the ``%pinfo`` magic). This is used often | |
489 | enough that it warrants an explicit message type, especially because frontends |
|
489 | enough that it warrants an explicit message type, especially because frontends | |
490 | may want to get object information in response to user keystrokes (like Tab or |
|
490 | may want to get object information in response to user keystrokes (like Tab or | |
491 | F1) besides from the user explicitly typing code like ``x??``. |
|
491 | F1) besides from the user explicitly typing code like ``x??``. | |
492 |
|
492 | |||
493 | Message type: ``object_info_request``:: |
|
493 | Message type: ``object_info_request``:: | |
494 |
|
494 | |||
495 | content = { |
|
495 | content = { | |
496 | # The (possibly dotted) name of the object to be searched in all |
|
496 | # The (possibly dotted) name of the object to be searched in all | |
497 | # relevant namespaces |
|
497 | # relevant namespaces | |
498 | 'oname' : str, |
|
498 | 'oname' : str, | |
499 |
|
499 | |||
500 | # The level of detail desired. The default (0) is equivalent to typing |
|
500 | # The level of detail desired. The default (0) is equivalent to typing | |
501 | # 'x?' at the prompt, 1 is equivalent to 'x??'. |
|
501 | # 'x?' at the prompt, 1 is equivalent to 'x??'. | |
502 | 'detail_level' : int, |
|
502 | 'detail_level' : int, | |
503 | } |
|
503 | } | |
504 |
|
504 | |||
505 | The returned information will be a dictionary with keys very similar to the |
|
505 | The returned information will be a dictionary with keys very similar to the | |
506 | field names that IPython prints at the terminal. |
|
506 | field names that IPython prints at the terminal. | |
507 |
|
507 | |||
508 | Message type: ``object_info_reply``:: |
|
508 | Message type: ``object_info_reply``:: | |
509 |
|
509 | |||
510 | content = { |
|
510 | content = { | |
511 | # The name the object was requested under |
|
511 | # The name the object was requested under | |
512 | 'name' : str, |
|
512 | 'name' : str, | |
513 |
|
513 | |||
514 | # Boolean flag indicating whether the named object was found or not. If |
|
514 | # Boolean flag indicating whether the named object was found or not. If | |
515 | # it's false, all other fields will be empty. |
|
515 | # it's false, all other fields will be empty. | |
516 | 'found' : bool, |
|
516 | 'found' : bool, | |
517 |
|
517 | |||
518 | # Flags for magics and system aliases |
|
518 | # Flags for magics and system aliases | |
519 | 'ismagic' : bool, |
|
519 | 'ismagic' : bool, | |
520 | 'isalias' : bool, |
|
520 | 'isalias' : bool, | |
521 |
|
521 | |||
522 | # The name of the namespace where the object was found ('builtin', |
|
522 | # The name of the namespace where the object was found ('builtin', | |
523 | # 'magics', 'alias', 'interactive', etc.) |
|
523 | # 'magics', 'alias', 'interactive', etc.) | |
524 | 'namespace' : str, |
|
524 | 'namespace' : str, | |
525 |
|
525 | |||
526 | # The type name will be type.__name__ for normal Python objects, but it |
|
526 | # The type name will be type.__name__ for normal Python objects, but it | |
527 | # can also be a string like 'Magic function' or 'System alias' |
|
527 | # can also be a string like 'Magic function' or 'System alias' | |
528 | 'type_name' : str, |
|
528 | 'type_name' : str, | |
529 |
|
529 | |||
530 | # The string form of the object, possibly truncated for length if |
|
530 | # The string form of the object, possibly truncated for length if | |
531 | # detail_level is 0 |
|
531 | # detail_level is 0 | |
532 | 'string_form' : str, |
|
532 | 'string_form' : str, | |
533 |
|
533 | |||
534 | # For objects with a __class__ attribute this will be set |
|
534 | # For objects with a __class__ attribute this will be set | |
535 | 'base_class' : str, |
|
535 | 'base_class' : str, | |
536 |
|
536 | |||
537 | # For objects with a __len__ attribute this will be set |
|
537 | # For objects with a __len__ attribute this will be set | |
538 | 'length' : int, |
|
538 | 'length' : int, | |
539 |
|
539 | |||
540 | # If the object is a function, class or method whose file we can find, |
|
540 | # If the object is a function, class or method whose file we can find, | |
541 | # we give its full path |
|
541 | # we give its full path | |
542 | 'file' : str, |
|
542 | 'file' : str, | |
543 |
|
543 | |||
544 | # For pure Python callable objects, we can reconstruct the object |
|
544 | # For pure Python callable objects, we can reconstruct the object | |
545 | # definition line which provides its call signature. For convenience this |
|
545 | # definition line which provides its call signature. For convenience this | |
546 | # is returned as a single 'definition' field, but below the raw parts that |
|
546 | # is returned as a single 'definition' field, but below the raw parts that | |
547 | # compose it are also returned as the argspec field. |
|
547 | # compose it are also returned as the argspec field. | |
548 | 'definition' : str, |
|
548 | 'definition' : str, | |
549 |
|
549 | |||
550 | # The individual parts that together form the definition string. Clients |
|
550 | # The individual parts that together form the definition string. Clients | |
551 | # with rich display capabilities may use this to provide a richer and more |
|
551 | # with rich display capabilities may use this to provide a richer and more | |
552 | # precise representation of the definition line (e.g. by highlighting |
|
552 | # precise representation of the definition line (e.g. by highlighting | |
553 | # arguments based on the user's cursor position). For non-callable |
|
553 | # arguments based on the user's cursor position). For non-callable | |
554 | # objects, this field is empty. |
|
554 | # objects, this field is empty. | |
555 | 'argspec' : { # The names of all the arguments |
|
555 | 'argspec' : { # The names of all the arguments | |
556 | args : list, |
|
556 | args : list, | |
557 | # The name of the varargs (*args), if any |
|
557 | # The name of the varargs (*args), if any | |
558 | varargs : str, |
|
558 | varargs : str, | |
559 | # The name of the varkw (**kw), if any |
|
559 | # The name of the varkw (**kw), if any | |
560 | varkw : str, |
|
560 | varkw : str, | |
561 | # The values (as strings) of all default arguments. Note |
|
561 | # The values (as strings) of all default arguments. Note | |
562 | # that these must be matched *in reverse* with the 'args' |
|
562 | # that these must be matched *in reverse* with the 'args' | |
563 | # list above, since the first positional args have no default |
|
563 | # list above, since the first positional args have no default | |
564 | # value at all. |
|
564 | # value at all. | |
565 | defaults : list, |
|
565 | defaults : list, | |
566 | }, |
|
566 | }, | |
567 |
|
567 | |||
568 | # For instances, provide the constructor signature (the definition of |
|
568 | # For instances, provide the constructor signature (the definition of | |
569 | # the __init__ method): |
|
569 | # the __init__ method): | |
570 | 'init_definition' : str, |
|
570 | 'init_definition' : str, | |
571 |
|
571 | |||
572 | # Docstrings: for any object (function, method, module, package) with a |
|
572 | # Docstrings: for any object (function, method, module, package) with a | |
573 | # docstring, we show it. But in addition, we may provide additional |
|
573 | # docstring, we show it. But in addition, we may provide additional | |
574 | # docstrings. For example, for instances we will show the constructor |
|
574 | # docstrings. For example, for instances we will show the constructor | |
575 | # and class docstrings as well, if available. |
|
575 | # and class docstrings as well, if available. | |
576 | 'docstring' : str, |
|
576 | 'docstring' : str, | |
577 |
|
577 | |||
578 | # For instances, provide the constructor and class docstrings |
|
578 | # For instances, provide the constructor and class docstrings | |
579 | 'init_docstring' : str, |
|
579 | 'init_docstring' : str, | |
580 | 'class_docstring' : str, |
|
580 | 'class_docstring' : str, | |
581 |
|
581 | |||
582 | # If it's a callable object whose call method has a separate docstring and |
|
582 | # If it's a callable object whose call method has a separate docstring and | |
583 | # definition line: |
|
583 | # definition line: | |
584 | 'call_def' : str, |
|
584 | 'call_def' : str, | |
585 | 'call_docstring' : str, |
|
585 | 'call_docstring' : str, | |
586 |
|
586 | |||
587 | # If detail_level was 1, we also try to find the source code that |
|
587 | # If detail_level was 1, we also try to find the source code that | |
588 | # defines the object, if possible. The string 'None' will indicate |
|
588 | # defines the object, if possible. The string 'None' will indicate | |
589 | # that no source was found. |
|
589 | # that no source was found. | |
590 | 'source' : str, |
|
590 | 'source' : str, | |
591 | } |
|
591 | } | |
592 |
|
592 | |||
593 |
|
593 | |||
594 | Complete |
|
594 | Complete | |
595 | -------- |
|
595 | -------- | |
596 |
|
596 | |||
597 | Message type: ``complete_request``:: |
|
597 | Message type: ``complete_request``:: | |
598 |
|
598 | |||
599 | content = { |
|
599 | content = { | |
600 | # The text to be completed, such as 'a.is' |
|
600 | # The text to be completed, such as 'a.is' | |
601 | # this may be an empty string if the frontend does not do any lexing, |
|
601 | # this may be an empty string if the frontend does not do any lexing, | |
602 | # in which case the kernel must figure out the completion |
|
602 | # in which case the kernel must figure out the completion | |
603 | # based on 'line' and 'cursor_pos'. |
|
603 | # based on 'line' and 'cursor_pos'. | |
604 | 'text' : str, |
|
604 | 'text' : str, | |
605 |
|
605 | |||
606 | # The full line, such as 'print a.is'. This allows completers to |
|
606 | # The full line, such as 'print a.is'. This allows completers to | |
607 | # make decisions that may require information about more than just the |
|
607 | # make decisions that may require information about more than just the | |
608 | # current word. |
|
608 | # current word. | |
609 | 'line' : str, |
|
609 | 'line' : str, | |
610 |
|
610 | |||
611 | # The entire block of text where the line is. This may be useful in the |
|
611 | # The entire block of text where the line is. This may be useful in the | |
612 | # case of multiline completions where more context may be needed. Note: if |
|
612 | # case of multiline completions where more context may be needed. Note: if | |
613 | # in practice this field proves unnecessary, remove it to lighten the |
|
613 | # in practice this field proves unnecessary, remove it to lighten the | |
614 | # messages. |
|
614 | # messages. | |
615 |
|
615 | |||
616 | 'block' : str or null/None, |
|
616 | 'block' : str or null/None, | |
617 |
|
617 | |||
618 | # The position of the cursor where the user hit 'TAB' on the line. |
|
618 | # The position of the cursor where the user hit 'TAB' on the line. | |
619 | 'cursor_pos' : int, |
|
619 | 'cursor_pos' : int, | |
620 | } |
|
620 | } | |
621 |
|
621 | |||
622 | Message type: ``complete_reply``:: |
|
622 | Message type: ``complete_reply``:: | |
623 |
|
623 | |||
624 | content = { |
|
624 | content = { | |
625 | # The list of all matches to the completion request, such as |
|
625 | # The list of all matches to the completion request, such as | |
626 | # ['a.isalnum', 'a.isalpha'] for the above example. |
|
626 | # ['a.isalnum', 'a.isalpha'] for the above example. | |
627 | 'matches' : list, |
|
627 | 'matches' : list, | |
628 |
|
628 | |||
629 | # the substring of the matched text |
|
629 | # the substring of the matched text | |
630 | # this is typically the common prefix of the matches, |
|
630 | # this is typically the common prefix of the matches, | |
631 | # and the text that is already in the block that would be replaced by the full completion. |
|
631 | # and the text that is already in the block that would be replaced by the full completion. | |
632 | # This would be 'a.is' in the above example. |
|
632 | # This would be 'a.is' in the above example. | |
633 | 'matched_text' : str, |
|
633 | 'matched_text' : str, | |
634 |
|
634 | |||
635 | # status should be 'ok' unless an exception was raised during the request, |
|
635 | # status should be 'ok' unless an exception was raised during the request, | |
636 | # in which case it should be 'error', along with the usual error message content |
|
636 | # in which case it should be 'error', along with the usual error message content | |
637 | # in other messages. |
|
637 | # in other messages. | |
638 | 'status' : 'ok' |
|
638 | 'status' : 'ok' | |
639 | } |
|
639 | } | |
640 |
|
640 | |||
641 |
|
641 | |||
642 | History |
|
642 | History | |
643 | ------- |
|
643 | ------- | |
644 |
|
644 | |||
645 | For clients to explicitly request history from a kernel. The kernel has all |
|
645 | For clients to explicitly request history from a kernel. The kernel has all | |
646 | the actual execution history stored in a single location, so clients can |
|
646 | the actual execution history stored in a single location, so clients can | |
647 | request it from the kernel when needed. |
|
647 | request it from the kernel when needed. | |
648 |
|
648 | |||
649 | Message type: ``history_request``:: |
|
649 | Message type: ``history_request``:: | |
650 |
|
650 | |||
651 | content = { |
|
651 | content = { | |
652 |
|
652 | |||
653 | # If True, also return output history in the resulting dict. |
|
653 | # If True, also return output history in the resulting dict. | |
654 | 'output' : bool, |
|
654 | 'output' : bool, | |
655 |
|
655 | |||
656 | # If True, return the raw input history, else the transformed input. |
|
656 | # If True, return the raw input history, else the transformed input. | |
657 | 'raw' : bool, |
|
657 | 'raw' : bool, | |
658 |
|
658 | |||
659 | # So far, this can be 'range', 'tail' or 'search'. |
|
659 | # So far, this can be 'range', 'tail' or 'search'. | |
660 | 'hist_access_type' : str, |
|
660 | 'hist_access_type' : str, | |
661 |
|
661 | |||
662 | # If hist_access_type is 'range', get a range of input cells. session can |
|
662 | # If hist_access_type is 'range', get a range of input cells. session can | |
663 | # be a positive session number, or a negative number to count back from |
|
663 | # be a positive session number, or a negative number to count back from | |
664 | # the current session. |
|
664 | # the current session. | |
665 | 'session' : int, |
|
665 | 'session' : int, | |
666 | # start and stop are line numbers within that session. |
|
666 | # start and stop are line numbers within that session. | |
667 | 'start' : int, |
|
667 | 'start' : int, | |
668 | 'stop' : int, |
|
668 | 'stop' : int, | |
669 |
|
669 | |||
670 | # If hist_access_type is 'tail' or 'search', get the last n cells. |
|
670 | # If hist_access_type is 'tail' or 'search', get the last n cells. | |
671 | 'n' : int, |
|
671 | 'n' : int, | |
672 |
|
672 | |||
673 | # If hist_access_type is 'search', get cells matching the specified glob |
|
673 | # If hist_access_type is 'search', get cells matching the specified glob | |
674 | # pattern (with * and ? as wildcards). |
|
674 | # pattern (with * and ? as wildcards). | |
675 | 'pattern' : str, |
|
675 | 'pattern' : str, | |
676 |
|
676 | |||
677 | # If hist_access_type is 'search' and unique is true, do not |
|
677 | # If hist_access_type is 'search' and unique is true, do not | |
678 | # include duplicated history. Default is false. |
|
678 | # include duplicated history. Default is false. | |
679 | 'unique' : bool, |
|
679 | 'unique' : bool, | |
680 |
|
680 | |||
681 | } |
|
681 | } | |
682 |
|
682 | |||
683 | .. versionadded:: 4.0 |
|
683 | .. versionadded:: 4.0 | |
684 | The key ``unique`` for ``history_request``. |
|
684 | The key ``unique`` for ``history_request``. | |
685 |
|
685 | |||
686 | Message type: ``history_reply``:: |
|
686 | Message type: ``history_reply``:: | |
687 |
|
687 | |||
688 | content = { |
|
688 | content = { | |
689 | # A list of 3 tuples, either: |
|
689 | # A list of 3 tuples, either: | |
690 | # (session, line_number, input) or |
|
690 | # (session, line_number, input) or | |
691 | # (session, line_number, (input, output)), |
|
691 | # (session, line_number, (input, output)), | |
692 | # depending on whether output was False or True, respectively. |
|
692 | # depending on whether output was False or True, respectively. | |
693 | 'history' : list, |
|
693 | 'history' : list, | |
694 | } |
|
694 | } | |
695 |
|
695 | |||
696 |
|
696 | |||
697 | Connect |
|
697 | Connect | |
698 | ------- |
|
698 | ------- | |
699 |
|
699 | |||
700 | When a client connects to the request/reply socket of the kernel, it can issue |
|
700 | When a client connects to the request/reply socket of the kernel, it can issue | |
701 | a connect request to get basic information about the kernel, such as the ports |
|
701 | a connect request to get basic information about the kernel, such as the ports | |
702 | the other ZeroMQ sockets are listening on. This allows clients to only have |
|
702 | the other ZeroMQ sockets are listening on. This allows clients to only have | |
703 | to know about a single port (the shell channel) to connect to a kernel. |
|
703 | to know about a single port (the shell channel) to connect to a kernel. | |
704 |
|
704 | |||
705 | Message type: ``connect_request``:: |
|
705 | Message type: ``connect_request``:: | |
706 |
|
706 | |||
707 | content = { |
|
707 | content = { | |
708 | } |
|
708 | } | |
709 |
|
709 | |||
710 | Message type: ``connect_reply``:: |
|
710 | Message type: ``connect_reply``:: | |
711 |
|
711 | |||
712 | content = { |
|
712 | content = { | |
713 | 'shell_port' : int, # The port the shell ROUTER socket is listening on. |
|
713 | 'shell_port' : int, # The port the shell ROUTER socket is listening on. | |
714 | 'iopub_port' : int, # The port the PUB socket is listening on. |
|
714 | 'iopub_port' : int, # The port the PUB socket is listening on. | |
715 | 'stdin_port' : int, # The port the stdin ROUTER socket is listening on. |
|
715 | 'stdin_port' : int, # The port the stdin ROUTER socket is listening on. | |
716 | 'hb_port' : int, # The port the heartbeat socket is listening on. |
|
716 | 'hb_port' : int, # The port the heartbeat socket is listening on. | |
717 | } |
|
717 | } | |
718 |
|
718 | |||
719 |
|
719 | |||
720 | Kernel info |
|
720 | Kernel info | |
721 | ----------- |
|
721 | ----------- | |
722 |
|
722 | |||
723 | If a client needs to know information about the kernel, it can |
|
723 | If a client needs to know information about the kernel, it can | |
724 | make a request of the kernel's information. |
|
724 | make a request of the kernel's information. | |
725 | This message can be used to fetch core information of the |
|
725 | This message can be used to fetch core information of the | |
726 | kernel, including language (e.g., Python), language version number and |
|
726 | kernel, including language (e.g., Python), language version number and | |
727 | IPython version number, and the IPython message spec version number. |
|
727 | IPython version number, and the IPython message spec version number. | |
728 |
|
728 | |||
729 | Message type: ``kernel_info_request``:: |
|
729 | Message type: ``kernel_info_request``:: | |
730 |
|
730 | |||
731 | content = { |
|
731 | content = { | |
732 | } |
|
732 | } | |
733 |
|
733 | |||
734 | Message type: ``kernel_info_reply``:: |
|
734 | Message type: ``kernel_info_reply``:: | |
735 |
|
735 | |||
736 | content = { |
|
736 | content = { | |
737 | # Version of messaging protocol (mandatory). |
|
737 | # Version of messaging protocol (mandatory). | |
738 | # The first integer indicates major version. It is incremented when |
|
738 | # The first integer indicates major version. It is incremented when | |
739 | # there is any backward incompatible change. |
|
739 | # there is any backward incompatible change. | |
740 | # The second integer indicates minor version. It is incremented when |
|
740 | # The second integer indicates minor version. It is incremented when | |
741 | # there is any backward compatible change. |
|
741 | # there is any backward compatible change. | |
742 | 'protocol_version': [int, int], |
|
742 | 'protocol_version': [int, int], | |
743 |
|
743 | |||
744 | # IPython version number (optional). |
|
744 | # IPython version number (optional). | |
745 | # Non-python kernel backend may not have this version number. |
|
745 | # Non-python kernel backend may not have this version number. | |
746 | # The last component is an extra field, which may be 'dev' or |
|
746 | # The last component is an extra field, which may be 'dev' or | |
747 | # 'rc1' in development version. It is an empty string for |
|
747 | # 'rc1' in development version. It is an empty string for | |
748 | # released version. |
|
748 | # released version. | |
749 | 'ipython_version': [int, int, int, str], |
|
749 | 'ipython_version': [int, int, int, str], | |
750 |
|
750 | |||
751 | # Language version number (mandatory). |
|
751 | # Language version number (mandatory). | |
752 | # It is Python version number (e.g., [2, 7, 3]) for the kernel |
|
752 | # It is Python version number (e.g., [2, 7, 3]) for the kernel | |
753 | # included in IPython. |
|
753 | # included in IPython. | |
754 | 'language_version': [int, ...], |
|
754 | 'language_version': [int, ...], | |
755 |
|
755 | |||
756 | # Programming language in which kernel is implemented (mandatory). |
|
756 | # Programming language in which kernel is implemented (mandatory). | |
757 | # Kernel included in IPython returns 'python'. |
|
757 | # Kernel included in IPython returns 'python'. | |
758 | 'language': str, |
|
758 | 'language': str, | |
759 | } |
|
759 | } | |
760 |
|
760 | |||
761 |
|
761 | |||
762 | Kernel shutdown |
|
762 | Kernel shutdown | |
763 | --------------- |
|
763 | --------------- | |
764 |
|
764 | |||
765 | The clients can request the kernel to shut itself down; this is used in |
|
765 | The clients can request the kernel to shut itself down; this is used in | |
766 | multiple cases: |
|
766 | multiple cases: | |
767 |
|
767 | |||
768 | - when the user chooses to close the client application via a menu or window |
|
768 | - when the user chooses to close the client application via a menu or window | |
769 | control. |
|
769 | control. | |
770 | - when the user types 'exit' or 'quit' (or their uppercase magic equivalents). |
|
770 | - when the user types 'exit' or 'quit' (or their uppercase magic equivalents). | |
771 | - when the user chooses a GUI method (like the 'Ctrl-C' shortcut in the |
|
771 | - when the user chooses a GUI method (like the 'Ctrl-C' shortcut in the | |
772 | IPythonQt client) to force a kernel restart to get a clean kernel without |
|
772 | IPythonQt client) to force a kernel restart to get a clean kernel without | |
773 | losing client-side state like history or inlined figures. |
|
773 | losing client-side state like history or inlined figures. | |
774 |
|
774 | |||
775 | The client sends a shutdown request to the kernel, and once it receives the |
|
775 | The client sends a shutdown request to the kernel, and once it receives the | |
776 | reply message (which is otherwise empty), it can assume that the kernel has |
|
776 | reply message (which is otherwise empty), it can assume that the kernel has | |
777 | completed shutdown safely. |
|
777 | completed shutdown safely. | |
778 |
|
778 | |||
779 | Upon their own shutdown, client applications will typically execute a last |
|
779 | Upon their own shutdown, client applications will typically execute a last | |
780 | minute sanity check and forcefully terminate any kernel that is still alive, to |
|
780 | minute sanity check and forcefully terminate any kernel that is still alive, to | |
781 | avoid leaving stray processes in the user's machine. |
|
781 | avoid leaving stray processes in the user's machine. | |
782 |
|
782 | |||
783 | Message type: ``shutdown_request``:: |
|
783 | Message type: ``shutdown_request``:: | |
784 |
|
784 | |||
785 | content = { |
|
785 | content = { | |
786 | 'restart' : bool # whether the shutdown is final, or precedes a restart |
|
786 | 'restart' : bool # whether the shutdown is final, or precedes a restart | |
787 | } |
|
787 | } | |
788 |
|
788 | |||
789 | Message type: ``shutdown_reply``:: |
|
789 | Message type: ``shutdown_reply``:: | |
790 |
|
790 | |||
791 | content = { |
|
791 | content = { | |
792 | 'restart' : bool # whether the shutdown is final, or precedes a restart |
|
792 | 'restart' : bool # whether the shutdown is final, or precedes a restart | |
793 | } |
|
793 | } | |
794 |
|
794 | |||
795 | .. Note:: |
|
795 | .. Note:: | |
796 |
|
796 | |||
797 | When the clients detect a dead kernel thanks to inactivity on the heartbeat |
|
797 | When the clients detect a dead kernel thanks to inactivity on the heartbeat | |
798 | socket, they simply send a forceful process termination signal, since a dead |
|
798 | socket, they simply send a forceful process termination signal, since a dead | |
799 | process is unlikely to respond in any useful way to messages. |
|
799 | process is unlikely to respond in any useful way to messages. | |
800 |
|
800 | |||
801 |
|
801 | |||
802 | Messages on the PUB/SUB socket |
|
802 | Messages on the PUB/SUB socket | |
803 | ============================== |
|
803 | ============================== | |
804 |
|
804 | |||
805 | Streams (stdout, stderr, etc) |
|
805 | Streams (stdout, stderr, etc) | |
806 | ------------------------------ |
|
806 | ------------------------------ | |
807 |
|
807 | |||
808 | Message type: ``stream``:: |
|
808 | Message type: ``stream``:: | |
809 |
|
809 | |||
810 | content = { |
|
810 | content = { | |
811 | # The name of the stream is one of 'stdout', 'stderr' |
|
811 | # The name of the stream is one of 'stdout', 'stderr' | |
812 | 'name' : str, |
|
812 | 'name' : str, | |
813 |
|
813 | |||
814 | # The data is an arbitrary string to be written to that stream |
|
814 | # The data is an arbitrary string to be written to that stream | |
815 | 'data' : str, |
|
815 | 'data' : str, | |
816 | } |
|
816 | } | |
817 |
|
817 | |||
818 | Display Data |
|
818 | Display Data | |
819 | ------------ |
|
819 | ------------ | |
820 |
|
820 | |||
821 | This type of message is used to bring back data that should be diplayed (text, |
|
821 | This type of message is used to bring back data that should be displayed (text, | |
822 | html, svg, etc.) in the frontends. This data is published to all frontends. |
|
822 | html, svg, etc.) in the frontends. This data is published to all frontends. | |
823 | Each message can have multiple representations of the data; it is up to the |
|
823 | Each message can have multiple representations of the data; it is up to the | |
824 | frontend to decide which to use and how. A single message should contain all |
|
824 | frontend to decide which to use and how. A single message should contain all | |
825 | possible representations of the same information. Each representation should |
|
825 | possible representations of the same information. Each representation should | |
826 | be a JSON'able data structure, and should be a valid MIME type. |
|
826 | be a JSON'able data structure, and should be a valid MIME type. | |
827 |
|
827 | |||
828 | Some questions remain about this design: |
|
828 | Some questions remain about this design: | |
829 |
|
829 | |||
830 | * Do we use this message type for pyout/displayhook? Probably not, because |
|
830 | * Do we use this message type for pyout/displayhook? Probably not, because | |
831 | the displayhook also has to handle the Out prompt display. On the other hand |
|
831 | the displayhook also has to handle the Out prompt display. On the other hand | |
832 | we could put that information into the metadata secion. |
|
832 | we could put that information into the metadata section. | |
833 |
|
833 | |||
834 | Message type: ``display_data``:: |
|
834 | Message type: ``display_data``:: | |
835 |
|
835 | |||
836 | content = { |
|
836 | content = { | |
837 |
|
837 | |||
838 | # Who create the data |
|
838 | # Who create the data | |
839 | 'source' : str, |
|
839 | 'source' : str, | |
840 |
|
840 | |||
841 |
# The data dict contains key/value pairs, where the k |
|
841 | # The data dict contains key/value pairs, where the keys are MIME | |
842 | # types and the values are the raw data of the representation in that |
|
842 | # types and the values are the raw data of the representation in that | |
843 | # format. |
|
843 | # format. | |
844 | 'data' : dict, |
|
844 | 'data' : dict, | |
845 |
|
845 | |||
846 | # Any metadata that describes the data |
|
846 | # Any metadata that describes the data | |
847 | 'metadata' : dict |
|
847 | 'metadata' : dict | |
848 | } |
|
848 | } | |
849 |
|
849 | |||
850 |
|
850 | |||
851 | The ``metadata`` contains any metadata that describes the output. |
|
851 | The ``metadata`` contains any metadata that describes the output. | |
852 | Global keys are assumed to apply to the output as a whole. |
|
852 | Global keys are assumed to apply to the output as a whole. | |
853 | The ``metadata`` dict can also contain mime-type keys, which will be sub-dictionaries, |
|
853 | The ``metadata`` dict can also contain mime-type keys, which will be sub-dictionaries, | |
854 | which are interpreted as applying only to output of that type. |
|
854 | which are interpreted as applying only to output of that type. | |
855 | Third parties should put any data they write into a single dict |
|
855 | Third parties should put any data they write into a single dict | |
856 | with a reasonably unique name to avoid conflicts. |
|
856 | with a reasonably unique name to avoid conflicts. | |
857 |
|
857 | |||
858 | The only metadata keys currently defined in IPython are the width and height |
|
858 | The only metadata keys currently defined in IPython are the width and height | |
859 | of images:: |
|
859 | of images:: | |
860 |
|
860 | |||
861 | 'metadata' : { |
|
861 | 'metadata' : { | |
862 | 'image/png' : { |
|
862 | 'image/png' : { | |
863 | 'width': 640, |
|
863 | 'width': 640, | |
864 | 'height': 480 |
|
864 | 'height': 480 | |
865 | } |
|
865 | } | |
866 | } |
|
866 | } | |
867 |
|
867 | |||
868 |
|
868 | |||
869 | Raw Data Publication |
|
869 | Raw Data Publication | |
870 | -------------------- |
|
870 | -------------------- | |
871 |
|
871 | |||
872 | ``display_data`` lets you publish *representations* of data, such as images and html. |
|
872 | ``display_data`` lets you publish *representations* of data, such as images and html. | |
873 | This ``data_pub`` message lets you publish *actual raw data*, sent via message buffers. |
|
873 | This ``data_pub`` message lets you publish *actual raw data*, sent via message buffers. | |
874 |
|
874 | |||
875 | data_pub messages are constructed via the :func:`IPython.lib.datapub.publish_data` function: |
|
875 | data_pub messages are constructed via the :func:`IPython.lib.datapub.publish_data` function: | |
876 |
|
876 | |||
877 | .. sourcecode:: python |
|
877 | .. sourcecode:: python | |
878 |
|
878 | |||
879 | from IPython.kernel.zmq.datapub import publish_data |
|
879 | from IPython.kernel.zmq.datapub import publish_data | |
880 | ns = dict(x=my_array) |
|
880 | ns = dict(x=my_array) | |
881 | publish_data(ns) |
|
881 | publish_data(ns) | |
882 |
|
882 | |||
883 |
|
883 | |||
884 | Message type: ``data_pub``:: |
|
884 | Message type: ``data_pub``:: | |
885 |
|
885 | |||
886 | content = { |
|
886 | content = { | |
887 | # the keys of the data dict, after it has been unserialized |
|
887 | # the keys of the data dict, after it has been unserialized | |
888 | keys = ['a', 'b'] |
|
888 | keys = ['a', 'b'] | |
889 | } |
|
889 | } | |
890 | # the namespace dict will be serialized in the message buffers, |
|
890 | # the namespace dict will be serialized in the message buffers, | |
891 | # which will have a length of at least one |
|
891 | # which will have a length of at least one | |
892 | buffers = ['pdict', ...] |
|
892 | buffers = ['pdict', ...] | |
893 |
|
893 | |||
894 |
|
894 | |||
895 | The interpretation of a sequence of data_pub messages for a given parent request should be |
|
895 | The interpretation of a sequence of data_pub messages for a given parent request should be | |
896 | to update a single namespace with subsequent results. |
|
896 | to update a single namespace with subsequent results. | |
897 |
|
897 | |||
898 | .. note:: |
|
898 | .. note:: | |
899 |
|
899 | |||
900 | No frontends directly handle data_pub messages at this time. |
|
900 | No frontends directly handle data_pub messages at this time. | |
901 | It is currently only used by the client/engines in :mod:`IPython.parallel`, |
|
901 | It is currently only used by the client/engines in :mod:`IPython.parallel`, | |
902 | where engines may publish *data* to the Client, |
|
902 | where engines may publish *data* to the Client, | |
903 | of which the Client can then publish *representations* via ``display_data`` |
|
903 | of which the Client can then publish *representations* via ``display_data`` | |
904 | to various frontends. |
|
904 | to various frontends. | |
905 |
|
905 | |||
906 | Python inputs |
|
906 | Python inputs | |
907 | ------------- |
|
907 | ------------- | |
908 |
|
908 | |||
909 | These messages are the re-broadcast of the ``execute_request``. |
|
909 | These messages are the re-broadcast of the ``execute_request``. | |
910 |
|
910 | |||
911 | Message type: ``pyin``:: |
|
911 | Message type: ``pyin``:: | |
912 |
|
912 | |||
913 | content = { |
|
913 | content = { | |
914 | 'code' : str, # Source code to be executed, one or more lines |
|
914 | 'code' : str, # Source code to be executed, one or more lines | |
915 |
|
915 | |||
916 | # The counter for this execution is also provided so that clients can |
|
916 | # The counter for this execution is also provided so that clients can | |
917 | # display it, since IPython automatically creates variables called _iN |
|
917 | # display it, since IPython automatically creates variables called _iN | |
918 | # (for input prompt In[N]). |
|
918 | # (for input prompt In[N]). | |
919 | 'execution_count' : int |
|
919 | 'execution_count' : int | |
920 | } |
|
920 | } | |
921 |
|
921 | |||
922 | Python outputs |
|
922 | Python outputs | |
923 | -------------- |
|
923 | -------------- | |
924 |
|
924 | |||
925 | When Python produces output from code that has been compiled in with the |
|
925 | When Python produces output from code that has been compiled in with the | |
926 | 'single' flag to :func:`compile`, any expression that produces a value (such as |
|
926 | 'single' flag to :func:`compile`, any expression that produces a value (such as | |
927 | ``1+1``) is passed to ``sys.displayhook``, which is a callable that can do with |
|
927 | ``1+1``) is passed to ``sys.displayhook``, which is a callable that can do with | |
928 | this value whatever it wants. The default behavior of ``sys.displayhook`` in |
|
928 | this value whatever it wants. The default behavior of ``sys.displayhook`` in | |
929 | the Python interactive prompt is to print to ``sys.stdout`` the :func:`repr` of |
|
929 | the Python interactive prompt is to print to ``sys.stdout`` the :func:`repr` of | |
930 | the value as long as it is not ``None`` (which isn't printed at all). In our |
|
930 | the value as long as it is not ``None`` (which isn't printed at all). In our | |
931 | case, the kernel instantiates as ``sys.displayhook`` an object which has |
|
931 | case, the kernel instantiates as ``sys.displayhook`` an object which has | |
932 | similar behavior, but which instead of printing to stdout, broadcasts these |
|
932 | similar behavior, but which instead of printing to stdout, broadcasts these | |
933 | values as ``pyout`` messages for clients to display appropriately. |
|
933 | values as ``pyout`` messages for clients to display appropriately. | |
934 |
|
934 | |||
935 | IPython's displayhook can handle multiple simultaneous formats depending on its |
|
935 | IPython's displayhook can handle multiple simultaneous formats depending on its | |
936 | configuration. The default pretty-printed repr text is always given with the |
|
936 | configuration. The default pretty-printed repr text is always given with the | |
937 | ``data`` entry in this message. Any other formats are provided in the |
|
937 | ``data`` entry in this message. Any other formats are provided in the | |
938 | ``extra_formats`` list. Frontends are free to display any or all of these |
|
938 | ``extra_formats`` list. Frontends are free to display any or all of these | |
939 | according to its capabilities. ``extra_formats`` list contains 3-tuples of an ID |
|
939 | according to its capabilities. ``extra_formats`` list contains 3-tuples of an ID | |
940 | string, a type string, and the data. The ID is unique to the formatter |
|
940 | string, a type string, and the data. The ID is unique to the formatter | |
941 | implementation that created the data. Frontends will typically ignore the ID |
|
941 | implementation that created the data. Frontends will typically ignore the ID | |
942 | unless if it has requested a particular formatter. The type string tells the |
|
942 | unless if it has requested a particular formatter. The type string tells the | |
943 | frontend how to interpret the data. It is often, but not always a MIME type. |
|
943 | frontend how to interpret the data. It is often, but not always a MIME type. | |
944 | Frontends should ignore types that it does not understand. The data itself is |
|
944 | Frontends should ignore types that it does not understand. The data itself is | |
945 | any JSON object and depends on the format. It is often, but not always a string. |
|
945 | any JSON object and depends on the format. It is often, but not always a string. | |
946 |
|
946 | |||
947 | Message type: ``pyout``:: |
|
947 | Message type: ``pyout``:: | |
948 |
|
948 | |||
949 | content = { |
|
949 | content = { | |
950 |
|
950 | |||
951 | # The counter for this execution is also provided so that clients can |
|
951 | # The counter for this execution is also provided so that clients can | |
952 | # display it, since IPython automatically creates variables called _N |
|
952 | # display it, since IPython automatically creates variables called _N | |
953 | # (for prompt N). |
|
953 | # (for prompt N). | |
954 | 'execution_count' : int, |
|
954 | 'execution_count' : int, | |
955 |
|
955 | |||
956 | # data and metadata are identical to a display_data message. |
|
956 | # data and metadata are identical to a display_data message. | |
957 | # the object being displayed is that passed to the display hook, |
|
957 | # the object being displayed is that passed to the display hook, | |
958 | # i.e. the *result* of the execution. |
|
958 | # i.e. the *result* of the execution. | |
959 | 'data' : dict, |
|
959 | 'data' : dict, | |
960 | 'metadata' : dict, |
|
960 | 'metadata' : dict, | |
961 | } |
|
961 | } | |
962 |
|
962 | |||
963 | Python errors |
|
963 | Python errors | |
964 | ------------- |
|
964 | ------------- | |
965 |
|
965 | |||
966 | When an error occurs during code execution |
|
966 | When an error occurs during code execution | |
967 |
|
967 | |||
968 | Message type: ``pyerr``:: |
|
968 | Message type: ``pyerr``:: | |
969 |
|
969 | |||
970 | content = { |
|
970 | content = { | |
971 | # Similar content to the execute_reply messages for the 'error' case, |
|
971 | # Similar content to the execute_reply messages for the 'error' case, | |
972 | # except the 'status' field is omitted. |
|
972 | # except the 'status' field is omitted. | |
973 | } |
|
973 | } | |
974 |
|
974 | |||
975 | Kernel status |
|
975 | Kernel status | |
976 | ------------- |
|
976 | ------------- | |
977 |
|
977 | |||
978 | This message type is used by frontends to monitor the status of the kernel. |
|
978 | This message type is used by frontends to monitor the status of the kernel. | |
979 |
|
979 | |||
980 | Message type: ``status``:: |
|
980 | Message type: ``status``:: | |
981 |
|
981 | |||
982 | content = { |
|
982 | content = { | |
983 | # When the kernel starts to execute code, it will enter the 'busy' |
|
983 | # When the kernel starts to execute code, it will enter the 'busy' | |
984 | # state and when it finishes, it will enter the 'idle' state. |
|
984 | # state and when it finishes, it will enter the 'idle' state. | |
985 | # The kernel will publish state 'starting' exactly once at process startup. |
|
985 | # The kernel will publish state 'starting' exactly once at process startup. | |
986 | execution_state : ('busy', 'idle', 'starting') |
|
986 | execution_state : ('busy', 'idle', 'starting') | |
987 | } |
|
987 | } | |
988 |
|
988 | |||
989 | Clear output |
|
989 | Clear output | |
990 | ------------ |
|
990 | ------------ | |
991 |
|
991 | |||
992 | This message type is used to clear the output that is visible on the frontend. |
|
992 | This message type is used to clear the output that is visible on the frontend. | |
993 |
|
993 | |||
994 | Message type: ``clear_output``:: |
|
994 | Message type: ``clear_output``:: | |
995 |
|
995 | |||
996 | content = { |
|
996 | content = { | |
997 |
|
997 | |||
998 | # Wait to clear the output until new output is available. Clears the |
|
998 | # Wait to clear the output until new output is available. Clears the | |
999 | # existing output immediately before the new output is displayed. |
|
999 | # existing output immediately before the new output is displayed. | |
1000 | # Useful for creating simple animations with minimal flickering. |
|
1000 | # Useful for creating simple animations with minimal flickering. | |
1001 | 'wait' : bool, |
|
1001 | 'wait' : bool, | |
1002 | } |
|
1002 | } | |
1003 |
|
1003 | |||
1004 | Messages on the stdin ROUTER/DEALER sockets |
|
1004 | Messages on the stdin ROUTER/DEALER sockets | |
1005 | =========================================== |
|
1005 | =========================================== | |
1006 |
|
1006 | |||
1007 | This is a socket where the request/reply pattern goes in the opposite direction: |
|
1007 | This is a socket where the request/reply pattern goes in the opposite direction: | |
1008 | from the kernel to a *single* frontend, and its purpose is to allow |
|
1008 | from the kernel to a *single* frontend, and its purpose is to allow | |
1009 | ``raw_input`` and similar operations that read from ``sys.stdin`` on the kernel |
|
1009 | ``raw_input`` and similar operations that read from ``sys.stdin`` on the kernel | |
1010 | to be fulfilled by the client. The request should be made to the frontend that |
|
1010 | to be fulfilled by the client. The request should be made to the frontend that | |
1011 | made the execution request that prompted ``raw_input`` to be called. For now we |
|
1011 | made the execution request that prompted ``raw_input`` to be called. For now we | |
1012 | will keep these messages as simple as possible, since they only mean to convey |
|
1012 | will keep these messages as simple as possible, since they only mean to convey | |
1013 | the ``raw_input(prompt)`` call. |
|
1013 | the ``raw_input(prompt)`` call. | |
1014 |
|
1014 | |||
1015 | Message type: ``input_request``:: |
|
1015 | Message type: ``input_request``:: | |
1016 |
|
1016 | |||
1017 | content = { 'prompt' : str } |
|
1017 | content = { 'prompt' : str } | |
1018 |
|
1018 | |||
1019 | Message type: ``input_reply``:: |
|
1019 | Message type: ``input_reply``:: | |
1020 |
|
1020 | |||
1021 | content = { 'value' : str } |
|
1021 | content = { 'value' : str } | |
1022 |
|
1022 | |||
1023 | .. note:: |
|
1023 | .. note:: | |
1024 |
|
1024 | |||
1025 | The stdin socket of the client is required to have the same zmq IDENTITY |
|
1025 | The stdin socket of the client is required to have the same zmq IDENTITY | |
1026 | as the client's shell socket. |
|
1026 | as the client's shell socket. | |
1027 | Because of this, the ``input_request`` must be sent with the same IDENTITY |
|
1027 | Because of this, the ``input_request`` must be sent with the same IDENTITY | |
1028 | routing prefix as the ``execute_reply`` in order for the frontend to receive |
|
1028 | routing prefix as the ``execute_reply`` in order for the frontend to receive | |
1029 | the message. |
|
1029 | the message. | |
1030 |
|
1030 | |||
1031 | .. note:: |
|
1031 | .. note:: | |
1032 |
|
1032 | |||
1033 | We do not explicitly try to forward the raw ``sys.stdin`` object, because in |
|
1033 | We do not explicitly try to forward the raw ``sys.stdin`` object, because in | |
1034 | practice the kernel should behave like an interactive program. When a |
|
1034 | practice the kernel should behave like an interactive program. When a | |
1035 | program is opened on the console, the keyboard effectively takes over the |
|
1035 | program is opened on the console, the keyboard effectively takes over the | |
1036 | ``stdin`` file descriptor, and it can't be used for raw reading anymore. |
|
1036 | ``stdin`` file descriptor, and it can't be used for raw reading anymore. | |
1037 | Since the IPython kernel effectively behaves like a console program (albeit |
|
1037 | Since the IPython kernel effectively behaves like a console program (albeit | |
1038 | one whose "keyboard" is actually living in a separate process and |
|
1038 | one whose "keyboard" is actually living in a separate process and | |
1039 | transported over the zmq connection), raw ``stdin`` isn't expected to be |
|
1039 | transported over the zmq connection), raw ``stdin`` isn't expected to be | |
1040 | available. |
|
1040 | available. | |
1041 |
|
1041 | |||
1042 |
|
1042 | |||
1043 | Heartbeat for kernels |
|
1043 | Heartbeat for kernels | |
1044 | ===================== |
|
1044 | ===================== | |
1045 |
|
1045 | |||
1046 | Initially we had considered using messages like those above over ZMQ for a |
|
1046 | Initially we had considered using messages like those above over ZMQ for a | |
1047 | kernel 'heartbeat' (a way to detect quickly and reliably whether a kernel is |
|
1047 | kernel 'heartbeat' (a way to detect quickly and reliably whether a kernel is | |
1048 | alive at all, even if it may be busy executing user code). But this has the |
|
1048 | alive at all, even if it may be busy executing user code). But this has the | |
1049 | problem that if the kernel is locked inside extension code, it wouldn't execute |
|
1049 | problem that if the kernel is locked inside extension code, it wouldn't execute | |
1050 | the python heartbeat code. But it turns out that we can implement a basic |
|
1050 | the python heartbeat code. But it turns out that we can implement a basic | |
1051 | heartbeat with pure ZMQ, without using any Python messaging at all. |
|
1051 | heartbeat with pure ZMQ, without using any Python messaging at all. | |
1052 |
|
1052 | |||
1053 | The monitor sends out a single zmq message (right now, it is a str of the |
|
1053 | The monitor sends out a single zmq message (right now, it is a str of the | |
1054 | monitor's lifetime in seconds), and gets the same message right back, prefixed |
|
1054 | monitor's lifetime in seconds), and gets the same message right back, prefixed | |
1055 | with the zmq identity of the DEALER socket in the heartbeat process. This can be |
|
1055 | with the zmq identity of the DEALER socket in the heartbeat process. This can be | |
1056 | a uuid, or even a full message, but there doesn't seem to be a need for packing |
|
1056 | a uuid, or even a full message, but there doesn't seem to be a need for packing | |
1057 | up a message when the sender and receiver are the exact same Python object. |
|
1057 | up a message when the sender and receiver are the exact same Python object. | |
1058 |
|
1058 | |||
1059 | The model is this:: |
|
1059 | The model is this:: | |
1060 |
|
1060 | |||
1061 | monitor.send(str(self.lifetime)) # '1.2345678910' |
|
1061 | monitor.send(str(self.lifetime)) # '1.2345678910' | |
1062 |
|
1062 | |||
1063 | and the monitor receives some number of messages of the form:: |
|
1063 | and the monitor receives some number of messages of the form:: | |
1064 |
|
1064 | |||
1065 | ['uuid-abcd-dead-beef', '1.2345678910'] |
|
1065 | ['uuid-abcd-dead-beef', '1.2345678910'] | |
1066 |
|
1066 | |||
1067 | where the first part is the zmq.IDENTITY of the heart's DEALER on the engine, and |
|
1067 | where the first part is the zmq.IDENTITY of the heart's DEALER on the engine, and | |
1068 | the rest is the message sent by the monitor. No Python code ever has any |
|
1068 | the rest is the message sent by the monitor. No Python code ever has any | |
1069 | access to the message between the monitor's send, and the monitor's recv. |
|
1069 | access to the message between the monitor's send, and the monitor's recv. | |
1070 |
|
1070 | |||
1071 | Custom Messages |
|
1071 | Custom Messages | |
1072 | =============== |
|
1072 | =============== | |
1073 |
|
1073 | |||
1074 | IPython 2.0 adds a messaging system for developers to add their own objects with Frontend |
|
1074 | IPython 2.0 adds a messaging system for developers to add their own objects with Frontend | |
1075 | and Kernel-side components, and allow them to communicate with each other. |
|
1075 | and Kernel-side components, and allow them to communicate with each other. | |
1076 | To do this, IPython adds a notion of a ``Comm``, which exists on both sides, |
|
1076 | To do this, IPython adds a notion of a ``Comm``, which exists on both sides, | |
1077 | and can communicate in either direction. |
|
1077 | and can communicate in either direction. | |
1078 |
|
1078 | |||
1079 | These messages are fully symmetrical - both the Kernel and the Frontend can send each message, |
|
1079 | These messages are fully symmetrical - both the Kernel and the Frontend can send each message, | |
1080 | and no messages expect a reply. |
|
1080 | and no messages expect a reply. | |
1081 | The Kernel listens for these messages on the Shell channel, |
|
1081 | The Kernel listens for these messages on the Shell channel, | |
1082 | and the Frontend listens for them on the IOPub channel. |
|
1082 | and the Frontend listens for them on the IOPub channel. | |
1083 |
|
1083 | |||
1084 | .. versionadded:: 2.0 |
|
1084 | .. versionadded:: 2.0 | |
1085 |
|
1085 | |||
1086 | Opening a Comm |
|
1086 | Opening a Comm | |
1087 | -------------- |
|
1087 | -------------- | |
1088 |
|
1088 | |||
1089 | Opening a Comm produces a ``comm_open`` message, to be sent to the other side:: |
|
1089 | Opening a Comm produces a ``comm_open`` message, to be sent to the other side:: | |
1090 |
|
1090 | |||
1091 | { |
|
1091 | { | |
1092 | 'comm_id' : 'u-u-i-d', |
|
1092 | 'comm_id' : 'u-u-i-d', | |
1093 | 'target_name' : 'my_comm', |
|
1093 | 'target_name' : 'my_comm', | |
1094 | 'data' : {} |
|
1094 | 'data' : {} | |
1095 | } |
|
1095 | } | |
1096 |
|
1096 | |||
1097 | Every Comm has an ID and a target name. |
|
1097 | Every Comm has an ID and a target name. | |
1098 | The code handling the message on the receiving side is responsible for maintaining a mapping |
|
1098 | The code handling the message on the receiving side is responsible for maintaining a mapping | |
1099 | of target_name keys to constructors. |
|
1099 | of target_name keys to constructors. | |
1100 | After a ``comm_open`` message has been sent, |
|
1100 | After a ``comm_open`` message has been sent, | |
1101 | there should be a corresponding Comm instance on both sides. |
|
1101 | there should be a corresponding Comm instance on both sides. | |
1102 | The ``data`` key is always a dict and can be any extra JSON information used in initialization of the comm. |
|
1102 | The ``data`` key is always a dict and can be any extra JSON information used in initialization of the comm. | |
1103 |
|
1103 | |||
1104 | If the ``target_name`` key is not found on the receiving side, |
|
1104 | If the ``target_name`` key is not found on the receiving side, | |
1105 | then it should immediately reply with a ``comm_close`` message to avoid an inconsistent state. |
|
1105 | then it should immediately reply with a ``comm_close`` message to avoid an inconsistent state. | |
1106 |
|
1106 | |||
1107 | Comm Messages |
|
1107 | Comm Messages | |
1108 | ------------- |
|
1108 | ------------- | |
1109 |
|
1109 | |||
1110 | Comm messages are one-way communications to update comm state, |
|
1110 | Comm messages are one-way communications to update comm state, | |
1111 | used for synchronizing widget state, or simply requesting actions of a comm's counterpart. |
|
1111 | used for synchronizing widget state, or simply requesting actions of a comm's counterpart. | |
1112 |
|
1112 | |||
1113 | Essentially, each comm pair defines their own message specification implemented inside the ``data`` dict. |
|
1113 | Essentially, each comm pair defines their own message specification implemented inside the ``data`` dict. | |
1114 |
|
1114 | |||
1115 | There are no expected replies (of course, one side can send another ``comm_msg`` in reply). |
|
1115 | There are no expected replies (of course, one side can send another ``comm_msg`` in reply). | |
1116 |
|
1116 | |||
1117 | Message type: ``comm_msg``:: |
|
1117 | Message type: ``comm_msg``:: | |
1118 |
|
1118 | |||
1119 | { |
|
1119 | { | |
1120 | 'comm_id' : 'u-u-i-d', |
|
1120 | 'comm_id' : 'u-u-i-d', | |
1121 | 'data' : {} |
|
1121 | 'data' : {} | |
1122 | } |
|
1122 | } | |
1123 |
|
1123 | |||
1124 | Tearing Down Comms |
|
1124 | Tearing Down Comms | |
1125 | ------------------ |
|
1125 | ------------------ | |
1126 |
|
1126 | |||
1127 | Since comms live on both sides, when a comm is destroyed the other side must be notified. |
|
1127 | Since comms live on both sides, when a comm is destroyed the other side must be notified. | |
1128 | This is done with a ``comm_close`` message. |
|
1128 | This is done with a ``comm_close`` message. | |
1129 |
|
1129 | |||
1130 | Message type: ``comm_close``:: |
|
1130 | Message type: ``comm_close``:: | |
1131 |
|
1131 | |||
1132 | { |
|
1132 | { | |
1133 | 'comm_id' : 'u-u-i-d', |
|
1133 | 'comm_id' : 'u-u-i-d', | |
1134 | 'data' : {} |
|
1134 | 'data' : {} | |
1135 | } |
|
1135 | } | |
1136 |
|
1136 | |||
1137 | Output Side Effects |
|
1137 | Output Side Effects | |
1138 | ------------------- |
|
1138 | ------------------- | |
1139 |
|
1139 | |||
1140 | Since comm messages can execute arbitrary user code, |
|
1140 | Since comm messages can execute arbitrary user code, | |
1141 | handlers should set the parent header and publish status busy / idle, |
|
1141 | handlers should set the parent header and publish status busy / idle, | |
1142 | just like an execute request. |
|
1142 | just like an execute request. | |
1143 |
|
1143 | |||
1144 |
|
1144 | |||
1145 | ToDo |
|
1145 | ToDo | |
1146 | ==== |
|
1146 | ==== | |
1147 |
|
1147 | |||
1148 | Missing things include: |
|
1148 | Missing things include: | |
1149 |
|
1149 | |||
1150 | * Important: finish thinking through the payload concept and API. |
|
1150 | * Important: finish thinking through the payload concept and API. | |
1151 |
|
1151 | |||
1152 | * Important: ensure that we have a good solution for magics like %edit. It's |
|
1152 | * Important: ensure that we have a good solution for magics like %edit. It's | |
1153 | likely that with the payload concept we can build a full solution, but not |
|
1153 | likely that with the payload concept we can build a full solution, but not | |
1154 | 100% clear yet. |
|
1154 | 100% clear yet. | |
1155 |
|
1155 | |||
1156 | .. include:: ../links.txt |
|
1156 | .. include:: ../links.txt |
General Comments 0
You need to be logged in to leave comments.
Login now