MCPcopy Create free account
hub / github.com/ipython/ipython / register_function

Method register_function

IPython/core/magic.py:413–445  ·  view source on GitHub ↗

Expose a standalone function as magic function for IPython. This will create an IPython magic (line, cell or both) from a standalone function. The functions should have the following signatures: * For line magics: `def f(line)` * For cell magics: `def f(li

(self, func, magic_kind='line', magic_name=None)

Source from the content-addressed store, hash-verified

411 self.magics[mtype].update(m.magics[mtype])
412
413 def register_function(self, func, magic_kind='line', magic_name=None):
414 """Expose a standalone function as magic function for IPython.
415
416 This will create an IPython magic (line, cell or both) from a
417 standalone function. The functions should have the following
418 signatures:
419
420 * For line magics: `def f(line)`
421 * For cell magics: `def f(line, cell)`
422 * For a function that does both: `def f(line, cell=None)`
423
424 In the latter case, the function will be called with `cell==None` when
425 invoked as `%f`, and with cell as a string when invoked as `%%f`.
426
427 Parameters
428 ----------
429 func : callable
430 Function to be registered as a magic.
431
432 magic_kind : str
433 Kind of magic, one of 'line', 'cell' or 'line_cell'
434
435 magic_name : optional str
436 If given, the name the magic will have in the IPython namespace. By
437 default, the name of the function itself is used.
438 """
439
440 # Create the new method in the user_magics and register it in the
441 # global table
442 validate_type(magic_kind)
443 magic_name = func.__name__ if magic_name is None else magic_name
444 setattr(self.user_magics, magic_name, func)
445 record_magic(self.magics, magic_kind, magic_name, func)
446
447 def register_alias(self, alias_name, magic_name, magic_kind='line', magic_params=None):
448 """Register an alias to a magic function.

Callers 2

define_aliasMethod · 0.80

Calls 2

validate_typeFunction · 0.85
record_magicFunction · 0.85

Tested by

no test coverage detected