CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-sphinx-rtd-theme

Read the Docs theme for Sphinx documentation with responsive design and interactive navigation

Overview
Eval results
Files

python-extension.mddocs/

Python Extension API

Core Sphinx extension functionality that registers the theme, validates configuration, and extends template context for Read the Docs integration.

Capabilities

Theme Setup and Registration

Main setup function that registers the sphinx_rtd_theme with Sphinx and configures all necessary components.

def setup(app):
    """
    Main theme setup function called by Sphinx.
    
    Parameters:
    - app: Sphinx application instance
    
    Returns:
    dict: Extension metadata with parallel_read_safe and parallel_write_safe flags
    
    Actions:
    - Validates Python 3+ and Sphinx 6+ requirements  
    - Registers theme with Sphinx via app.add_html_theme()
    - Sets up message catalogs for internationalization support
    - Configures jQuery dependency via sphinxcontrib-jquery extension
    - Connects event handlers for configuration validation and context extension
    - Sets permalink icon to FontAwesome link icon (\uf0c1)
    """

Usage Example:

# Theme is automatically registered via entry point in setup.cfg:
# [options.entry_points]
# sphinx.html_themes = 
#     sphinx_rtd_theme = sphinx_rtd_theme

# In conf.py:
html_theme = 'sphinx_rtd_theme'

Theme Path Access

Provides access to theme directory path (deprecated function maintained for backwards compatibility).

def get_html_theme_path():
    """
    Return list of HTML theme paths (deprecated).
    
    Returns:
    str: Absolute path to theme directory
    
    Status: Deprecated with warning message
    Note: Safe to remove calls to this function when defining html_theme_path
    """

Configuration Validation

Validates theme configuration and warns about deprecated options.

def config_initiated(app, config):
    """
    Sphinx event handler for configuration validation.
    
    Parameters:
    - app: Sphinx application instance
    - config: Sphinx configuration object
    
    Validates and warns about deprecated options:
    - canonical_url (use html_baseurl from Sphinx instead)
    - analytics_id (use sphinxcontrib-googleanalytics extension instead)
    - analytics_anonymize_ip (use sphinxcontrib-googleanalytics extension instead)
    - extra_css_file (use html_css_files from Sphinx instead)
    """

Template Context Extension

Extends Jinja2 template context with theme-specific variables and Read the Docs environment detection.

def extend_html_context(app, pagename, templatename, context, doctree):
    """
    Sphinx event handler for extending template context.
    
    Parameters:
    - app: Sphinx application instance
    - pagename: Current page name being rendered
    - templatename: Name of template being rendered
    - context: Template context dictionary (modified in-place)
    - doctree: Document tree for current page
    
    Adds to context:
    - sphinx_version_info: Sphinx version tuple for template conditionals
    - READTHEDOCS: Boolean indicating Read the Docs environment
    - READTHEDOCS_*: All Read the Docs environment variables
    """

Template Context Variables:

# Available in all templates after context extension:
{
    'sphinx_version_info': tuple,  # e.g. (7, 1, 2)
    'READTHEDOCS': bool,           # True if building on Read the Docs
    'READTHEDOCS_VERSION': str,    # RTD version identifier (if applicable)
    'READTHEDOCS_PROJECT': str,    # RTD project name (if applicable)
    'READTHEDOCS_LANGUAGE': str,   # RTD language code (if applicable)
    # ... additional READTHEDOCS_* variables as available
}

Module Constants

__version__ = "3.0.2"
__version_full__ = "3.0.2" 
logger = getLogger(__name__)  # Package logger instance

Entry Point Registration

# Automatic registration via setup.cfg:
[options.entry_points]
sphinx.html_themes = 
    sphinx_rtd_theme = sphinx_rtd_theme

Error Handling

The setup function performs validation and raises errors for:

  • Python version: Requires Python 3+
  • Sphinx version: Requires Sphinx 6.0+
  • HTML4 writer: Not supported, raises error if html4_writer is enabled
  • jQuery dependency: Automatically adds sphinxcontrib-jquery for Sphinx 6+

Usage Examples

Basic Theme Usage

# In Sphinx conf.py - theme is automatically available
html_theme = 'sphinx_rtd_theme'

Advanced Setup Integration

# Custom extension that uses theme functions
def my_setup(app):
    # Access theme path (though not typically needed)
    theme_path = sphinx_rtd_theme.get_html_theme_path()
    
    # The theme's setup function handles all necessary registration
    # No manual theme registration needed due to entry point
    
    return {'version': '1.0', 'parallel_read_safe': True}

Environment Detection

# In Jinja2 templates, after context extension:
{% if READTHEDOCS %}
    <div class="rtd-environment">
        Building on Read the Docs: {{ READTHEDOCS_VERSION }}
    </div>
{% endif %}

{% if sphinx_version_info >= (7, 0, 0) %}
    <!-- Use Sphinx 7+ features -->
{% endif %}

Install with Tessl CLI

npx tessl i tessl/pypi-sphinx-rtd-theme

docs

build-system.md

index.md

javascript-navigation.md

python-extension.md

theme-configuration.md

tile.json