IPython Magic Commands in Jupyter Notebook (2026) - Part 4 | 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. %prun¶
In Jupyter Notebook, %prun is a magic command used to profile Python code execution.
It helps identify slow functions and performance bottlenecks.
Useful for optimization and debugging performance issues.
%prun sum(range(100000))
4 function calls in 0.014 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.014 0.014 0.014 0.014 {built-in method builtins.sum}
1 0.000 0.000 0.014 0.014 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
1 0.000 0.000 0.014 0.014 <string>:1(<module>)
2. %load_ext¶
In Jupyter Notebook, %load_ext is used to load IPython extensions.
Extensions add extra functionality to notebooks.
Commonly used for autoreload and profiling tools.
%load_ext autoreload
3. %autoreload¶
In Jupyter Notebook, %autoreload automatically reloads imported modules.
Useful while editing external Python files during development.
Prevents restarting the notebook repeatedly.
%autoreload 2
%%writefile helper.py
def greet():
print("Version 1")
Writing helper.py
import helper
helper.greet()
Version 2
4. %reload_ext¶
In Jupyter Notebook, %reload_ext reloads an already loaded extension.
Useful after modifying extension behavior or settings.
Helps refresh notebook extensions without restarting kernel.
%reload_ext autoreload
5. %sx¶
In Jupyter Notebook, %sx executes shell commands and stores output.
The output is returned as a Python list.
Useful for interacting with the operating system.
files = %sx dir
files
[' Volume in drive C is Windows-SSD', ' Volume Serial Number is 565A-BBE5', '', ' Directory of C:\\Users\\schol\\Projects\\Magic-Commands', '', '05/30/2026 01:23 <DIR> .', '05/25/2026 16:01 <DIR> ..', '05/30/2026 01:23 <DIR> .ipynb_checkpoints', '05/30/2026 00:55 16 demo.py', '05/30/2026 01:23 38 helper.py', '05/26/2026 17:07 39,584 Magic-Commands-1.ipynb', '05/27/2026 15:41 171,122 Magic-Commands-2.ipynb', '05/29/2026 20:29 20,443 Magic-Commands-3.ipynb', '05/30/2026 01:22 9,567 Magic-Commands-4.ipynb', '05/26/2026 15:46 8,905 Magic-Commands-5.ipynb', '05/29/2026 02:14 266 myscript.py', '05/29/2026 23:32 31 script.py', '05/26/2026 17:07 <DIR> Test', '05/30/2026 01:23 <DIR> __pycache__', ' 9 File(s) 249,972 bytes', ' 5 Dir(s) 137,942,614,016 bytes free']
6. %%writefile¶
In Jupyter Notebook, %%writefile writes cell contents into a file.
Useful for creating Python scripts directly from notebooks.
Can overwrite existing files.
%%writefile demo.py
print("Hello")
Writing demo.py
7. %%bash¶
In Jupyter Notebook, %%bash runs the entire cell as a Bash script.
On Windows systems, %%bash is usually not supported because Bash is not installed by default.
Instead, Windows uses Command Prompt (cmd) or PowerShell for shell commands.
In Jupyter on Windows, use ! for executing system commands instead of %%bash.
!echo Hello
!cd
Hello C:\Users\schol\Projects\Magic-Commands
8. %%html¶
In Jupyter Notebook, %%html renders HTML content directly in the notebook.
Useful for displaying formatted text and web elements.
Supports HTML, CSS, and simple webpage structures.
%%html
<h1>Hello Jupyter</h1>
Hello Jupyter
9. %%capture¶
In Jupyter Notebook, %%capture captures cell output silently.
Useful for hiding unnecessary outputs or logs.
Can store output into variables for later use.
%%capture output
print("Hidden Output")
output.stdout
'Hidden Output\n'
10. %%latex¶
In Jupyter Notebook, %%latex renders LaTeX equations and expressions.
Useful for mathematical documentation and formulas.
Supports scientific notation and equation formatting.
%%latex
\[
\frac{a}{b}
\]
%%latex
$E = mc^2$
Comments
Post a Comment