Configuration

The following extension scoped configuration parameters are available. These should be added to the Sphinx configuration file. For example, to override the default typer_render_html() function our conf.py might look like:

import html

extensions = [
    'sphinxcontrib.typer',
]

# change the default iframe padding
typer_iframe_height_padding = 20

# redfine the default render_html function
def typer_render_html(
    directive: TyperDirective,
    normal_cmd: str,
    html_page: str
) -> str:
    return f'<iframe srcdoc="{html.escape(html_page)}"></iframe>'

Configuration Attributes

typer_iframe_height_padding
Type:
int
Default:
30

A number of pixels to use for padding html iframes.

typer_render_html
Type:
str | Callable[[TyperDirective, str, str], str]
Default:
"sphinxcontrib.typer.typer_render_html"

A callable function (or import path to a callable function) that returns the html to embed in an html page. Only used if the target format is html. See default implementation typer_render_html().

typer_get_iframe_height
Type:
str | Callable[[TyperDirective, str, str], int]
Default:
"sphinxcontrib.typer.typer_get_iframe_height"

A callable function that determines height of the iframe when rendering html format onto an html page. The function must return an integer containing the iframe height. See the default implementation typer_get_iframe_height().

typer_svg2pdf
Type:
str | Callable[[TyperDirective, str, str], None]
Default:
"sphinxcontrib.typer.typer_svg2pdf"

A callable function to convert svg to pdf. The function must write the converted pdf format to the given path. This is only used for latex/pdf builders. See the default implementation typer_svg2pdf().

typer_convert_png
Type:
str | Callable[[TyperDirective, str, str | Path, int, int], None]
Default:
"sphinxcontrib.typer.typer_convert_png"

A callable function to convert the given format to png. The function must write the converted png format to the given path. This function is used when the builder is listed in the typer:convert-png: parameter. See the default implementation typer_convert_png().

typer_get_page
Type:
str | Callable[[TyperDirective, int, int], ContextManager[Page]]
Default:
"sphinxcontrib.typer.typer_get_page"

A callable function to get a playwright browser page. This function must be a context manager and it must yield a playwright.sync_api.Page. It is used by other workflows that need to render content in a browser (iframe height calculation and png conversion). See the default implementation typer_get_page().

Changed in version 0.10.0: Replaces typer_get_web_driver which yielded a selenium web driver.

typer_playwright_install
Type:
bool
Default:
True

When the playwright chromium browser is not installed, install it automatically by running playwright install chromium in a subprocess the first time it is needed. Set to False to fail with an error instead.

Added in version 0.10.0.

typer_dark_theme
Type:
str | None
Default:
"auto"

The default typer:dark-theme for all typer directives. The default "auto" resolves to "dark" when html_theme is a theme known to support light and dark modes (furo, pydata_sphinx_theme and sphinx_book_theme) and to None otherwise. Set a theme name (see typer:theme) to render every help output for both modes regardless of the theme, or None to switch dual rendering off.

Added in version 0.10.0.

Function Hooks

These functions may all be redefined in conf.py to override default behaviors. Your override functions must conform to these function signatures.

Warning

Sphinx will warn that these functions are not pickleable. This messes up sphinx’s caching but that wont break the doc build. You can either suppress the warning or specify these configuration values as import strings instead.

sphinxcontrib.typer.typer_render_html(directive, normal_cmd, html_page)[source]

The default html rendering function. This function returns the html console output wrapped in an iframe. The height of the iframe is dynamically determined by calling the configured typer_get_iframe_height function.

Parameters:
  • directive (TyperDirective) – The TyperDirective instance

  • normal_cmd (str) – The normalized name of the command. (Subcommands are delimited by :)

  • html_page (str) – The html page rendered by console.export_html

Return type:

str

sphinxcontrib.typer.typer_get_iframe_height(directive, normal_cmd, html_page)[source]

The default iframe height calculation function. The iframe height resolution proceeds as follows:

  1. Return the global iframe-height parameter if one was supplied as a parameter on the directive.

  2. Check for a cached height value.

  3. Render the page in a headless browser (see typer_get_page()) to dynamically determine the height of the iframe. Padding will be added from the config.typer_iframe_height_padding configuration value. The resulting height is then cached.

Parameters:
  • directive (TyperDirective) – The TyperDirective instance

  • normal_cmd (str) – The normalized name of the command. (Subcommands are delimited by :)

  • html_page (str) – The full html document that will be rendered in the iframe

Return type:

int

sphinxcontrib.typer.typer_svg2pdf(directive, svg_contents, pdf_path)[source]

The default typer_svg2pdf function. This function uses the cairosvg package to convert svg to pdf.

Note

You will likely need to install fonts locally on your machine for the output of these conversions to look correct. The default font used by the svg export from rich is FiraCode.

Parameters:
  • directive (TyperDirective) – The TyperDirective instance

  • svg_contents (str) – The svg contents to convert to pdf

  • pdf_path (str) – The path to write the pdf to

sphinxcontrib.typer.typer_convert_png(directive, rendered, png_path, width=1920, height=2048)[source]

The default typer_convert_png function. This function writes a png file to the given path by taking a screenshot of the rendered help in a headless browser (see typer_get_page()). It requires playwright to be installed.

To override this function with a custom function see the typer_convert_png configuration parameter.

Parameters:
  • directive (TyperDirective) – The TyperDirective instance

  • rendered (str) – The rendered command help. May be html, svg, or text.

  • png_path (str | Path) – The path to write the png to

  • width (int) – The width of the browser viewport, the screenshot is of the rendered element only so this just needs to be larger than the help output.

  • height (int) – The height of the browser viewport.

sphinxcontrib.typer.typer_get_page(directive, width=1920, height=2048)[source]

The default get_page function. This function yields a headless playwright chromium Page. It requires playwright to be installed. If the chromium browser has not been installed it will be installed on first use unless the typer_playwright_install configuration value is False.

To override this function with a custom function see the typer_get_page configuration parameter.

Note

This must be implemented as a context manager that yields the page instance and cleans it up on exit!

Parameters:
  • directive (TyperDirective) – The TyperDirective instance

  • width (int) – The width of the browser viewport in pixels

  • height (int) – The height of the browser viewport in pixels

Return type:

Iterator[Any]

sphinxcontrib.typer.typer_install_browser(directive)[source]

Install the chromium browser used by playwright by running python -m playwright install chromium in a subprocess with the active interpreter.

This is invoked automatically by typer_get_page() when the browser is missing and the typer_playwright_install configuration value is enabled.

Parameters:

directive (TyperDirective) – The TyperDirective instance

Return type:

None