CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-etils

Collection of common python utils for machine learning and scientific computing workflows

Pending
Overview
Eval results
Files

colab-integration.mddocs/

Google Colab Integration (ecolab)

Utilities specifically designed for Google Colab environments including enhanced display functions, code inspection, HTML rendering, Python-JavaScript communication, and development workflow improvements.

Capabilities

Display and Visualization

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
    """

Colab Interface Utilities

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
    """

Code Inspection and Analysis

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
    """

HTML and Syntax Highlighting

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
    """

Environment Configuration

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)
    """

Python-JavaScript Communication

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
    """

Module Management

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.
    """

Usage Examples

Enhanced Display

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 image

Interface Enhancements

from 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}")

Code Inspection

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)

Long-Running Operations

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()

HTML and Highlighting

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')

Python-JavaScript Bridge

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)

Environment Configuration

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 changes

Module Reloading

from 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 version

Development Workflow

from 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

docs

application-framework.md

array-types.md

colab-integration.md

dataclass-enhancements.md

index.md

numpy-utilities.md

path-operations.md

python-utilities.md

tree-manipulation.md

tile.json