CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-myst-nb

A Jupyter Notebook Sphinx reader built on top of the MyST markdown parser.

Pending
Overview
Eval results
Files

reading-processing.mddocs/

Reading and Processing

Reading, parsing, and processing of Jupyter notebooks in various formats including standard .ipynb and MyST markdown notebooks. The reading system provides flexible input handling with format detection and conversion capabilities.

Capabilities

Standard Notebook Reading

Function for reading standard Jupyter notebook files in .ipynb format.

def standard_nb_read(text: str) -> nbf.NotebookNode:
    """
    Read standard .ipynb notebook format.
    
    Parameters:
    - text: str - Raw notebook JSON content as string
    
    Returns:
    nbf.NotebookNode: Parsed notebook object
    
    Parses JSON notebook content and returns a structured
    NotebookNode object for further processing.
    """

Notebook Reader Factory

Factory function for creating appropriate reader instances based on file format and configuration.

def create_nb_reader(
    path: str,
    md_config: MdParserConfig,
    nb_config: NbParserConfig,
    content: None | str | Iterator[str]
) -> NbReader | None:
    """
    Create appropriate notebook reader based on format and configuration.
    
    Parameters:
    - path: str - Path to the input source being processed
    - md_config: MdParserConfig - Configuration for parsing Markdown
    - nb_config: NbParserConfig - Configuration for parsing Notebooks
    - content: None | str | Iterator[str] - Input string (optionally used to check for text-based notebooks)
    
    Returns:
    NbReader | None: Configured reader instance or None if format unsupported
    
    Analyzes file format and creates the appropriate reader for processing.
    """

MyST Markdown Notebook Reading

Specialized reader for MyST markdown notebooks that contain notebook content in markdown format.

def read_myst_markdown_notebook(...):
    """
    Read MyST markdown notebooks.
    
    Parameters:
    - content: str - MyST markdown content
    - path: str - File path for context
    - config: NbParserConfig - Reading configuration
    
    Returns:
    nbf.NotebookNode: Converted notebook object
    
    Parses MyST markdown format and converts to notebook structure
    with proper cell separation and metadata handling.
    """

Notebook Reader Class

Main reader class that encapsulates notebook reading functionality and configuration.

class NbReader:
    """
    Notebook reader with format detection and processing capabilities.
    
    Handles various notebook formats and provides unified interface
    for reading and processing notebook content.
    """
    
    def __init__(self, path: str, config: NbParserConfig):
        """
        Initialize notebook reader.
        
        Parameters:
        - path: str - Path to notebook file
        - config: NbParserConfig - Reader configuration
        """
    
    def read(self) -> nbf.NotebookNode:
        """Read and parse the notebook."""
        pass
        
    def detect_format(self) -> str:
        """Detect notebook format from file content."""
        pass

Jupyter-Cache Plugin

Plugin function for integration with jupyter-cache system for cached reading.

def myst_nb_reader_plugin():
    """
    MyST-NB reader plugin for jupyter-cache integration.
    
    Returns:
    Callable: Reader function compatible with jupyter-cache
    
    Provides jupyter-cache integration for cached notebook reading
    and processing within the caching system.
    """

Supported Formats

Standard Jupyter Notebooks (.ipynb)

Standard JSON-based Jupyter notebook format with cells, metadata, and outputs.

{
  "cells": [
    {
      "cell_type": "code",
      "source": ["print('Hello, World!')"],
      "metadata": {},
      "outputs": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}

MyST Markdown Detection

Function to check if input text represents a MyST markdown notebook.

def is_myst_markdown_notebook(text: str | Iterator[str]) -> bool:
    """
    Check if the input is a MyST Markdown notebook.
    
    Parameters:
    - text: str | Iterator[str] - Input text to check
    
    Returns:
    bool: True if the input is a markdown notebook
    
    Identifies MyST markdown notebooks by checking for top-matter
    section containing either 'file_format: mystnb' or Jupytext
    format configuration.
    """

MyST Notebook Reader Plugin

Plugin function for reading MyST notebooks from file URIs.

def myst_nb_reader_plugin(uri: str) -> nbf.NotebookNode:
    """
    Read a MyST notebook from a string.
    
    Parameters:
    - uri: str - URI/path to the notebook file
    
    Returns:
    nbf.NotebookNode: Parsed notebook object
    
    Used as plugin for jupyter-cache to read MyST notebooks
    from file URIs with source mapping enabled.
    """

Metadata Parsing Error

Exception class for MyST metadata parsing errors.

class MystMetadataParsingError(Exception):
    """
    Error when parsing metadata from MyST formatted text.
    
    Raised when MyST markdown notebook metadata cannot be
    properly parsed or contains invalid structure.
    """

MyST Markdown Notebooks

Markdown-based notebook format with special cell delimiters and metadata.

---
jupyter:
  kernelspec:
    name: python3
    display_name: Python 3
---

# My Notebook

This is a markdown cell.

```{code-cell} python
print("Hello, World!")

Another markdown cell.

:tags: [hide-output]
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [1, 4, 2])
plt.show()
### Custom Formats

Support for custom notebook formats through configuration:

```python
# In configuration
custom_formats = {
    ".Rmd": "Rmd",      # R Markdown
    ".qmd": "qmd",      # Quarto
    ".py": "py:percent" # Python with percent comments
}

Usage Examples

Basic Notebook Reading

from myst_nb.core.read import standard_nb_read
import nbformat as nbf

# Read standard notebook
with open("notebook.ipynb", "r") as f:
    content = f.read()

nb = standard_nb_read(content)
print(f"Notebook has {len(nb.cells)} cells")

MyST Markdown Reading

from myst_nb.core.read import read_myst_markdown_notebook
from myst_nb.core.config import NbParserConfig

# Read MyST markdown notebook
with open("notebook.md", "r") as f:
    content = f.read()

config = NbParserConfig()
nb = read_myst_markdown_notebook(content, "notebook.md", config)

# Process cells
for i, cell in enumerate(nb.cells):
    print(f"Cell {i}: {cell.cell_type}")
    if cell.cell_type == "code":
        print(f"  Source: {cell.source[:50]}...")

Reader Factory Usage

from myst_nb.core.read import create_nb_reader
from myst_nb.core.config import NbParserConfig

config = NbParserConfig(
    execution_mode="auto",
    custom_formats={".Rmd": "Rmd"}
)

# Create reader for different formats
reader = create_nb_reader("notebook.ipynb", None, config)
if reader:
    nb = reader.read()
    print(f"Read notebook with {len(nb.cells)} cells")

# Handle MyST markdown
reader = create_nb_reader("notebook.md", "myst", config)
if reader:
    nb = reader.read()

Format Detection

import os
from myst_nb.core.read import NbReader

def process_notebook_file(filepath):
    """Process notebook file with automatic format detection."""
    reader = NbReader(filepath, config)
    
    # Detect format
    fmt = reader.detect_format()
    print(f"Detected format: {fmt}")
    
    # Read notebook
    nb = reader.read()
    
    # Process based on format
    if fmt == "ipynb":
        print("Processing standard Jupyter notebook")
    elif fmt == "myst":
        print("Processing MyST markdown notebook")
    elif fmt in config.custom_formats:
        print(f"Processing custom format: {fmt}")
    
    return nb

# Process various formats
notebooks = [
    "standard.ipynb",
    "myst_notebook.md", 
    "r_notebook.Rmd"
]

for nb_path in notebooks:
    if os.path.exists(nb_path):
        nb = process_notebook_file(nb_path)

Batch Processing

import glob
from pathlib import Path
from myst_nb.core.read import create_nb_reader

def read_all_notebooks(directory, config):
    """Read all notebooks in a directory."""
    notebooks = []
    
    # Find all notebook files
    patterns = ["*.ipynb", "*.md", "*.Rmd"]
    for pattern in patterns:
        for filepath in Path(directory).glob(pattern):
            reader = create_nb_reader(str(filepath), None, config)
            if reader:
                try:
                    nb = reader.read()
                    notebooks.append((filepath, nb))
                    print(f"Read: {filepath}")
                except Exception as e:
                    print(f"Error reading {filepath}: {e}")
    
    return notebooks

# Usage
config = NbParserConfig()
notebooks = read_all_notebooks("notebooks/", config)
print(f"Read {len(notebooks)} notebooks")

Integration with Jupyter-Cache

from myst_nb.core.read import myst_nb_reader_plugin
from jupyter_cache import get_cache

# Setup cache with MyST-NB reader
cache = get_cache(".jupyter_cache")

# Register MyST-NB reader plugin
reader_plugin = myst_nb_reader_plugin()

# Use with cached execution
cached_nb = cache.cache_notebook_file("notebook.md", reader=reader_plugin)

Error Handling and Validation

from myst_nb.core.read import standard_nb_read, NbReader
import nbformat as nbf

def safe_notebook_read(filepath):
    """Safely read notebook with error handling."""
    try:
        reader = NbReader(filepath, config)
        nb = reader.read()
        
        # Validate notebook structure
        nbf.validate(nb)
        
        return nb
    except nbf.ValidationError as e:
        print(f"Invalid notebook format: {e}")
        return None
    except FileNotFoundError:
        print(f"File not found: {filepath}")
        return None
    except Exception as e:
        print(f"Error reading notebook: {e}")
        return None

# Usage with validation
nb = safe_notebook_read("notebook.ipynb")
if nb:
    print(f"Successfully read valid notebook with {len(nb.cells)} cells")

The reading and processing system provides the foundation for all MyST-NB functionality, handling format detection, parsing, and conversion to enable seamless notebook integration across different input formats.

Install with Tessl CLI

npx tessl i tessl/pypi-myst-nb

docs

cli.md

configuration.md

docutils.md

execution.md

glue.md

index.md

reading-processing.md

rendering.md

sphinx-extension.md

tile.json