##// END OF EJS Templates
Merge pull request #11542 from Carreau/local-scope...
Matthias Bussonnier -
r24967:f0f6cd8b merge
parent child Browse files
Show More
@@ -2343,8 +2343,13 b' class InteractiveShell(SingletonConfigurable):'
2343 2343 magic_arg_s = line
2344 2344 else:
2345 2345 magic_arg_s = self.var_expand(line, stack_depth)
2346 kwargs = {}
2347 if getattr(fn, "needs_local_scope", False):
2348 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
2349
2346 2350 with self.builtin_trap:
2347 result = fn(magic_arg_s, cell)
2351 args = (magic_arg_s, cell)
2352 result = fn(*args, **kwargs)
2348 2353 return result
2349 2354
2350 2355 def find_line_magic(self, magic_name):
@@ -134,6 +134,18 b' instantiate the class yourself before registration:'
134 134 :func:`define_magic` function are advised to adjust their code
135 135 for the current API.
136 136
137
138 Accessing user namespace and local scope
139 ========================================
140
141 When creating line magics, you may need to access surrounding scope to get user
142 variables (e.g when called inside functions). IPython provide the
143 ``@needs_local_scope`` decorator that can be imported from
144 ``IPython.core.magics``. When decorated with ``@needs_local_scope`` a magic will
145 be passed ``local_ns`` as an argument. As a convenience ``@needs_local_scope``
146 can also be applied to cell magics even if cell magics cannot appear at local
147 scope context.
148
137 149 Complete Example
138 150 ================
139 151
General Comments 0
You need to be logged in to leave comments. Login now