Skip to content

Rendering code

The content rendering code of [Material for] MkDocs is contained in a series of Python libraries. This section describes how to acquire and install them.

The following has been tested on OpenSuSE Leap 15.6, with the distribution-standard version 3.6.15 of Python. Once the rendering code has been isolated, there is no longer a specific dependency on OpenSuSE, but only on the Python interpreter of the environment in which the isolated code will end up getting deployed.

Virtual environment

Material for MkDocs is a hodgepodge of Python libraries. To avoid pollution of a system-standard Python installation, set up a virtual environment for it.

Identification

Pick a location on the filesystem. This needs to be the path to a directory that will be created by the venv installation process. (It is also possible for this to be an existing directory, but in that case the contents of the directory will end up getting modified.)

On the bash command prompt, put the path to the directory into an environment variable – which for this example is called VIRTUAL.

VIRTUAL=~/Projects/MkDocs/Solo

Initialization

Run...

python3 -m venv "$VIRTUAL"

This operation should complete quietly. The venv automatically creates any filesystem directory, within $VIRTUAL, that does not already exist.

At this point, the inside of $VIRTUAL looks like this:

drwxr-xr-x 2 nsure users 4096 Apr 19 15:21 bin
drwxr-xr-x 2 nsure users 4096 Apr 19 15:21 include
drwxr-xr-x 3 nsure users 4096 Apr 19 15:21 lib
lrwxrwxrwx 1 nsure users    3 Apr 19 15:21 lib64 -> lib
-rw-r--r-- 1 nsure users   70 Apr 19 15:21 pyvenv.cfg

Utilization

The previously [created and] filled venv directory is still dormant. To make use of it interactively, run the following bash command:

. "$VIRTUAL/bin/activate"

The fact that the virtual environment in question has been ‘activated’ is denoted by a change in the bash prompt, to which the base name of the virtual environment directory has been prepended. In the example case, that prefix is (Solo).

Python invocations now run against the installation that is contained by the virtual environment directory — its binary executables but also the libraries that have been installed into it using pip.

Departure

To stop making use of the virtual environment in the context of the current bash shell, run the command...

deactivate

Inlining

A bash wrapper script can be put together to have a Python operation executed against a virtual environment.

Python execution commands like...

python3 -c 'import sys; print(sys.path)'

...can be surrounded with python3 -m venv and deactivate commands in order to get something like this,

#!/bin/bash
. ~/Projects/MkDocs/Solo/bin/activate
python3 -c 'import sys; print(sys.path)'
deactivate

When run through the wrapper script, Python library paths will be shown that are specific to the virtual environment.

Clean installation

A basic...

pip install mkdocs-material

...command should be enough to fill the [virtual] Python environment with the necessary components. However, on OpenSuSE Leap 15.6 the operation runs into an error,

Collecting mkdocs-material
  Using cached mkdocs_material-8.2.12-py2.py3-none-any.whl (5.1 MB)
Collecting markdown>=3.2
  Using cached Markdown-3.3.7-py3-none-any.whl (97 kB)
Collecting mkdocs-material-extensions>=1.0.3
  Using cached mkdocs_material_extensions-1.0.3-py3-none-any.whl (8.1 kB)
ERROR: Could not find a version that satisfies the requirement pymdown-extension
s>=9.4 (from mkdocs-material) (from versions: 1.0.0, 1.0.1, 1.1, 1.2, 1.3, 1.4, 
1.5, 1.6, 1.6.1, 1.7, 1.8, 2.0, 3.0, 3.1, 3.2, 3.2.1, 3.3, 3.4, 3.5, 4.0, 4.1, 4
.2, 4.3, 4.4, 4.5, 4.5.1, 4.6, 4.7, 4.8, 4.9, 4.9.1, 4.9.2, 4.10, 4.10.1, 4.10.2
, 4.11, 4.12, 5.0, 6.0, 6.1, 6.2, 6.2.1, 6.3, 7.0b1, 7.0b2, 7.0rc1, 7.0rc2, 7.0,
 7.1, 8.0, 8.0.1, 8.1, 8.1.1, 8.2, 9.0.dev0, 9.0, 9.1)
ERROR: No matching distribution found for pymdown-extensions>=9.4 (from mkdocs-m
aterial)
WARNING: You are using pip version 20.0.2; however, version 21.3.1 is available.
You should consider upgrading via the '/home/nsure/Projects/MkDocs/Solo/bin/pyth
on3 -m pip install --upgrade pip' command.

Apparently pip is not new enough. Against the same Python environment, upgrade pip using...

pip install --upgrade pip

This produces output resembling the following (later runthroughs likely showing higher version numbers),

Collecting pip
  Using cached pip-21.3.1-py3-none-any.whl (1.7 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 20.0.2
    Uninstalling pip-20.0.2:
      Successfully uninstalled pip-20.0.2
Successfully installed pip-21.3.1

The version of pip that the upgrade is starting out with is not familiar with the --no-cache-dir option.

Installation offlining

The single...

pip install mkdocs-material

...action will pull in a zillion separate Python packages, all with specific version numbers and depencies on specific versions of other packages. On a machine that happens to have its own connectivity to the public Internet.

When such a live pull-in is performed later, the version numbers of ‘whatever happens to be available in PyPI at the time’ are bound to be different. Which introduces the potential of the ‘uncertified other version’ behaving differently. Assuming that there will be a PyPI to begin with, and the necessary packages turn out to exist there.

Package identification

Before running pip install mkdocs-material (when it’s already too late, start over with a clean new virtual environment), run this command:

pip freeze >/tmp/before.txt

Only then run...

pip install --no-cache-dir mkdocs-material

This will generate a considerable amount of output, along the lines of...

Collecting mkdocs-material
  Downloading mkdocs_material-8.2.12-py2.py3-none-any.whl (5.1 MB)
     |████████████████████████████████| 5.1 MB 12.0 MB/s
Collecting mkdocs>=1.3.0
  Downloading mkdocs-1.3.1-py3-none-any.whl (3.7 MB)
     |████████████████████████████████| 3.7 MB 80.5 MB/s
Collecting mkdocs-material
  Downloading mkdocs_material-8.2.11-py2.py3-none-any.whl (5.1 MB)
     |████████████████████████████████| 5.1 MB 87.5 MB/s
Collecting jinja2>=2.11.1
  Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)
     |████████████████████████████████| 133 kB 92.3 MB/s
Collecting pymdown-extensions<9.4,>=9.0
  Downloading pymdown_extensions-9.1-py3-none-any.whl (215 kB)
     |████████████████████████████████| 215 kB 66.1 MB/s
Collecting mkdocs-material-extensions>=1.0.3
  Downloading mkdocs_material_extensions-1.0.3-py3-none-any.whl (8.1 kB)
Collecting pygments<2.12,>=2.10
  Downloading Pygments-2.11.2-py3-none-any.whl (1.1 MB)
     |████████████████████████████████| 1.1 MB 78.3 MB/s
Collecting markdown>=3.2
  Downloading Markdown-3.3.7-py3-none-any.whl (97 kB)
     |████████████████████████████████| 97 kB 79.3 MB/s
Collecting MarkupSafe>=2.0
  Downloading MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64
.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (30 kB)
Collecting importlib-metadata>=4.4
  Downloading importlib_metadata-4.8.3-py3-none-any.whl (17 kB)
Collecting ghp-import>=1.0
  Downloading ghp_import-2.1.0-py3-none-any.whl (11 kB)
Collecting pyyaml-env-tag>=0.1
  Downloading pyyaml_env_tag-0.1-py3-none-any.whl (3.9 kB)
Collecting PyYAML>=3.10
  Downloading PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64
.whl (677 kB)
     |████████████████████████████████| 677 kB 55.5 MB/s
Collecting mergedeep>=1.3.4
  Downloading mergedeep-1.3.4-py3-none-any.whl (6.4 kB)
Collecting watchdog>=2.0
  Downloading watchdog-2.3.1-py3-none-manylinux2014_x86_64.whl (80 kB)
     |████████████████████████████████| 80 kB 72.5 MB/s
Collecting packaging>=20.5
  Downloading packaging-21.3-py3-none-any.whl (40 kB)
     |████████████████████████████████| 40 kB 65.9 MB/s
Collecting click>=3.3
  Downloading click-8.0.4-py3-none-any.whl (97 kB)
     |████████████████████████████████| 97 kB 76.4 MB/s
Collecting python-dateutil>=2.8.1
  Downloading python_dateutil-2.9.0.post0-py2.py3-none-any.whl (229 kB)
     |████████████████████████████████| 229 kB 68.3 MB/s
Collecting zipp>=0.5
  Downloading zipp-3.6.0-py3-none-any.whl (5.3 kB)
Collecting typing-extensions>=3.6.4
  Downloading typing_extensions-4.1.1-py3-none-any.whl (26 kB)
Collecting pyparsing!=3.0.5,>=2.0.2
  Downloading pyparsing-3.1.4-py3-none-any.whl (104 kB)
     |████████████████████████████████| 104 kB 81.5 MB/s
Collecting six>=1.5
  Downloading six-1.17.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: zipp, typing-extensions, six, PyYAML, python-date
util, pyparsing, MarkupSafe, importlib-metadata, watchdog, pyyaml-env-tag, packa
ging, mergedeep, markdown, jinja2, ghp-import, click, pymdown-extensions, pygmen
ts, mkdocs-material-extensions, mkdocs, mkdocs-material
Successfully installed MarkupSafe-2.0.1 PyYAML-6.0.1 click-8.0.4 ghp-import-2.1.
0 importlib-metadata-4.8.3 jinja2-3.0.3 markdown-3.3.7 mergedeep-1.3.4 mkdocs-1.
3.1 mkdocs-material-8.2.11 mkdocs-material-extensions-1.0.3 packaging-21.3 pygme
nts-2.11.2 pymdown-extensions-9.1 pyparsing-3.1.4 python-dateutil-2.9.0.post0 py
yaml-env-tag-0.1 six-1.17.0 typing-extensions-4.1.1 watchdog-2.3.1 zipp-3.6.0

Run pip freeze again, now redirecting to a different file:

pip freeze >/tmp/after.txt

Have the difference calculated between the before and after freeze snapshots,

comm -13 <(sort /tmp/before.txt) <(sort /tmp/after.txt) >/tmp/fetched.txt

The fetched.txt file now contains the packages that have been retrieved in the fulfillment of the pip install command.

Isolated downloading

Create a temporary directory in which packages can be stored in ‘original download’ form,

mkdir --verbose /tmp/download

This should produce the following feedback,

mkdir: created directory '/tmp/download'

Issue a download command for the previously identified packages,

pip download --dest /tmp/download --no-cache-dir --requirement /tmp/fetched.txt

The command’s output looks familiar,

Collecting click==8.0.4
  Downloading click-8.0.4-py3-none-any.whl (97 kB)
     |████████████████████████████████| 97 kB 10.0 MB/s
Collecting ghp-import==2.1.0
  Downloading ghp_import-2.1.0-py3-none-any.whl (11 kB)
Collecting importlib-metadata==4.8.3
  Downloading importlib_metadata-4.8.3-py3-none-any.whl (17 kB)
Collecting Jinja2==3.0.3
  Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)
     |████████████████████████████████| 133 kB 14.2 MB/s
Collecting Markdown==3.3.7
  Downloading Markdown-3.3.7-py3-none-any.whl (97 kB)
     |████████████████████████████████| 97 kB 13.2 MB/s
Collecting MarkupSafe==2.0.1
  Downloading MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64
.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (30 kB)
Collecting mergedeep==1.3.4
  Downloading mergedeep-1.3.4-py3-none-any.whl (6.4 kB)
Collecting mkdocs==1.3.1
  Downloading mkdocs-1.3.1-py3-none-any.whl (3.7 MB)
     |████████████████████████████████| 3.7 MB 15.5 MB/s
Collecting mkdocs-material==8.2.11
  Downloading mkdocs_material-8.2.11-py2.py3-none-any.whl (5.1 MB)
     |████████████████████████████████| 5.1 MB 20.8 MB/s
Collecting mkdocs-material-extensions==1.0.3
  Downloading mkdocs_material_extensions-1.0.3-py3-none-any.whl (8.1 kB)
Collecting packaging==21.3
  Downloading packaging-21.3-py3-none-any.whl (40 kB)
     |████████████████████████████████| 40 kB 82.5 MB/s
Collecting Pygments==2.11.2
  Downloading Pygments-2.11.2-py3-none-any.whl (1.1 MB)
     |████████████████████████████████| 1.1 MB 44.4 MB/s
Collecting pymdown-extensions==9.1
  Downloading pymdown_extensions-9.1-py3-none-any.whl (215 kB)
     |████████████████████████████████| 215 kB 61.3 MB/s
Collecting pyparsing==3.1.4
  Downloading pyparsing-3.1.4-py3-none-any.whl (104 kB)
     |████████████████████████████████| 104 kB 23.0 MB/s
Collecting python-dateutil==2.9.0.post0
  Downloading python_dateutil-2.9.0.post0-py2.py3-none-any.whl (229 kB)
     |████████████████████████████████| 229 kB 35.6 MB/s
Collecting PyYAML==6.0.1
  Downloading PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64
.whl (677 kB)
     |████████████████████████████████| 677 kB 41.2 MB/s
Collecting pyyaml_env_tag==0.1
  Downloading pyyaml_env_tag-0.1-py3-none-any.whl (3.9 kB)
Collecting six==1.17.0
  Downloading six-1.17.0-py2.py3-none-any.whl (11 kB)
Collecting typing_extensions==4.1.1
  Downloading typing_extensions-4.1.1-py3-none-any.whl (26 kB)
Collecting watchdog==2.3.1
  Downloading watchdog-2.3.1-py3-none-manylinux2014_x86_64.whl (80 kB)
     |████████████████████████████████| 80 kB 38.9 MB/s
Collecting zipp==3.6.0
  Downloading zipp-3.6.0-py3-none-any.whl (5.3 kB)
Saved /tmp/download/click-8.0.4-py3-none-any.whl
Saved /tmp/download/ghp_import-2.1.0-py3-none-any.whl
Saved /tmp/download/importlib_metadata-4.8.3-py3-none-any.whl
Saved /tmp/download/Jinja2-3.0.3-py3-none-any.whl
Saved /tmp/download/Markdown-3.3.7-py3-none-any.whl
Saved /tmp/download/MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_
x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Saved /tmp/download/mergedeep-1.3.4-py3-none-any.whl
Saved /tmp/download/mkdocs-1.3.1-py3-none-any.whl
Saved /tmp/download/mkdocs_material-8.2.11-py2.py3-none-any.whl
Saved /tmp/download/mkdocs_material_extensions-1.0.3-py3-none-any.whl
Saved /tmp/download/packaging-21.3-py3-none-any.whl
Saved /tmp/download/Pygments-2.11.2-py3-none-any.whl
Saved /tmp/download/pymdown_extensions-9.1-py3-none-any.whl
Saved /tmp/download/pyparsing-3.1.4-py3-none-any.whl
Saved /tmp/download/python_dateutil-2.9.0.post0-py2.py3-none-any.whl
Saved /tmp/download/PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_
x86_64.whl
Saved /tmp/download/pyyaml_env_tag-0.1-py3-none-any.whl
Saved /tmp/download/six-1.17.0-py2.py3-none-any.whl
Saved /tmp/download/typing_extensions-4.1.1-py3-none-any.whl
Saved /tmp/download/watchdog-2.3.1-py3-none-manylinux2014_x86_64.whl
Saved /tmp/download/zipp-3.6.0-py3-none-any.whl
Successfully downloaded click ghp-import importlib-metadata Jinja2 Markdown Mark
upSafe mergedeep mkdocs mkdocs-material mkdocs-material-extensions packaging Pyg
ments pymdown-extensions pyparsing python-dateutil PyYAML pyyaml-env-tag six typ
ing-extensions watchdog zipp

Create a tarball of the downloaded content,

(cd /tmp/download;
    find . -print0 | cut --zero --characters=3- \
        | tar --create --owner=root:0 --group=root:0 \
            --directory /tmp/download --null --files-from -) \
    | xz -9 --extreme --verbose \
    > /tmp/download.tar.xz

Remove the download directory and the text files,

rm --recursive /tmp/{{before,after,fetched}.txt,download/}

Finally, transfer the proverbial /tmp/download.tar.xz file, presumably renaming it to something like /path/to/material-YYYYMMDD.tar.xz or something.

Offline installation

Create a temporary directory for previously downloaded Python packages,

mkdir --verbose /tmp/download

This should produce the following feedback,

mkdir: created directory '/tmp/download'

Extract a previously created ‘Material download’ tarball into the directory,

tar --extract --xz --directory /tmp/download \
    --file /path/to/material-YYYYMMDD.tar.xz

Against the right Python environment, install the packages from the local directory,

pip install --no-index --find-links /tmp/download/*.whl

For this particular install operation, no Internet connectivity is required. There is also no dependency on the newer version of pip that was needed for the live download, so the offline installation can take place against a totally empty new virtual environment.

The package installer generates output like the following,

Looking in links: /tmp/download/click-8.0.4-py3-none-any.whl
Processing /tmp/download/ghp_import-2.1.0-py3-none-any.whl
Processing /tmp/download/importlib_metadata-4.8.3-py3-none-any.whl
Processing /tmp/download/Jinja2-3.0.3-py3-none-any.whl
Processing /tmp/download/Markdown-3.3.7-py3-none-any.whl
Processing /tmp/download/MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_5_x86_64.manyli
nux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Processing /tmp/download/mergedeep-1.3.4-py3-none-any.whl
Processing /tmp/download/mkdocs-1.3.1-py3-none-any.whl
Processing /tmp/download/mkdocs_material-8.2.11-py2.py3-none-any.whl
Processing /tmp/download/mkdocs_material_extensions-1.0.3-py3-none-any.whl
Processing /tmp/download/packaging-21.3-py3-none-any.whl
Processing /tmp/download/Pygments-2.11.2-py3-none-any.whl
Processing /tmp/download/pymdown_extensions-9.1-py3-none-any.whl
Processing /tmp/download/pyparsing-3.1.4-py3-none-any.whl
Processing /tmp/download/python_dateutil-2.9.0.post0-py2.py3-none-any.whl
Processing /tmp/download/PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_x86_64.manylinux
2014_x86_64.whl
Processing /tmp/download/pyyaml_env_tag-0.1-py3-none-any.whl
Processing /tmp/download/six-1.17.0-py2.py3-none-any.whl
Processing /tmp/download/typing_extensions-4.1.1-py3-none-any.whl
Processing /tmp/download/watchdog-2.3.1-py3-none-manylinux2014_x86_64.whl
Processing /tmp/download/zipp-3.6.0-py3-none-any.whl
Processing /tmp/download/click-8.0.4-py3-none-any.whl
Installing collected packages: six, python-dateutil, ghp-import, typing-extensio
ns, zipp, importlib-metadata, MarkupSafe, Jinja2, Markdown, mergedeep, PyYAML, w
atchdog, pyparsing, packaging, pyyaml-env-tag, click, mkdocs, mkdocs-material-ex
tensions, pymdown-extensions, Pygments, mkdocs-material
Successfully installed Jinja2-3.0.3 Markdown-3.3.7 MarkupSafe-2.0.1 PyYAML-6.0.1
 Pygments-2.11.2 click-8.0.4 ghp-import-2.1.0 importlib-metadata-4.8.3 mergedeep
-1.3.4 mkdocs-1.3.1 mkdocs-material-8.2.11 mkdocs-material-extensions-1.0.3 pack
aging-21.3 pymdown-extensions-9.1 pyparsing-3.1.4 python-dateutil-2.9.0.post0 py
yaml-env-tag-0.1 six-1.17.0 typing-extensions-4.1.1 watchdog-2.3.1 zipp-3.6.0

At this point, the local filesystem directory that contains the directly readable content of the tarball can be removed,

rm --recursive /tmp/download/

Verification

After installation, a mkdocs executable is available in the targeted Python environment. A...

mkdocs --help

...command should produce output like this:

Usage: mkdocs [OPTIONS] COMMAND [ARGS]...

  MkDocs - Project documentation with Markdown.

Options:
  -V, --version  Show the version and exit.
  -q, --quiet    Silence warnings
  -v, --verbose  Enable verbose output
  -h, --help     Show this message and exit.

Commands:
  build      Build the MkDocs documentation
  gh-deploy  Deploy your documentation to GitHub Pages
  new        Create a new MkDocs project
  serve      Run the builtin development server