Skip to main content

Embed interactive notebooks in your MkDocs site using JupyterLite.

Project description

mkdocs-jupyterlite

A MkDocs plugin for embedding interactive notebooks in your docs via jupyterlite.

Screenshot of a JupyterLite notebook embedded in MkDocs

Say you have a notebook example.ipynb in your awesome project, and you want users to be able to play around with it. By using JupyterLite, you can run Jupyter notebooks directly in the browser without any server-side dependencies.

See this project's documentation site for a live example.

Installation

Step 1: Install the plugin from PyPI:

python -m pip install mkdocs-jupyterlite

Step 2: Configure your mkdocs.yml file

See the mkdocs.yml that configures this project's site.

site_name: mkdocs-jupyterlite
site_url: https://nickcrews.github.io/mkdocs-jupyterlite/
repo_url: https://github.com/nickcrews/mkdocs-jupyterlite/

nav:
  - Home: index.md
  - Notebook 1: notebook.ipynb

plugins:
  - jupyterlite:
      # An easy way to turn off the entire plugin.
      enabled: true
      # Uses the same syntax as .gitignore:
      # Include and exclude files using glob patterns, the last matching pattern
      # determines if a file is included or excluded.
      notebook_patterns:
        # Include all. This is the default.
        - "**/*.ipynb"
        # Exclude drafts.
        - "!**/draft_*.ipynb"
        # Re-include a specific draft.
        - "project/drafts/draft_keep.ipynb"
        # Exclude a notebook at the root. Doesn't match /a/b/c/top_secret.ipynb
        - "!/top_secret.ipynb"
      # Add custom wheels so that when you
      # `%pip install my-package` in the notebook,
      # the micropip library has a backup after PyPI to fall back on.
      wheels:
        # Specify a url directly.
        - url: "https://files.pythonhosted.org/packages/2d/2c/7f32ba15302847f0cd0d01101470b2f427ec5b3a07756f41c823c01c0242/ibis_framework-10.5.0-py3-none-any.whl"
        # Run a shell command that dynamically
        # builds/fetches/creates 0 to N .whl files in the given {wheels_dir}
        # (which will be replaced by this plugin with a real, temporary directory).
        - command: "curl -L -o {wheels_dir}/cowsay-6.1-py3-none-any.whl https://files.pythonhosted.org/packages/f1/13/63c0a02c44024ee16f664e0b36eefeb22d54e93531630bd99e237986f534/cowsay-6.1-py3-none-any.whl"
        - command: "cd src/package_not_on_pypi/ && uv build --out-dir {wheels_dir}"

Here are the details on the configuration options:

enabled

bool, whether or not the plugin is enabled. Defaults to true.

notebook_patterns

A list of patterns that uses gitignore semantics to include and exclude files. The last matching pattern will be used to determine if a file is a notebook.

These are resolved relative to your MkDocs docs_dir directory, which by default for most projects is docs/. So if you have a notebook at docs/notebook.ipynb, that corresponds to the pattern /notebook.ipynb.

For all files that match, the content of the page will be an iframe that embeds the JupyterLite Notebook html.

wheels

A list of wheels to include in the JupyterLite environment. The simplest form is to specify a file path or URL directly underneath the url key.

Or, if you use the command key, this is interpreted as a shell command. This plugin will replace the {wheels_dir} placeholder with a temporary directory, and then run the command in the directory that the mkdocs command was run from. This command must place/create 0 to N .whl files in the given {wheels_dir} directory.

Related Work and Alternatives

  • Binder: A popular tool for creating sharable, interactive environments for Jupyter notebooks. Requires a full docker environment and a remote server. The notebook is hosted separately from your docs, so a user has to click away in order to run the notebook. Takes 30-60 seconds to boot up a new environment.
  • mkdocs-jupyter: A MkDocs plugin that embeds static Jupyter notebooks into your MkDocs site. That project is very similar to this one, except that one executes and renders the notebooks as static HTML at build time, so they aren't interactive on the site. It is much more mature than this project though.
  • jupyterlite-sphinx: A Sphinx extension that integrates JupyterLite within your Sphinx documentation. That project is very similar to this one, except that it is for Sphinx instead of MkDocs. It also has more features than this project. I will probably use that as inspiration for future development of this project, for example how it uses environment.yml to specify python packages that will be included in the JupyterLite environment.
  • jupyterlite: The core project that powers this plugin. You can use jupyterlite directly in your docs, it just is more work. You would need to a build step to package your notebooks, other files, and python dependencies into a single static .html file. Then you would need to inject this .html file into the proper pages of your MkDocs site. This plugin automates that process for you.

Contributing

I want this to be usable for other people, so file an issue if you want to use this in your site, but run into any problems.

Possible improvements:

  • Include custom python wheels into the JupyterLite environment.
  • Passing an entire jupyter-lite.json config file.
  • Instead of using an iframe, actually inline the contents of the generated HTML?
  • Fix the TOC so clicking headers actually scrolls in the iframe.

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

mkdocs_jupyterlite-0.3.2.tar.gz (141.4 kB view details)

Uploaded Source

Built Distribution

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

mkdocs_jupyterlite-0.3.2-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file mkdocs_jupyterlite-0.3.2.tar.gz.

File metadata

  • Download URL: mkdocs_jupyterlite-0.3.2.tar.gz
  • Upload date:
  • Size: 141.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for mkdocs_jupyterlite-0.3.2.tar.gz
Algorithm Hash digest
SHA256 ad69e44a880b7c468808b7ea775b36a3408ca0415b8fb829b926e8fab6ca36b5
MD5 23c2d24f0915bf13cd1c8f4eb0c02c75
BLAKE2b-256 6ff9581036fc9aad8fb864a37f500900cf69bc79fb6bad1d8092c4af6e19086c

See more details on using hashes here.

Provenance

The following attestation bundles were made for mkdocs_jupyterlite-0.3.2.tar.gz:

Publisher: publish.yml on NickCrews/mkdocs-jupyterlite

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

File details

Details for the file mkdocs_jupyterlite-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for mkdocs_jupyterlite-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1d7461a8c8568d78b1d1a1745c021fbf486d380d2e73aaab36baa9034cc4c38a
MD5 f172221aa546d83219c32d038b15ec38
BLAKE2b-256 575d0f259b1e4136e9b6d401b41419750e2cbd71695c6071d279d42230344e26

See more details on using hashes here.

Provenance

The following attestation bundles were made for mkdocs_jupyterlite-0.3.2-py3-none-any.whl:

Publisher: publish.yml on NickCrews/mkdocs-jupyterlite

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