Skip to main content

Merkury

Merkury is a command line utility to run Python scripts and render static HTML or Markdown reports. It uses standard .py files as input - any valid script that can be run from command line, can also be turned into a report.

It's a lightweight alternative to tools such as jupyter/papermill and can be used as a BI-as-code solution with Python-based workflow.

Non-goals of the project:

  • interactive code execution in the browser (see jupyter),
  • generating data apps that require backend server (see e.g. dash),
  • converting any input into static HTML (see e.g. nikola).

Installation

pip3 install merkury

# if you also need server mode
pip3 install merkury[server]

There is also docker image available:

# default command runs in server mode
podman run \
    --rm \
    -p 8000:8000 \
    -v path/to/script/dir:/etc/merkury/scripts \
    ghcr.io/ppatrzyk/merkury:v0.15

Usage

Merkury can run in the following modes:

Single file

merkury -f html -o report.html myscript.py

Batch

Runs concurrently all python scripts inside given directory and produces report for each one.

merkury -f html -o path/to/reports batch path/to/scripts

Server

Starts server that exposes execution endpoints for each script in provided directory.

merkury -s localhost:8000 server path/to/scripts

With this configuration, there is:

  • Homepage with script list at /,
  • For each script (e.g., s.py):
    • Execution endpoint to run it (and refresh report html) at /execute/s,
    • Report endpoint to view latest report at /read/s.

Arguments to script can be passed via query params. Calling /execute/s?arg=mycommand&arg=123 is equivalent to running python3 s.py mycommand 123.

If a script reads data from stdin, it can be passed via body in POST request:

curl \
    -X POST \
    -d 'in data' \
    http://localhost:8000/execute/s

Options

$ merkury -h
merkury

Usage:
    merkury [options] server <dir_path>
    merkury [options] batch <dir_path> [ARGS...]
    merkury [options] <script_path> [ARGS...]

Options:
    -h --help                         Show this screen.
    -o <file>, --output <file>        Specify report file (if missing, <script_name>.<format>).
    -f <format>, --format <format>    Specify report format: html (default), md.
    -a <author>, --author <author>    Specify author (if missing, user name).
    -t <title>, --title <title>       Specify report title (if missing, script file name).
    -p <count>, --parallel <count>    Parallel processes (if missing, cpu cores).
    -s <address>, --server <address>  Server address (if missing, localhost:8000).
    -d, --timestamp                   Add timestamp to default report file name.
    -i, --no-input                    Hide input blocks in generated report.
    -c, --toc                         Generate Table of Contents.
    -l, --debug                       Print debug messages.
    -v, --version                     Show version and exit.

Source:
    https://github.com/ppatrzyk/merkury

Formatting

Formatting of output inside report is controlled by inserting magic comments inside input script. There are following optional directives:

By default merkury treats any output as standard code print and puts it into <code> blocks. Only If your output is actually HTML or Markdown, you need to indicate that by placing a magic comment inside a code chunk.

HTML

You need to put a comment #HTML after a line that outputs raw HTML. For example:

print(pandas_df.to_html(border=0))
# HTML

In addition to writing HTML by hand or using libraries that allow formatting output as HTML, merkury provides utility functions to format plots from common libraries. See plotting docs for details.

Markdown

It's also possible to render text formatted in markdown. You need to put magic comment #MARKDOWN after print statement.

For example:

print("""
# I'm a markdown header

List:

* l1
* l2

""")
# MARKDOWN

Title

In produced report, code will be broken into sections. Each section ends with a statement printing some output (e.g., print()). You can optionally give titles to each section by placing magic comment #TITLE <your_section_title> inside code chunk.

PDF reports

It is also possible to obtain PDF reports with usage of additional conversion tools (e.g., pandoc). For example:

merkury -o /dev/stdout -f md <your_script> | pandoc --highlight-style=tango -t pdf -o report.pdf

Note, in case your report file contains raw html chunks (such as plots or images), you will need use wkhtmltopdf pdf engine.

DEV

dev installation:

pip3 install -e .[dev,server]

docker build:

podman build -t ghcr.io/ppatrzyk/merkury:dev .

docs:

merkury --no-input docs/index.py
merkury --toc docs/intro-py.py
merkury docs/plotting.py

Acknowledgements

Metadata

Release files for merkury 0.15

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for merkury 0.15
File Size Uploaded Provenance Actions
merkury-0.15.tar.gz 37.7 kB Without provenance Details for merkury-0.15.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for merkury 0.15
File Interpreter ABI Platform Actions
merkury-0.15-py3-none-any.whl Python 3 none any Details for merkury-0.15-py3-none-any.whl

Total release size: 74.0 kB

Release files / merkury-0.15.tar.gz

Download URL merkury-0.15.tar.gz
Size 37.7 kB
Tags Source
SHA-256 checksum
How to use checksums
3562fc63642279280a29875274d6efaa25f5382ad84216742c1a91311fb0212f
BLAKE2b-256 checksum
How to use checksums
8a99dd14f04a925e759614696c01abf5fd8faac6676a0d9e75336e5ce2beb7a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.0

Release files / merkury-0.15-py3-none-any.whl

Download URL merkury-0.15-py3-none-any.whl
Size 36.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
755066e228dbc71d402f304b26a404628595923cf2416c5590067188b6e41662
BLAKE2b-256 checksum
How to use checksums
7509d2e1c4017d602ccb6602b775338ab6d6ae525d9bb191e002a53f4362650d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.0

Security for merkury 0.15

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page