Kernels & Magic Commands
Kernels & Magic Commands
Section titled “Kernels & Magic Commands”Kernels
Section titled “Kernels”A kernel is the computational engine behind a notebook. Each notebook has exactly one kernel running.
Managing Kernels
Section titled “Managing Kernels”Kernel menu → Restart # clears all variables, fresh stateKernel menu → Interrupt # stops running cell (like Ctrl+C)Kernel menu → Restart & Run All # clean run of entire notebookInstalling Additional Kernels
Section titled “Installing Additional Kernels”# R kernelinstall.packages('IRkernel')IRkernel::installspec()
# Julia kernel# In Julia REPL:using Pkg; Pkg.add("IJulia")
# .NET Interactive (C#, F#)dotnet tool install -g Microsoft.dotnet-interactivedotnet interactive jupyter install
# Bash kernelpip install bash_kernelpython -m bash_kernel.install
# List installed kernelsjupyter kernelspec listKernel State Pitfall
Section titled “Kernel State Pitfall”# Cell 1 (run first)x = 10
# Cell 2 (run second)x = 20
# Cell 3 (run third)print(x) # 20
# If you re-run Cell 1 then Cell 3 without Cell 2:print(x) # still 20 — kernel remembers last assignmentAlways use Restart & Run All before sharing a notebook to ensure reproducibility.
Magic Commands
Section titled “Magic Commands”Magic commands are IPython extensions prefixed with % (line magic) or %% (cell magic).
Line Magics (%)
Section titled “Line Magics (%)”# Timing%time sum(range(1_000_000))# CPU times: user 12.3 ms, sys: 0 ns, total: 12.3 ms
%timeit sum(range(1_000_000))# 12.1 ms ± 234 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
# Current directory and file system%pwd # print working directory%ls # list files%cd /path # change directory
# Variable inspection%who # list all variables%whos # detailed variable info (name, type, value)%reset # delete all variables
# History%history # show command history%history -n 10 # last 10 commands
# Load/save%load script.py # load file content into cell%run script.py # run a Python file%save output.py 1-5 # save cells 1-5 to file
# Environment%env # show environment variables%env MY_VAR=hello # set environment variable
# Matplotlib%matplotlib inline # render plots inline%matplotlib widget # interactive plots (requires ipympl)Cell Magics (%%)
Section titled “Cell Magics (%%)”%%time# Times the entire cellimport timetime.sleep(0.1)result = sum(range(10_000_000))
%%timeit# Benchmarks the entire cellresult = [x**2 for x in range(1000)]
%%bash# Run bash commands in the cellecho "Hello from bash"ls -lapip list | grep numpy
%%html<h1 style="color: blue">Hello HTML</h1><p>This renders as HTML in the output</p>
%%javascriptconsole.log("Hello from JS");element.textContent = "Rendered in notebook";
%%writefile mymodule.py# Writes cell content to a filedef greet(name): return f"Hello, {name}!"
%%capture output# Captures all output (stdout, stderr, display)import warningswarnings.warn("This won't show")print("This is captured")# Access via: output.stdout
%%latex\begin{equation}E = mc^2\end{equation}Shell Commands
Section titled “Shell Commands”Prefix with ! to run shell commands directly:
!pip install pandas!git status!ls -la!python --version
# Capture output into Python variablefiles = !ls *.csvprint(files) # Python list of strings
# Pass Python variables to shellname = "myfile"!echo {name}%autoreload — Auto-reload Modules
Section titled “%autoreload — Auto-reload Modules”Essential when developing a module alongside a notebook:
%load_ext autoreload%autoreload 2 # reload all modules before each cell execution
import mymodule# Now edits to mymodule.py are picked up automaticallyProfiling
Section titled “Profiling”%prun my_function() # cProfile — function call stats%lprun -f my_function my_function() # line-by-line (requires line_profiler)%memit my_function() # memory usage (requires memory_profiler)
# Install profilers!pip install line_profiler memory_profiler%load_ext line_profiler%load_ext memory_profiler