IPython Magic Commands in Jupyter Notebook - Part 2 | Step-by-Step Guide
Magic Commands¶
In Jupyter Notebook, Magic Commands are special built-in commands that provide additional functionality beyond normal Python commands.
They help users perform tasks such as file handling, timing code execution, managing variables, and controlling the notebook environment more easily.
Magic commands usually start with % for line magics and %% for cell magics.
They are designed to make working in Jupyter Notebook faster and more interactive.
1. %lsmagic¶
In Jupyter Notebook, %lsmagic is a magic command used to list all available magic commands.
It displays both line magics (%) and cell magics (%%).
It helps users discover supported magic commands in the notebook.
%lsmagic
Available line magics: %alias %alias_magic %autoawait %autocall %automagic %autosave %bookmark %cd %clear %cls %code_wrap %colors %conda %config %connect_info %copy %ddir %debug %dhist %dirs %doctest_mode %echo %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %macro %magic %mamba %matplotlib %micromamba %mkdir %more %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %pip %popd %pprint %precision %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %ren %rep %rerun %reset %reset_selective %rmdir %run %save %sc %set_env %store %subshell %sx %system %tb %time %timeit %unalias %unload_ext %uv %who %who_ls %whos %xdel %xmode Available cell magics: %%! %%HTML %%SVG %%bash %%capture %%cmd %%code_wrap %%debug %%file %%html %%javascript %%js %%latex %%markdown %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile Automagic is ON, % prefix IS NOT needed for line magics.
2. %magic¶
In Jupyter Notebook, %magic is a magic command used to display detailed help about magic commands.
It explains how line magics and cell magics work.
It is useful for beginners learning notebook commands.
%magic
3. %quickref¶
In Jupyter Notebook, %quickref is a magic command used to show a quick reference guide for IPython commands.
It provides shortcuts and commonly used commands.
It helps users quickly remember syntax.
%quickref
4. %pinfo¶
In Jupyter Notebook, %pinfo is a magic command used to display information about an object or function.
It shows details such as type and documentation.
It is useful for understanding Python functions.
%pinfo len
Signature: len(obj, /) Docstring: Return the number of items in a container. Type: builtin_function_or_method
5. %pinfo2¶
In Jupyter Notebook, %pinfo2 is a magic command used to display detailed information about an object.
It provides more details than %pinfo.
It is useful for advanced inspection of functions and variables.
%pinfo2 list
Init signature: list(iterable=(), /) Docstring: Built-in mutable sequence. If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified. Type: type Subclasses: _List, _HashedSeq, StackSummary, _Threads, ConvertingList, DeferredConfigList, _ymd, _Accumulator, SList, _ImmutableLineList, ...
6. %automagic¶
In Jupyter Notebook, %automagic is a magic command used to enable or disable automatic magic commands.
When enabled, magic commands can be used without %.
It makes command usage more convenient.
%automagic
Automagic is OFF, % prefix IS needed for line magics.
7. %config¶
In Jupyter Notebook, %config is a magic command used to view or change IPython configuration settings.
It helps customize notebook behavior.
It is useful for advanced notebook configuration.
%config
Available objects for config:
AliasManager
DisplayFormatter
HistoryManager
IPCompleter
IPKernelApp
LoggingMagics
MagicsManager
OSMagics
PrefilterManager
ScriptMagics
StoreMagics
ZMQDisplayPublisher
ZMQInteractiveShell
8. %page¶
In Jupyter Notebook, %page is a magic command used to display long output in a scrollable pager view.
It improves readability of large text outputs.
It is useful for viewing documentation or large datasets.
numbers = list(range(50))
%page numbers
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
9. %bookmark¶
In Jupyter Notebook, %bookmark is a magic command used to save frequently used directory paths.
It allows quick navigation to saved folders.
It helps users manage project locations efficiently.
%bookmark project C:\Users\schol\Projects\Magic-Commands
%bookmark -l
Current bookmarks: project -> C:\Users\schol\Projects\Magic-Commands
%pwd
'C:\\Users\\schol\\Projects\\Magic-Commands'
%cd Test
C:\Users\schol\Projects\Magic-Commands\Test
%pwd
'C:\\Users\\schol\\Projects\\Magic-Commands\\Test'
%cd -b project
(bookmark:project) -> C:\Users\schol\Projects\Magic-Commands C:\Users\schol\Projects\Magic-Commands
%pwd
'C:\\Users\\schol\\Projects\\Magic-Commands'
10. %reset¶
In Jupyter Notebook, %reset is a magic command used to remove all user-defined variables from memory.
It clears the notebook workspace.
It is useful for starting with a clean environment.
data=1
%who
data numbers
%reset
%who
Interactive namespace is empty.
Comments
Post a Comment