Jupyter Extensions
MINEO supports a range of Jupyter extensions, offering enhanced functionality and flexibility within your notebooks. Here, we'll cover some commonly used extensions, including autoreload
, timeit
, memory_profiler
, and line_profiler
, each adding valuable capabilities to your development workflow.
Autoreload
The autoreload
extension is especially useful for reloading modules automatically when they are modified, without restarting the kernel. This helps streamline development and testing by allowing you to see updates to code immediately.
Enabling Autoreload
To enable autoreload
, add the following commands at the beginning of your notebook:
# Load the autoreload extension
%load_ext autoreload
# Set autoreload to automatically reload all modules before executing a new line
%autoreload 2
Setting %autoreload 2
ensures all imported modules are reloaded before each execution.
Example of Autoreload in Action
Suppose you have a module called mymodule.py
with a greet()
function:
# mymodule.py
def greet():
return "Hello, world!"
In your notebook, you can import and call the greet()
function:
# Load the autoreload extension
%load_ext autoreload
# Set autoreload to automatically reload all modules before executing a new line
%autoreload 2
from mymodule import greet
print(greet()) # Output: Hello, world!
Now, if you update the greet()
function in mymodule.py
to return "Hello, MINEO!" instead, the autoreload
extension will automatically reload the module, and the output of the print(greet())
call will reflect the updated function:
# mymodule.py
def greet():
return "Hello, MINEO!"
from mymodule import greet
print(greet()) # Output: Hello, MINEO!
With %autoreload 2
set, you don't need to restart the kernel to see the changes made to the module. This helps streamline your development and testing workflow.
Timeit
The timeit
extension provides a simple way to measure the execution time of code snippets, helping you identify and optimize slower code.
Using Timeit
To use timeit
, load it in your notebook:
%load_ext timeit
You can time a single line of code as follows:
%timeit sum(range(1000))
Or use %%timeit
to time a multi-line code block:
%%timeit
total = 0
for i in range(1000):
total += i
Memory Profiler
memory_profiler
helps you track memory usage across lines of code, which is essential for memory-sensitive applications.
Using Memory Profiler
First, install the memory_profiler
package, then enable it:
!pip install memory-profiler
%load_ext memory_profiler
Use %%memit
to measure memory usage for a single line or block:
%%memit
x = [i for i in range(100000)]