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(lin
(
self,
func: Callable[..., Any],
magic_kind: _MagicSpec = "line",
magic_name: str | None = None,
)
| 508 | self.magics[mtype].update(m.magics[mtype]) |
| 509 | |
| 510 | def register_function( |
| 511 | self, |
| 512 | func: Callable[..., Any], |
| 513 | magic_kind: _MagicSpec = "line", |
| 514 | magic_name: str | None = None, |
| 515 | ) -> None: |
| 516 | """Expose a standalone function as magic function for IPython. |
| 517 | |
| 518 | This will create an IPython magic (line, cell or both) from a |
| 519 | standalone function. The functions should have the following |
| 520 | signatures: |
| 521 | |
| 522 | * For line magics: `def f(line)` |
| 523 | * For cell magics: `def f(line, cell)` |
| 524 | * For a function that does both: `def f(line, cell=None)` |
| 525 | |
| 526 | In the latter case, the function will be called with `cell==None` when |
| 527 | invoked as `%f`, and with cell as a string when invoked as `%%f`. |
| 528 | |
| 529 | Parameters |
| 530 | ---------- |
| 531 | func : callable |
| 532 | Function to be registered as a magic. |
| 533 | magic_kind : str |
| 534 | Kind of magic, one of 'line', 'cell' or 'line_cell' |
| 535 | magic_name : optional str |
| 536 | If given, the name the magic will have in the IPython namespace. By |
| 537 | default, the name of the function itself is used. |
| 538 | """ |
| 539 | |
| 540 | # Create the new method in the user_magics and register it in the |
| 541 | # global table |
| 542 | validate_type(magic_kind) |
| 543 | magic_name = func.__name__ if magic_name is None else magic_name |
| 544 | assert self.user_magics is not None |
| 545 | setattr(self.user_magics, magic_name, func) |
| 546 | record_magic(self.magics, magic_kind, magic_name, func) |
| 547 | |
| 548 | def register_alias( |
| 549 | self, |
no test coverage detected