Collection of common python utils for machine learning and scientific computing workflows
—
Utilities specifically designed for Google Colab environments including enhanced display functions, code inspection, HTML rendering, Python-JavaScript communication, and development workflow improvements.
Enhanced display functions for better Colab experience.
def auto_display(obj: Any) -> None:
"""
Automatically display objects with enhanced formatting in Colab.
Args:
obj: Object to display (lists, dicts, images, etc.)
"""
def disp(obj: Any) -> None:
"""
Shorthand for auto_display function.
Args:
obj: Object to display
"""
def auto_plot_array(
array: np.ndarray,
**kwargs
) -> None:
"""
Automatically plot arrays as images when appropriate.
Args:
array: NumPy array to potentially plot as image
**kwargs: Additional plotting arguments
"""Functions for enhancing the Colab interface.
def collapse(content: str, title: str = 'Details') -> None:
"""
Create a collapsible section in Colab output.
Args:
content: Content to display in collapsible section
title: Title for the collapsible section
"""
def get_permalink() -> str:
"""
Get a permalink to the current Colab notebook.
Returns:
Permalink URL string
"""
def interruptible(fn: Callable) -> Callable:
"""
Decorator to make long-running functions interruptible in Colab.
Args:
fn: Function to make interruptible
Returns:
Wrapped function that can be interrupted
"""
def json(obj: Any) -> None:
"""
Display objects as formatted JSON in Colab.
Args:
obj: Object to display as JSON
"""Tools for inspecting and analyzing code in Colab.
def auto_inspect(obj: Any) -> None:
"""
Automatically inspect objects with enhanced Colab display.
Args:
obj: Object to inspect (functions, classes, modules)
"""
def inspect(
obj: Any,
show_source: bool = True,
show_docs: bool = True
) -> None:
"""
Inspect objects with detailed information display.
Args:
obj: Object to inspect
show_source: Whether to show source code
show_docs: Whether to show documentation
"""Enhanced HTML rendering and code highlighting.
def highlight_html(
code: str,
language: str = 'python'
) -> str:
"""
Generate syntax-highlighted HTML for code.
Args:
code: Source code to highlight
language: Programming language for syntax highlighting
Returns:
HTML string with syntax highlighting
"""Tools for configuring the Colab environment.
def patch_graphviz() -> None:
"""
Patch graphviz for better Colab compatibility.
"""
def set_verbose(level: bool | int = True) -> None:
"""
Set verbosity level for various operations.
Args:
level: Verbosity level (True/False or integer)
"""Bridge between Python and JavaScript in Colab.
def pyjs_import(
python_fn: Callable,
name: str | None = None
) -> str:
"""
Make Python function available to JavaScript.
Args:
python_fn: Python function to expose
name: Name to use in JavaScript (defaults to function name)
Returns:
JavaScript code to call the function
"""
def register_js_fn(
js_fn_name: str,
python_handler: Callable
) -> None:
"""
Register JavaScript function to call Python handler.
Args:
js_fn_name: Name of JavaScript function
python_handler: Python function to handle calls
"""Utilities for managing modules and reloading in Colab.
class ReloadMode(Enum):
"""
Enumeration for module reload modes.
"""
NONE = "none"
SOFT = "soft"
HARD = "hard"
def clear_cached_modules() -> None:
"""
Clear cached modules for fresh imports.
"""from etils import ecolab
import numpy as np
# Auto display with enhanced formatting
data = {
'experiment': 'vision_model_v2',
'metrics': {'accuracy': 0.94, 'loss': 0.15},
'config': {'batch_size': 32, 'learning_rate': 0.001}
}
ecolab.auto_display(data) # Pretty formatted display
# Quick display shorthand
results = [1, 2, 3, 4, 5]
ecolab.disp(results)
# Automatic array plotting
image_array = np.random.rand(64, 64, 3) # RGB image
ecolab.auto_plot_array(image_array) # Automatically plots as imagefrom etils import ecolab
# Create collapsible sections
long_output = "\\n".join([f"Line {i}" for i in range(100)])
ecolab.collapse(long_output, "Debug Output")
# Display as formatted JSON
config = {
'model': {'layers': 4, 'units': 128},
'training': {'epochs': 50, 'batch_size': 32}
}
ecolab.json(config)
# Get notebook permalink
permalink = ecolab.get_permalink()
print(f"Share this notebook: {permalink}")from etils import ecolab
import numpy as np
# Inspect functions with enhanced display
ecolab.inspect(np.array, show_source=True, show_docs=True)
# Auto-inspect with smart defaults
def my_function(x, y=10):
"""Example function for demonstration."""
return x + y
ecolab.auto_inspect(my_function)from etils import ecolab
import time
@ecolab.interruptible
def long_training_loop():
"""Training loop that can be interrupted."""
for epoch in range(1000):
# Simulate training
time.sleep(0.1)
if epoch % 100 == 0:
print(f"Epoch {epoch}")
return "Training complete"
# Run the function - can be interrupted with stop button
result = long_training_loop()from etils import ecolab
# Syntax highlighting
python_code = '''
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
'''
highlighted = ecolab.highlight_html(python_code, 'python')
# Display highlighted code in Colab
# Different languages
js_code = '''
function factorial(n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}
'''
highlighted_js = ecolab.highlight_html(js_code, 'javascript')from etils import ecolab
# Expose Python function to JavaScript
def process_data(data):
"""Process data and return result."""
return [x * 2 for x in data]
js_code = ecolab.pyjs_import(process_data, 'processData')
print(js_code) # JavaScript code to call the function
# Register JavaScript callback
def handle_button_click(event_data):
print(f"Button clicked with data: {event_data}")
ecolab.register_js_fn('onButtonClick', handle_button_click)from etils import ecolab
# Configure graphviz for Colab
ecolab.patch_graphviz()
# Set verbosity for debugging
ecolab.set_verbose(True) # Enable verbose output
ecolab.set_verbose(2) # Higher verbosity level
ecolab.set_verbose(False) # Disable verbose output
# Clear module cache for development
ecolab.clear_cached_modules()
# Now reimport your modules to get latest changesfrom etils import ecolab
# Configure reload mode for development
reload_mode = ecolab.ReloadMode.SOFT
# Clear cached modules during development
ecolab.clear_cached_modules()
# Reimport your modules
import my_module # Gets fresh versionfrom etils import ecolab
# Complete development setup
ecolab.patch_graphviz() # Fix graphviz
ecolab.set_verbose(True) # Enable verbose logging
ecolab.clear_cached_modules() # Clear module cache
# Enhanced display for debugging
debug_info = {
'model_state': 'training',
'current_epoch': 42,
'gpu_memory': '8.2GB used / 16GB total'
}
ecolab.auto_display(debug_info)
# Collapsible debug output
debug_logs = "\\n".join([f"DEBUG: Step {i}" for i in range(50)])
ecolab.collapse(debug_logs, "Debug Logs")Install with Tessl CLI
npx tessl i tessl/pypi-etils