Skip to main content

Make better chemistry documentation!

Project description

Author:

Lukas Turcani

Introduction

moldoc is a Sphinx extension for making better chemistry documentation. It allows you to embed 3D, interactive models of molecules directly into your compiled docs. You can see it being used in the stk docs.

moldoc.gif

Installation

First, run

pip install moldoc

and then add it to your extensions in conf.py

extensions = [
    'moldoc',
]

Adding Molecules into Your Docs

You can define molecules you show with the moldoc directive, which you can place it into your rst files

.. moldoc::

    # The content of a moldoc directive is just a Python script
    # which needs to define a moldoc_display_molecule variable.

    import moldoc.molecule as molecule

    moldoc_display_molecule = molecule.Molecule(
        atoms=(
            # molecule.Atom(atomic_number, position)
            molecule.Atom(6, (-0.06, -0.17, 0.)),
            molecule.Atom(17, (-1.35, 1.04, -0.04)),
            molecule.Atom(35, (1.65, 0.73, -0.06)),
            molecule.Atom(1, (-0.15, -0.88, -0.87)),
            molecule.Atom(1, (-0.09, -0.72, 0.97)),
        ),
        bonds=(
            # molecule.Bond(atom1_id, atom2_id, order)
            molecule.Bond(0, 1, 1),
            molecule.Bond(0, 2, 1),
            molecule.Bond(0, 3, 1),
            molecule.Bond(0, 4, 1),
        ),
    )

or in your Python docstrings

def some_fn():
    """
    Do something.

    .. moldoc::

        # The content of a moldoc directive is just a Python script
        # which needs to define a moldoc_display_molecule variable.

        import moldoc.molecule as molecule

        moldoc_display_molecule = molecule.Molecule(
            atoms=(
                # molecule.Atom(atomic_number, position)
                molecule.Atom(6, (-0.06, -0.17, 0.)),
                molecule.Atom(17, (-1.35, 1.04, -0.04)),
                molecule.Atom(35, (1.65, 0.73, -0.06)),
                molecule.Atom(1, (-0.15, -0.88, -0.87)),
                molecule.Atom(1, (-0.09, -0.72, 0.97)),
            ),
            bonds=(
                # molecule.Bond(atom1_id, atom2_id, order)
                molecule.Bond(0, 1, 1),
                molecule.Bond(0, 2, 1),
                molecule.Bond(0, 3, 1),
                molecule.Bond(0, 4, 1),
            ),
        )

    """

    print('In some_fn()')

Note that the content in the moldoc directive is a just a Python script, which has to define a moldoc_display_molecule variable holding a moldoc.molecule.Molecule instance.

Because the content of a moldoc directive is just a Python script you can define your molecules programatically

def some_fn():
    """
    Do something.

    .. moldoc::

        # The content of a moldoc directive is just a Python script
        # which needs to define a moldoc_display_molecule variable.

        import moldoc.molecule as molecule

        atoms = [molecule.Atom(6, (i, 0., 0.)) for i in range(10)]
        bonds = [molecule.Bond(i-1, i, 1) for i in range(1, 10)]

        moldoc_display_molecule = molecule.Molecule(
            atoms=atoms,
            bonds=bonds,
        )

    """

    print('In some_fn()')

Configuration

Global

You can use the moldoc_default_molecule_config to set the default MoleculeConfig value for all renderings. This is defined in conf.py:

import moldoc.molecule as molecule
moldoc_default_molecule_config = molecule.MoleculeConfig(
    background_color=molecule.Color(32, 32, 32),
)

Local

The display of molecules is pretty configurable, here is a snapshot of the different configuration options you have, but note that this is not an exhaustive list

configuration.jpg

Configuration happens on both the molecule and the atom level. For example

.. moldoc::

    # The content of a moldoc directive is just a Python script
    # which needs to define a moldoc_display_molecule variable.

    import moldoc.molecule as molecule

    atoms = [
        molecule.Atom(
            atomic_number=6,
            position=(i, 0., 0.),
            # Configure the atom size and color.
            config=molecule.AtomConfig(
                color=molecule.Color(
                    red=255,
                    green=0,
                    blue=0,
                ),
                size=1.2,
            ),
        ) for i in range(10),
    ]
    bonds = [molecule.Bond(i-1, i, 1) for i in range(1, 10)]

    moldoc_display_molecule = molecule.Molecule(
        atoms=atoms,
        bonds=bonds,
        config=molecule.MoleculeConfig(
            atom_scale=1,
            material=molecule.MeshStandardMaterial(),
            background_color=molecule.Color(0, 255, 0),
            is_outlined=False,
        ),
    )

Note that there are many materials to choose from, and that each has its own set of configuration options. You can see the materials and their configuration options in src/moldoc/molecule.py. Note that the materials correspond to classes in THREE.js, for example https://threejs.org/docs/#api/en/materials/MeshStandardMaterial, so if you wish to understand the configuration options of each material the THREE.js docs are the place to look. Most should be straighforward to understand from the name however.

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

moldoc-3.0.5.tar.gz (1.6 MB view details)

Uploaded Source

Built Distribution

moldoc-3.0.5-py3-none-any.whl (219.8 kB view details)

Uploaded Python 3

File details

Details for the file moldoc-3.0.5.tar.gz.

File metadata

  • Download URL: moldoc-3.0.5.tar.gz
  • Upload date:
  • Size: 1.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for moldoc-3.0.5.tar.gz
Algorithm Hash digest
SHA256 9c929e6671c5daa1b924e89bb58dbc0462bf12c4050df903ff048d5adcf7f093
MD5 344b1a394b48223d1f33a870811e3ba3
BLAKE2b-256 8e6a734147c0fe968025e35a4497509ad259f7780a0700c6300a2e748948518e

See more details on using hashes here.

File details

Details for the file moldoc-3.0.5-py3-none-any.whl.

File metadata

  • Download URL: moldoc-3.0.5-py3-none-any.whl
  • Upload date:
  • Size: 219.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for moldoc-3.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 d7ab5e3968972fe9083c3cc74d764a4e9868b76b82f6104a4c5c6a15e613a895
MD5 b8d1a11c007c76081ed9b12dcb662206
BLAKE2b-256 087a37b5830072adb3b8f3c6de3e0313e7f15db98a3354a4a7c8bb22c3d7b3f4

See more details on using hashes here.

Supported by

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