Documentation¤
The documentation for mplhep is built using MkDocs with the Material for MkDocs theme.
To build the documentation locally, run the following command from the root directory of the repository:
With MkDocs:
Additional requirements for building the docs are listed in new_docs/requirements_docs.txt and can be installed via:
mkdocs build # Builds the documentation into the site/ directory
mkdocs serve # Serves the documentation locally
With Nox:
nox -s docs # Builds the documentation into the site/ directory
nox -s docs -- --fast # Builds the documentation quickly without rendering code blocks (much faster building time)
nox -s docs -- --serve (--fast) # Serves the documentation locally
Style-Specific Guide Generation¤
This directory contains an automated system for generating the documentation files with tabs for each experiment style from a template.
Overview¤
Instead of manually maintaining separate guide files for each experiment style or manually maintaining one file with all the tabs, we use a template-based approach:
- Template Files:
docs/*_template.mdcontains the contents with tab placeholders ({{TABS_START}}/{{TABS_END}}) - Generation Script:
generate_style_guides.pyexpands tabs with style-specific code for each experiment - MkDocs Hook:
docs_hooks.pyautomatically runs the script before each build - Generated Files:
docs/*.mdwithout the_template, with tabs for CMS, ATLAS, LHCb, ALICE, DUNE, and plothist
Files¤
docs/*_template.md- Templates with{{TABS_START}}/{{TABS_END}}markersgenerate_style_guides.py- Script to generate guide.md with experiment tabsdocs_hooks.py- MkDocs hook that runs the generation script automatically
How It Works¤
Template Structure¤
The template uses special markers to denote tabbed sections:
{{TABS_START}}
{{TAB_HEADER}}
```python
# mkdocs: render
import mplhep as mh
mh.style.use('{{STYLE_CODE}}')
# ... code ...
{{LABEL_CODE}}
{{MAGIC_CODE}} ax.legend()
```
{{TABS_END}}
This generates tabs for all 6 styles (CMS, ATLAS, LHCb, ALICE, DUNE, plothist).
Placeholders¤
Within tabbed sections, these placeholders get replaced per style:
{{STYLE_NAME}}- Full style name (e.g., "CMS", "ATLAS"){{STYLE_CODE}}- Style code formh.style.use()(e.g., "CMS", "LHCb2"){{LABEL_CODE}}- Label code with all parameters{{LABEL_CODE_NODATA}}- Label code without data parameter{{LABEL_CODE_DATA}}- Label code with data=True{{LABEL_CODE_AX}}- Label code with ax parameter{{LABEL_CODE_LUMI}}- Label code with luminosity{{MAGIC_CODE}}- mpl_magic() call if needed (ATLAS, ALICE only)
Generation Script¤
Run manually:
# Generate guide.md with tabs
python generate_style_guides.py
# Custom paths
python generate_style_guides.py --template my_template.md --output my_output/guide.md
Automatic Generation¤
The MkDocs hook automatically runs the generation script before every build:
With MkDocs:
With Nox:
nox -s docs # Guide is generated automatically
nox -s docs -- --serve # Guide is generated on startup
Making Changes¤
To update the templates:¤
- Edit
docs/*_template.md - For content that should be the same across all styles, edit it directly
- For content that varies by style, use
{{TABS_START}}...{{TABS_END}}markers - Test generation:
python generate_style_guides.py - Build docs via MkDocs or Nox to see changes
docs/*.mdwithout the_templatefiles will be regenerated automatically
To add a new style:¤
- Add style configuration to
generate_style_guides.pyin theSTYLESdict - Add the style name to
STYLE_ORDERlist - Run the generation script
To modify style-specific code:¤
- Edit the appropriate entry in the
STYLESdict ingenerate_style_guides.py - Regenerate:
python generate_style_guides.py
Example Template Usage¤
#### Simple Example
This shows a basic histogram with different styles:
{{TABS_START}}
{{TAB_HEADER}}
```python
# mkdocs: render
import mplhep as mh
mh.style.use('{{STYLE_CODE}}')
# Create plot
{{LABEL_CODE}}
{{MAGIC_CODE}} plt.show()
```
{{TABS_END}}
This generates:
=== "CMS"
```python
# mkdocs: render
import mplhep as mh
mh.style.use('CMS')
# Create plot
mh.cms.label('Preliminary', data=True, lumi=100, com=13)
plt.show()
```
=== "ATLAS"
```python
# mkdocs: render
import mplhep as mh
mh.style.use('ATLAS')
# Create plot
mh.atlas.label('Internal', data=True, lumi=150, com=13)
mh.mpl_magic(soft_fail=True)
plt.show()
```
...
Benefits¤
- Single Source of Truth: Edit one template, update all style tabs
- Consistency: All style tabs have identical structure and examples
- Easy Maintenance: No need to manually sync changes across tabs
- Automatic: Integrated into the build process via MkDocs hook
- Flexible: Easy to add new styles or modify existing ones
- Clean: Template is much simpler than manually maintaining all tabs
Troubleshooting¤
Guide not updating¤
Make sure you're editing docs/*_template.md.
Build errors¤
Check that all placeholders in the template are defined in the STYLES dict in generate_style_guides.py.
Missing mpl_magic calls¤
ATLAS and ALICE styles need mh.mpl_magic(soft_fail=True) after labels. This is handled automatically via the MAGIC_CODE placeholder which includes proper indentation.
Testing changes¤
Always test locally before committing:
python generate_style_guides.py
mkdocs build / nox -s docs
# Check the generated file
less docs/guide.md
Manual regeneration¤
If the hook doesn't run for some reason: