Skip to main content

Jupyter Notebook Integration

tif1 provides Jupyter notebook integration with rich HTML displays, automatic environment detection, and interactive utilities for Formula 1 data analysis. The library shows formatted displays for Session, Driver, and Lap objects in the notebook.

Overview

The Jupyter integration module (tif1.jupyter) provides:
  • Automatic Environment Detection: Detects when running in Jupyter and enables rich displays automatically
  • Rich HTML Representations: Formatted displays for all major tif1 objects
  • DataFrame Summaries: Enhanced DataFrame displays with memory usage and shape information
  • Zero Configuration: Works out of the box with no setup required
  • Fallback Support: Gracefully degrades to standard repr() when not in Jupyter
  • Performance Optimized: Minimal overhead with lazy loading and efficient HTML generation

Automatic Activation

The Jupyter integration activates automatically when tif1 is imported in a Jupyter environment. The library detects Jupyter notebooks, JupyterLab, and IPython kernels.

How It Works

When tif1 is imported, the library:
  1. Detects the Environment: Checks if IPython is available and if an IPython kernel is running
  2. Enables Rich Displays: Automatically patches Session, Driver, and Lap classes with _repr_html_() methods
  3. Logs Activation: Records activation status in the logger (visible with debug logging enabled)
  4. Handles Errors Gracefully: Falls back to standard text representation if HTML generation fails

Environment Detection Details

The _is_notebook() function performs the following checks:
  • Tries to import the IPython module
  • Retrieves the current IPython instance via get_ipython()
  • Checks for IPKernelApp in the shell configuration (indicates Jupyter kernel)
  • Returns True only if all checks pass, which confirms a genuine Jupyter environment
This approach ensures compatibility with:
  • Jupyter Notebook (classic interface)
  • JupyterLab
  • VS Code Jupyter extension
  • Google Colab
  • Kaggle Notebooks
  • Any IPython kernel-based environment

Rich HTML Displays

All major tif1 objects provide rich HTML representations that display automatically in a Jupyter cell. These displays are styled with borders, colors, and structured tables.

Session Display

The Session object displays information about the loaded F1 session: year, Grand Prix name, session type, backend, and driver count. Display Features:
  • 🏎️ Emoji icon for quick visual identification
  • Bordered container with light gray background
  • Structured table layout with key-value pairs
  • URL-decoded Grand Prix and session names (handles special characters)
  • Dynamic driver count (shows “Not loaded” if drivers have not been fetched yet)
  • Backend library indicator (pandas or polars)
HTML Output Structure:
Visual Output:

Driver Display

The Driver object displays information about one driver in a session: driver code, session context, and data loading status. Display Features:
  • 👤 Emoji icon for driver identification
  • Light blue background (#f0f8ff) for visual distinction
  • Session context (year, Grand Prix, session type)
  • Laps loading status indicator
  • Compact, single-driver focused layout
HTML Output Structure:
Visual Output:

Lap Display

The Lap object displays information about a specific lap, including lap number, driver, session context, and telemetry loading status. Display Features:
  • 🏁 Emoji icon for lap identification
  • Wheat-colored background (#fff8dc) for visual distinction
  • Lap number prominently displayed in header
  • Driver code and session context
  • Telemetry loading status indicator
  • Useful for tracking which laps have telemetry data loaded
HTML Output Structure:
Visual Output:

DataFrame Summary Display

DataFrames (both pandas and polars) automatically display with enhanced summary information when returned from tif1 methods. Display Features:
  • Green left border for visual emphasis
  • Row count with thousands separator
  • Column count
  • Memory usage in MB (calculated differently for pandas vs polars)
  • Compact single-line format
  • Appears above the standard DataFrame display
HTML Output Structure:
Memory Calculation:
  • Pandas: Uses df.memory_usage(deep=True).sum() for accurate memory including object dtypes
  • Polars: Uses df.estimated_size() if available, otherwise returns 0

Manual Control and Advanced Usage

The Jupyter integration activates automatically. Control and customize the display behavior manually for advanced use cases.

Manual Activation

Manually enable Jupyter displays with the enable_jupyter_display() function when needed (for example, after importing tif1 before IPython was available):
What This Does:
  • Imports Session, Driver, and Lap classes from tif1.core
  • Patches each class with a _repr_html_() method
  • Logs activation status to the logger
  • Handles errors gracefully if classes cannot be imported
When to Use:
  • Dynamic module loading scenarios
  • Custom IPython kernel configurations
  • Testing and development
  • Troubleshooting display issues

Environment Detection

Check if the code runs in a Jupyter environment:
Use Cases:
  • Conditional display logic
  • Notebook vs script detection
  • Custom display implementations
  • Testing different output formats
Note: The function name starts with an underscore (_is_notebook). This marks an internal API, but the function is stable and safe to use.

Programmatic HTML Generation

Generate HTML representations programmatically without displaying them:

Custom Display Functions

Create custom display functions for specific use cases:

Disabling Rich Displays

Use standard text output in Jupyter when preferred:

Notebook Best Practices and Patterns

1. Efficient Data Loading

Load session data efficiently by controlling what gets fetched:
Performance Tips:
  • Use session.load() with specific parameters to avoid fetching unnecessary data
  • Telemetry data is large - only load when needed
  • Weather and messages are lightweight - safe to load by default
  • Cache is automatically used for subsequent loads

2. Progress Indication

Provide visual feedback during long-running operations:
Advanced Progress Tracking:

3. Interactive Data Exploration

Explore data interactively with rich displays:
Structured Exploration Pattern:

4. Combining with Plotting

Integrate rich displays with matplotlib visualizations:

5. Interactive Widgets

Use ipywidgets for interactive data exploration:
Advanced Widget Example with Multiple Controls:

6. Memory Management

Handle large datasets efficiently in notebooks:
Memory Monitoring:

Troubleshooting

Display Not Working

If rich displays are not showing:

HTML Display Issues

If HTML displays are broken:

Memory Issues

For large datasets in notebooks:

Complete notebook example


API Reference

Functions

function
Enable rich Jupyter display for tif1 objects. Called automatically when in a notebook.
function
Check if running in a Jupyter notebook environment.Returns: bool - True if in Jupyter, False otherwise
function
Generate HTML display for a Session object.Parameters:
  • session: Session object
Returns: str - HTML string
function
Generate HTML display for a Driver object.Parameters:
  • driver: Driver object
Returns: str - HTML string
function
Generate HTML display for a Lap object.Parameters:
  • lap: Lap object
Returns: str - HTML string
function
Generate summary display for a DataFrame.Parameters:
  • df: pandas or polars DataFrame
Returns: str - HTML string with summary
Last modified on September 3, 2026