Skip to main content

A Sphinx extension that allows fetching of other HTML elements after DOM load.

Project description

TeachBooks Fetch

A Sphinx extension that allows fetching of other HTML elements after DOM load.

This extension introduces two new directives:

  • fetch: Fetches content from a specified reference and inserts it into the rendered document.
  • click-to-fetch: Creates a clickable element that, when clicked, fetches content from a specified reference and inserts it into the rendered document.

The extension is designed to be used for the HTML output of Jupyter Book and Sphinx projects, and can be easily installed and activated as described below.

Installation

pip install teachbooks-fetch

Activation

Jupyter Book

Add the extension to your _config.yml:

sphinx:
  extra_extensions:
    - teachbooks_fetch

Sphinx

Add the extension to your conf.py:

extensions = [
    # ...
    'teachbooks_fetch',
]

Configuration

Currently no configuration options are available for this extension.

Usage

Fetch Directive

The fetch directive allows you to fetch content from a specified reference and insert it into the rendered document.

Minimal usage (MyST syntax):

:::{fetch} <ref>
:::

Replace <ref> with code that generates a reference to the content you want to fetch. This can be one of the following:

  • A markdown style link, e.g. [link text](url#id).
  • An internal reference using the roles ref, numref, prf:ref (see Sphinx Proof) or bdg-link-<name> (see Sphinx Design).
  • A HTML link, e.g. <a href="url#id">link text</a>.

This will, initially, render a placeholder admonition with the following base MyST code:

:::{admonition} Fetching <ref>
:class: fetch
Content loading...
:::

In the placeholder the original reference/link is disabled.

After the DOM has loaded, the extension will attempt to fetch the content from the specified reference and replace the placeholder with the fetched content. If the fetch fails, the content of the placeholder will be updated to indicate that the fetch failed.

If preferred, you can also specify the content of the placeholder by including content in the directive:

:::{fetch} <ref>
This content will be shown while the fetch of [!a!] is in progress.
:::

Here [!a!] will be replaced with reference given in the directive argument.

If preferred, you can also specify the title of the placeholder by including the title option:

:::{fetch} <ref>
:title: Searching for [!a!]
:::

Here [!a!] will again be replaced with reference given in the directive argument.

If preferred, you can also specify additional CSS classes for the placeholder by including the class option:

:::{fetch} <ref>
:class: my-custom-class
:::

The special CSS class cancel-fetch can be used to indicate that the fetch should not be performed and that the placeholder should be rendered as-is.

Click-to-Fetch Directive

The click-to-fetch directive allows you to create a clickable element that, when clicked, fetches content from a specified reference and inserts it into the rendered document.

Minimal usage (MyST syntax):

:::{click-to-fetch} <ref>
:::

This will, initially, render a placeholder admonition with the following base MyST code:

:::{admonition} Click to fetch <ref>
:class: fetch click-to-fetch
Waiting for a click...
:::

All options, content and behavior for the fetch directive are also available for the click-to-fetch directive. The only difference is that the click-to-fetch directive will not attempt to fetch the content until the user clicks on the placeholder.

If the click-to-fetch behavior is preferred without changing the default placeholder, you can also use the fetch directive with the click-to-fetch class.

Fetch rules

The extension will attempt to fetch the content from the specified reference using the following steps:

  1. Extract the URL and ID from the reference.
  2. Attempt to fetch the content from the URL and extract the element with the specified ID from the fetched HTML.

This fetches the specified element all it's child elements as-is. This means that no CSS files nor JavaScript files will be fetched. This also means that if the fetched element contains any references, these will not be resolved and will be rendered as-is and might result in broken links.

Examples

Examples can be found here: TeachBooks Manual

Contribute

This tool's repository is stored on GitHub. If you'd like to contribute, you can create a fork and open a pull request on the GitHub repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

teachbooks_fetch-0.0.1.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

teachbooks_fetch-0.0.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file teachbooks_fetch-0.0.1.tar.gz.

File metadata

  • Download URL: teachbooks_fetch-0.0.1.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for teachbooks_fetch-0.0.1.tar.gz
Algorithm Hash digest
SHA256 deb6fe7816dbb4a4b86289f5644f5e85b5e7c8be097a1a4b1f43f805aaca1284
MD5 0528497cd8fd68674ea9505e7e3b1a74
BLAKE2b-256 005bfd9f822cd87d7a94ace1e76d3e45ad760108efb22b058f5ac7846d1d5ca1

See more details on using hashes here.

Provenance

The following attestation bundles were made for teachbooks_fetch-0.0.1.tar.gz:

Publisher: python-publish.yml on TeachBooks/TeachBooks-Fetch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file teachbooks_fetch-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for teachbooks_fetch-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4233c2fdcbc6f8bb8e897e1c0fc9cdf4a9f79a2bbef8fa7c98688eebc0d28dc6
MD5 acd6b9208bd205f8f96571f996a25468
BLAKE2b-256 7f2477b78c112c60e2ddb16351e77f9a108628a6c3c18a618815112d79d374a2

See more details on using hashes here.

Provenance

The following attestation bundles were made for teachbooks_fetch-0.0.1-py3-none-any.whl:

Publisher: python-publish.yml on TeachBooks/TeachBooks-Fetch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page