Skip to main content

A Python wrapper for MRML (Rust port of MJML).

Project description

mjml-python

Compile MJML at runtime without an external Node service/process. It is a Python wrapper for MRML (Rust port of MJML).

Why

From MRML:

A Node.js server rendering an MJML template takes around 20 MB of RAM at startup and 130 MB under stress test. In Rust, less than 1.7 MB at startup and a bit less that 3 MB under stress test. The Rust version can also handle twice as many requests per second.

All of that is without considering http transaction cost when using a node service or process.

How

Install from PyPI:

pip install mjml-python

Import mjml2html and pass a string to compile:

from mjml import mjml2html

html = mjml2html(
    '''
    <mjml>
      <mj-body>
        <mj-section>
          <mj-column>
            <mj-image width="100px" src="/assets/img/logo-small.png"></mj-image>
            <mj-divider border-color="#F45E43"></mj-divider>
            <!-- Say hello to the user -->
            <mj-text font-size="20px" color="#F45E43" font-family="Open Sans">Hello World</mj-text>
          </mj-column>
        </mj-section>
         <mj-section>
          <mj-column>
            <mj-social font-size="15px" icon-size="30px" mode="horizontal">
              <mj-social-element name="facebook" href="https://mjml.io/">
                Facebook
              </mj-social-element>
              <mj-social-element name="google" href="https://mjml.io/">
                Google
              </mj-social-element>
              <mj-social-element  name="twitter" href="https://mjml.io/">
                Twitter
              </mj-social-element>
            </mj-social>
          </mj-column>
        </mj-section>
      </mj-body>
    </mjml>
    ''',
    disable_comments=True,
    social_icon_origin="https://example.com",
    fonts={
        "Open Sans": "https://fonts.googleapis.com/css?family=Open+Sans:300,400,500,700",
        "Ubuntu": "https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700",
    })

Example using Django templates

from django.core.mail import send_mail
from django.template.loader import render_to_string
from mjml import mjml2html

context = {'foo': 'bar'}
text_message = render_to_string('my_text_template.txt', context)
html_message = mjml2html(render_to_string('my_mjml_template.mjml', context))
send_mail(
    'Subject here',
    text_message,
    'from@example.com',
    ['to@example.com'],
    fail_silently=False,
    html_message=html_message,
)

Options

mjml-python supports the following options:

Name Type Default value Comment
disable_comments bool False Strip comments out of rendered HTML
social_icon_origin str | None None Custom URL origin for social icons. Icon name is appended (e.g. facebook.png).
fonts dict[str, str] | None None Fonts imported in the HTML rendered by MJML.
include_loader Callable[[str], str] | None None Fetch the included template using the path attribute.

Notes :

  • When fonts option is set to None, the following default fonts will be used:
    {
        "Open Sans": "https://fonts.googleapis.com/css?family=Open+Sans:300,400,500,700",
        "Droid Sans": "https://fonts.googleapis.com/css?family=Droid+Sans:300,400,500,700",
        "Lato": "https://fonts.googleapis.com/css?family=Lato:300,400,500,700",
        "Roboto": "https://fonts.googleapis.com/css?family=Roboto:300,400,500,700",
        "Ubuntu": "https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700",
    }       
    

Development

With Nix:

nix-shell

With Python 3.7+, Rust and Cargo installed:

python3 -m venv env
source env/bin/activate
pip install -r requirements.txt
maturin develop
python -m unittest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

mjml_python-1.3.4-cp37-abi3-win_amd64.whl (417.8 kB view details)

Uploaded CPython 3.7+ Windows x86-64

mjml_python-1.3.4-cp37-abi3-musllinux_1_2_x86_64.whl (655.0 kB view details)

Uploaded CPython 3.7+ musllinux: musl 1.2+ x86-64

mjml_python-1.3.4-cp37-abi3-musllinux_1_2_aarch64.whl (766.6 kB view details)

Uploaded CPython 3.7+ musllinux: musl 1.2+ ARM64

mjml_python-1.3.4-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (594.2 kB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

mjml_python-1.3.4-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (579.8 kB view details)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ ARM64

mjml_python-1.3.4-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (1.1 MB view details)

Uploaded CPython 3.7+ macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

File details

Details for the file mjml_python-1.3.4-cp37-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for mjml_python-1.3.4-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 7d85df5fa0fd431fb5519fc4adc90ce71a57272b0b9e99aae16cd11d6db4bd11
MD5 ebfc00b0f60be7217655b218324b680b
BLAKE2b-256 c91103c362f6e7c5d3b75dbb26748953b43ffe5ecfd42524b266978bb9269981

See more details on using hashes here.

File details

Details for the file mjml_python-1.3.4-cp37-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for mjml_python-1.3.4-cp37-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 1dd15993b709b49232488c543c6bdc2276a5b955ebe38dc95f9184abd622f5d8
MD5 a6a3821c121c7dc4eba71e02e4c83ded
BLAKE2b-256 cbeae72fdbafa66c625b1b86a0e157d2bdf1a83002261587bc76f3bd4b369c1d

See more details on using hashes here.

File details

Details for the file mjml_python-1.3.4-cp37-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for mjml_python-1.3.4-cp37-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 f4cab6a804a3a53a2390e70ce36ee81b8d8249a31c4f052295949fe7d2202b6d
MD5 c42570ef91d0069b0757b8f6c6f52d47
BLAKE2b-256 7912172e7b52f01ba0755744f26030fc2afe1b5070a728205fa54cb38e49b85a

See more details on using hashes here.

File details

Details for the file mjml_python-1.3.4-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mjml_python-1.3.4-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 534a68f368fae1acd9b9e186e8d0fbbe6971d294268e3694b216571ba6ed73b9
MD5 f06840dd57647c9841f76e9bd5e0fa8e
BLAKE2b-256 f00bce858e6bbde85d5d30aaacbc61a345bb14022747f070f5ba33256181986a

See more details on using hashes here.

File details

Details for the file mjml_python-1.3.4-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for mjml_python-1.3.4-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 90cb077f4a8af6f788567d52175cda69c5a88fb7e5864217d9af08569f8bee6f
MD5 1a563105b51a19db3de78023a972ff14
BLAKE2b-256 54544dad415318ca287bad1761a470f6bd2e7e20f91d42c84037f7723e11059f

See more details on using hashes here.

File details

Details for the file mjml_python-1.3.4-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for mjml_python-1.3.4-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 45dfb2fe3cc14d56cd72473d403e184557d43ddfe5740559f31dc8139b32ecfd
MD5 16be6e9c157fa88e86dd4532d23937c5
BLAKE2b-256 fe91353f00d6797d438470d0e64d4b67af8a7df5c9fb2283df7a72160f2f6c42

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