Magic commands¶
Line magics¶
Cell magics¶
-
%%inactive
¶ Magic to not execute a cell.
This magic can be used to mark a cell (temporary) as inactive.
Examples:
In [1]: %load_ext ipyext.inactive 'inactive' magic loaded. In [2]: %%inactive ...: print("code not run...") ...: Cell inactive: not executed!
-
%%writeandexecute
¶ Writes the content of the cell to a file and then executes it.
This magic can be used to write the content of a cell to a .py file and afterwards execute the cell. This enables to code reuse.
Code is replaced on the next execution (using the needed identifier) and other code can be appended by using the same file name.
Parameters
- -i <identifier> : str
- Surrounds the code written to the file with a line containing the identifier. The use of an identifier enables you to easily overwrite a given code section in the output file.
- <filename> : str
- The file to which the code should be written. Can be
specified without an extension and can also include a directory
(
dir/file
) - -d : (optional)
- Write some debugging output. Default: – (no debugging output)
Examples:
In [1]: %load_ext ipyext.writeandexecute 'writeandexecute' magic loaded. In [2]: %%writeandexecute -i my_code_block functions.py ...: print "Hello world" ...: Hello world
In addition to executing (and printing “Hello world”), it would also create a file “functions.py” with the following content
# -*- coding: utf-8 -*- # -- ==my_code_block== -- print "Hello world" # -- ==my_code_block== --
Cell content is transformed, so
%magic
commands are executed, butget_ipython()
must be available, i.e. the code must be executed in an IPython session.