##// END OF EJS Templates
Backport PR #13600: Allow to get cellid from ipykernel
Matthias Bussonnier -
Show More
@@ -41,7 +41,7 b' or using unicode completion:'
41
41
42 Only valid Python identifiers will complete. Combining characters (like arrow or
42 Only valid Python identifiers will complete. Combining characters (like arrow or
43 dots) are also available, unlike latex they need to be put after the their
43 dots) are also available, unlike latex they need to be put after the their
44 counterpart that is to say, `F\\\\vec<tab>` is correct, not `\\\\vec<tab>F`.
44 counterpart that is to say, ``F\\\\vec<tab>`` is correct, not ``\\\\vec<tab>F``.
45
45
46 Some browsers are known to display combining characters incorrectly.
46 Some browsers are known to display combining characters incorrectly.
47
47
@@ -297,19 +297,29 b' class ExecutionInfo(object):'
297 store_history = False
297 store_history = False
298 silent = False
298 silent = False
299 shell_futures = True
299 shell_futures = True
300 cell_id = None
300
301
301 def __init__(self, raw_cell, store_history, silent, shell_futures):
302 def __init__(self, raw_cell, store_history, silent, shell_futures, cell_id):
302 self.raw_cell = raw_cell
303 self.raw_cell = raw_cell
303 self.store_history = store_history
304 self.store_history = store_history
304 self.silent = silent
305 self.silent = silent
305 self.shell_futures = shell_futures
306 self.shell_futures = shell_futures
307 self.cell_id = cell_id
306
308
307 def __repr__(self):
309 def __repr__(self):
308 name = self.__class__.__qualname__
310 name = self.__class__.__qualname__
309 raw_cell = ((self.raw_cell[:50] + '..')
311 raw_cell = (
310 if len(self.raw_cell) > 50 else self.raw_cell)
312 (self.raw_cell[:50] + "..") if len(self.raw_cell) > 50 else self.raw_cell
311 return '<%s object at %x, raw_cell="%s" store_history=%s silent=%s shell_futures=%s>' %\
313 )
312 (name, id(self), raw_cell, self.store_history, self.silent, self.shell_futures)
314 return '<%s object at %x, raw_cell="%s" store_history=%s silent=%s shell_futures=%s cell_id=%s>' % (
315 name,
316 id(self),
317 raw_cell,
318 self.store_history,
319 self.silent,
320 self.shell_futures,
321 self.cell_id,
322 )
313
323
314
324
315 class ExecutionResult(object):
325 class ExecutionResult(object):
@@ -2928,7 +2938,14 b' class InteractiveShell(SingletonConfigurable):'
2928 self.showtraceback()
2938 self.showtraceback()
2929 warn('Unknown failure executing module: <%s>' % mod_name)
2939 warn('Unknown failure executing module: <%s>' % mod_name)
2930
2940
2931 def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True):
2941 def run_cell(
2942 self,
2943 raw_cell,
2944 store_history=False,
2945 silent=False,
2946 shell_futures=True,
2947 cell_id=None,
2948 ):
2932 """Run a complete IPython cell.
2949 """Run a complete IPython cell.
2933
2950
2934 Parameters
2951 Parameters
@@ -2955,14 +2972,22 b' class InteractiveShell(SingletonConfigurable):'
2955 result = None
2972 result = None
2956 try:
2973 try:
2957 result = self._run_cell(
2974 result = self._run_cell(
2958 raw_cell, store_history, silent, shell_futures)
2975 raw_cell, store_history, silent, shell_futures, cell_id
2976 )
2959 finally:
2977 finally:
2960 self.events.trigger('post_execute')
2978 self.events.trigger('post_execute')
2961 if not silent:
2979 if not silent:
2962 self.events.trigger('post_run_cell', result)
2980 self.events.trigger('post_run_cell', result)
2963 return result
2981 return result
2964
2982
2965 def _run_cell(self, raw_cell:str, store_history:bool, silent:bool, shell_futures:bool):
2983 def _run_cell(
2984 self,
2985 raw_cell: str,
2986 store_history: bool,
2987 silent: bool,
2988 shell_futures: bool,
2989 cell_id: str,
2990 ) -> ExecutionResult:
2966 """Internal method to run a complete IPython cell."""
2991 """Internal method to run a complete IPython cell."""
2967
2992
2968 # we need to avoid calling self.transform_cell multiple time on the same thing
2993 # we need to avoid calling self.transform_cell multiple time on the same thing
@@ -2982,6 +3007,7 b' class InteractiveShell(SingletonConfigurable):'
2982 shell_futures=shell_futures,
3007 shell_futures=shell_futures,
2983 transformed_cell=transformed_cell,
3008 transformed_cell=transformed_cell,
2984 preprocessing_exc_tuple=preprocessing_exc_tuple,
3009 preprocessing_exc_tuple=preprocessing_exc_tuple,
3010 cell_id=cell_id,
2985 )
3011 )
2986
3012
2987 # run_cell_async is async, but may not actually need an eventloop.
3013 # run_cell_async is async, but may not actually need an eventloop.
@@ -3002,7 +3028,9 b' class InteractiveShell(SingletonConfigurable):'
3002 try:
3028 try:
3003 return runner(coro)
3029 return runner(coro)
3004 except BaseException as e:
3030 except BaseException as e:
3005 info = ExecutionInfo(raw_cell, store_history, silent, shell_futures)
3031 info = ExecutionInfo(
3032 raw_cell, store_history, silent, shell_futures, cell_id
3033 )
3006 result = ExecutionResult(info)
3034 result = ExecutionResult(info)
3007 result.error_in_exec = e
3035 result.error_in_exec = e
3008 self.showtraceback(running_compiled_code=True)
3036 self.showtraceback(running_compiled_code=True)
@@ -3060,7 +3088,8 b' class InteractiveShell(SingletonConfigurable):'
3060 shell_futures=True,
3088 shell_futures=True,
3061 *,
3089 *,
3062 transformed_cell: Optional[str] = None,
3090 transformed_cell: Optional[str] = None,
3063 preprocessing_exc_tuple: Optional[Any] = None
3091 preprocessing_exc_tuple: Optional[Any] = None,
3092 cell_id=None,
3064 ) -> ExecutionResult:
3093 ) -> ExecutionResult:
3065 """Run a complete IPython cell asynchronously.
3094 """Run a complete IPython cell asynchronously.
3066
3095
@@ -3091,8 +3120,7 b' class InteractiveShell(SingletonConfigurable):'
3091
3120
3092 .. versionadded:: 7.0
3121 .. versionadded:: 7.0
3093 """
3122 """
3094 info = ExecutionInfo(
3123 info = ExecutionInfo(raw_cell, store_history, silent, shell_futures, cell_id)
3095 raw_cell, store_history, silent, shell_futures)
3096 result = ExecutionResult(info)
3124 result = ExecutionResult(info)
3097
3125
3098 if (not raw_cell) or raw_cell.isspace():
3126 if (not raw_cell) or raw_cell.isspace():
@@ -1,7 +1,7 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 The :class:`~IPython.core.application.Application` object for the command
4 The :class:`~traitlets.config.application.Application` object for the command
5 line :command:`ipython` program.
5 line :command:`ipython` program.
6 """
6 """
7
7
@@ -17,22 +17,29 b' For example::'
17 def __init__(self, ip):
17 def __init__(self, ip):
18 self.shell = ip
18 self.shell = ip
19 self.last_x = None
19 self.last_x = None
20
20
21 def pre_execute(self):
21 def pre_execute(self):
22 self.last_x = self.shell.user_ns.get('x', None)
22 self.last_x = self.shell.user_ns.get('x', None)
23
23
24 def pre_run_cell(self, info):
24 def pre_run_cell(self, info):
25 print('Cell code: "%s"' % info.raw_cell)
25 print('info.raw_cell =', info.raw_cell)
26
26 print('info.store_history =', info.store_history)
27 print('info.silent =', info.silent)
28 print('info.shell_futures =', info.shell_futures)
29 print('info.cell_id =', info.cell_id)
30 print(dir(info))
31
27 def post_execute(self):
32 def post_execute(self):
28 if self.shell.user_ns.get('x', None) != self.last_x:
33 if self.shell.user_ns.get('x', None) != self.last_x:
29 print("x changed!")
34 print("x changed!")
30
35
31 def post_run_cell(self, result):
36 def post_run_cell(self, result):
32 print('Cell code: "%s"' % result.info.raw_cell)
37 print('result.execution_count = ', result.execution_count)
33 if result.error_before_exec:
38 print('result.error_before_exec = ', result.error_before_exec)
34 print('Error before execution: %s' % result.error_before_exec)
39 print('result.error_in_exec = ', result.error_in_exec)
35
40 print('result.info = ', result.info)
41 print('result.result = ', result.result)
42
36 def load_ipython_extension(ip):
43 def load_ipython_extension(ip):
37 vw = VarWatcher(ip)
44 vw = VarWatcher(ip)
38 ip.events.register('pre_execute', vw.pre_execute)
45 ip.events.register('pre_execute', vw.pre_execute)
@@ -40,6 +47,13 b' For example::'
40 ip.events.register('post_execute', vw.post_execute)
47 ip.events.register('post_execute', vw.post_execute)
41 ip.events.register('post_run_cell', vw.post_run_cell)
48 ip.events.register('post_run_cell', vw.post_run_cell)
42
49
50 .. versionadded:: 8.3
51
52 Since IPython 8.3 and ipykernel 6.12.1, the ``info`` objects in the callback
53 now have a the ``cell_id`` that will be set to the value sent by the
54 frontened, when those send it.
55
56
43
57
44 Events
58 Events
45 ======
59 ======
General Comments 0
You need to be logged in to leave comments. Login now