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

configuration.mddocs/

Configuration

Comprehensive configuration system with 40+ options controlling execution, rendering, output formatting, and display behavior. The configuration system provides fine-grained control over every aspect of notebook processing.

Capabilities

Main Configuration Class

Central configuration dataclass that manages all MyST-NB settings with validation and type checking.

class NbParserConfig:
    """
    Central configuration for MyST-NB notebook parsing and processing.
    
    Contains all configuration options with default values, validation,
    and documentation for each setting.
    """
    
    # File read options
    custom_formats: Dict[str, Tuple[str, dict, bool]] = {}
    read_as_md: bool = False
    
    # Configuration override keys
    metadata_key: str = "mystnb"
    cell_metadata_key: str = "mystnb"
    
    # Notebook execution options
    kernel_rgx_aliases: Dict[str, str] = {}
    eval_name_regex: str = r"^[a-zA-Z_][a-zA-Z0-9_]*$"
    execution_mode: Literal["off", "force", "auto", "cache", "inline"] = "auto"
    execution_cache_path: str = ""
    execution_excludepatterns: Sequence[str] = ()
    execution_timeout: int = 30
    execution_in_temp: bool = False
    execution_allow_errors: bool = False
    execution_raise_on_error: bool = False
    execution_show_tb: bool = False
    
    # Pre-processing options
    merge_streams: bool = False
    
    # Render options
    render_plugin: str = "default"
    remove_code_source: bool = False
    remove_code_outputs: bool = False
    scroll_outputs: bool = False
    code_prompt_show: str = "Show code cell {type}"
    code_prompt_hide: str = "Hide code cell {type}"
    number_source_lines: bool = False
    builder_name: str = "html"
    mime_priority_overrides: Sequence[Tuple[str, str, Optional[int]]] = ()
    output_stderr: Literal["show", "remove", "remove-warn", "warn", "error", "severe"] = "show"
    render_text_lexer: str = "myst-ansi"
    render_error_lexer: str = "ipythontb"
    render_image_options: Dict[str, str] = {}
    render_figure_options: Dict[str, str] = {}
    render_markdown_format: Literal["commonmark", "gfm", "myst"] = "commonmark"
    ipywidgets_js: Dict[str, Dict[str, str]] = ipywidgets_js_factory()
    
    # Write options for docutils
    output_folder: str = "build"
    append_css: bool = True
    metadata_to_fm: bool = False

Configuration Sections

Enumeration defining different levels of configuration scope for hierarchical settings management.

class Section(Enum):
    """
    Configuration section tags.
    
    Defines the scope and hierarchy of configuration settings:
    - global_lvl: Global level configuration
    - file_lvl: File level configuration (notebook)
    - cell_lvl: Cell level configuration
    - config: Meta configuration
    - read: Configuration for reading files
    - execute: Configuration for executing notebooks
    - render: Configuration for rendering notebook elements
    """
    global_lvl = "global"
    file_lvl = "notebook" 
    cell_lvl = "cell"
    config = "config"
    read = "read"
    execute = "execute"
    render = "render"

Execution Configuration

Execution Modes

  • "auto": Execute cells only if outputs are missing
  • "force": Always execute all cells
  • "cache": Use jupyter-cache for execution caching
  • "inline": Execute only inline evaluation expressions
  • "off": Never execute cells

Execution Settings

# Core execution options
execution_mode: str = "auto"
execution_timeout: int = 30  # Timeout in seconds
execution_show_tb: bool = False  # Show full tracebacks
execution_in_temp: bool = False  # Execute in temporary directory
execution_allow_errors: bool = False  # Continue on cell errors
execution_raise_on_error: bool = False  # Raise exceptions on errors

Rendering Configuration

Output Control

# Code visibility
remove_code_source: bool = False  # Hide input code
remove_code_outputs: bool = False  # Hide outputs
remove_markdown_source: bool = False  # Hide markdown source

# Output handling
output_stderr: str = "show"  # "show", "remove", "remove-warn", "warn", "error"
merge_streams: bool = False  # Merge stdout/stderr

MIME Type and Rendering

# Renderer selection
render_plugin: str = "default"  # Renderer plugin name
render_priority: dict = None  # MIME type priority overrides

# Syntax highlighting
render_text_lexer: str = "myst-ansi"  # Lexer for text output
render_error_lexer: str = "ipythontb"  # Lexer for error output

Image and Figure Options

# Image rendering
render_image_options: dict = {
    "width": "100%",
    "align": "center"
}

# Figure rendering
render_figure_options: dict = {
    "caption": True,
    "numbering": True
}

Advanced Configuration

Custom Formats

# Custom notebook formats
custom_formats: dict = {
    ".Rmd": "Rmd",
    ".qmd": "qmd"
}

# Kernel name aliases
kernel_rgx_aliases: dict = {
    "python.*": "python",
    "ir": "r"
}

Cell-Level Configuration

Individual cells can override global configuration through cell metadata:

{
    "tags": ["hide-input", "hide-output"],
    "mystnb": {
        "execution_timeout": 60,
        "remove_code_source": true
    }
}

Configuration Usage Examples

Sphinx Configuration

# In conf.py for Sphinx
extensions = ['myst_nb']

# Execution settings
nb_execution_mode = "cache"
nb_execution_timeout = 120
nb_execution_cache_path = ".jupyter_cache"

# Display settings
nb_remove_code_source = False
nb_remove_code_outputs = False
nb_merge_streams = True
nb_output_stderr = "show"

# Rendering settings
nb_render_plugin = "default"
nb_render_image_options = {"width": "75%"}
nb_render_figure_options = {"align": "center"}

Docutils Configuration

from myst_nb.core.config import NbParserConfig

# Create configuration
config = NbParserConfig(
    execution_mode="force",
    execution_timeout=60,
    remove_code_source=True,
    render_text_lexer="python"
)

File-Level Configuration

In MyST markdown files:

---
mystnb:
  execution_mode: "force"
  execution_timeout: 120
  remove_code_source: false
---

Environment-Based Configuration

import os
from myst_nb.core.config import NbParserConfig

# Configure based on environment
config = NbParserConfig(
    execution_mode=os.getenv("NB_EXECUTION_MODE", "auto"),
    execution_timeout=int(os.getenv("NB_TIMEOUT", "30")),
    remove_code_source=os.getenv("NB_HIDE_CODE") == "true"
)

## Warning System

### Warning Types

MyST-NB provides a structured warning system for handling various processing issues.

```python { .api }
class MystNBWarnings(Enum):
    """
    Enumeration of MyST-NB warning types for filtering and error handling.
    
    Warning types:
    - LEXER: Issues with syntax highlighting lexer selection
    - FIG_CAPTION: Problems with figure caption processing
    - MIME_TYPE: MIME type handling issues  
    - OUTPUT_TYPE: Unsupported output type warnings
    - CELL_METADATA_KEY: Cell metadata key conflicts
    - CELL_CONFIG: Cell-level configuration issues
    """
    LEXER = "lexer"
    FIG_CAPTION = "fig_caption"
    MIME_TYPE = "mime_type"
    OUTPUT_TYPE = "output_type"
    CELL_METADATA_KEY = "cell_metadata_key"
    CELL_CONFIG = "cell_config"

Warning Filtering

# In Sphinx conf.py - filter specific warning types
import warnings
from myst_nb.warnings_ import MystNBWarnings

# Suppress specific warning types
warnings.filterwarnings("ignore", category=UserWarning, 
                       message=f".*{MystNBWarnings.LEXER.value}.*")

Custom Lexers

MyST-NB provides custom Pygments lexers for enhanced syntax highlighting.

ANSI Color Lexer

class AnsiColorLexer:
    """
    Pygments lexer for ANSI color escape sequences.
    
    Entry point: 'myst-ansi'
    Used for: render_text_lexer configuration
    Purpose: Syntax highlighting of terminal output with color codes
    """

IPython Traceback Lexer

class IPythonTracebackLexer:
    """
    Pygments lexer for IPython/Jupyter error tracebacks.
    
    Entry point: 'ipythontb'  
    Used for: render_error_lexer configuration
    Purpose: Enhanced formatting of Python exception tracebacks
    """

Important Constants

MIME Type Constants

# Widget-related MIME types
WIDGET_STATE_MIMETYPE = "application/vnd.jupyter.widget-state+json"
WIDGET_VIEW_MIMETYPE = "application/vnd.jupyter.widget-view+json"

# Entry point group names
RENDER_ENTRY_GROUP = "myst_nb.renderers"
MIME_RENDER_ENTRY_GROUP = "myst_nb.mime_renderers"

# Glue system
GLUE_PREFIX = "application/papermill.record/"

# Sphinx output folder
OUTPUT_FOLDER = "jupyter_execute"

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