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

If you're not sure about the file name format, learn more about wheel file names.

mjml_python-1.3.5-cp37-abi3-win_amd64.whl (431.0 kB view details)

Uploaded CPython 3.7+Windows x86-64

mjml_python-1.3.5-cp37-abi3-musllinux_1_2_x86_64.whl (690.3 kB view details)

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

mjml_python-1.3.5-cp37-abi3-musllinux_1_2_aarch64.whl (771.0 kB view details)

Uploaded CPython 3.7+musllinux: musl 1.2+ ARM64

mjml_python-1.3.5-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (611.5 kB view details)

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

mjml_python-1.3.5-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (593.8 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ ARM64

mjml_python-1.3.5-cp37-abi3-macosx_11_0_arm64.whl (541.3 kB view details)

Uploaded CPython 3.7+macOS 11.0+ ARM64

mjml_python-1.3.5-cp37-abi3-macosx_10_12_x86_64.whl (562.1 kB view details)

Uploaded CPython 3.7+macOS 10.12+ x86-64

File details

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

File metadata

File hashes

Hashes for mjml_python-1.3.5-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 0bc1098bf5939a6b729b433e07c479ca8c4c2ad37f2dc4071a49b2e5e6b248ea
MD5 6fcb3747be58f6081785d83d54d7118e
BLAKE2b-256 e600247c090dfb5b85e4cfdeaa55cc5b16f3782c7e2135f22495accb5ce1be29

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mjml_python-1.3.5-cp37-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 60c46ca5ed5ae218805ff88f048428dc3a6568748a6abb25a028428ee3e27fc9
MD5 0e1d1015229f8e657e42ef91ff039338
BLAKE2b-256 f124bbee29182103421af2b003133e70617623c8c99d79f0a86fea1f80f65644

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mjml_python-1.3.5-cp37-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 193298d33211d617deebef9d2c869eaefa5866998bd777dd835cb7305aee41aa
MD5 878498012fc669b83decddc5f9568a0a
BLAKE2b-256 1fd06f7f0d4039e823278692b5d8be1e8067f1f613bde8ea293dc86e14361ff0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mjml_python-1.3.5-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4f06c4122263082cb92c5f725e2a60efe93a56d9ae85cabb7701ed022c7363c6
MD5 7b9e6539f1de30ee5a31a14b46defe68
BLAKE2b-256 65f789f90e016a312395e2e847e4ae538284d8068bcd5a484b421aa632f49cfd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mjml_python-1.3.5-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 995ab02019885440325919b0f70ea85cdbf5fe09d768d2ad8dccf8ddfa6f3bb2
MD5 0c4bf7abfe8d41dcc58dabe1c4993f31
BLAKE2b-256 34bc75bdf4c90e5a04417287ee586132bb811cbe6a9da838aa494b4ceae54305

See more details on using hashes here.

File details

Details for the file mjml_python-1.3.5-cp37-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for mjml_python-1.3.5-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9c5852b39d87e762c6c5e1d34f14953f16c943e10b1e632c1b482b5cb978823a
MD5 13d0e306e19e9551e75a807aa0756f0d
BLAKE2b-256 083cc157812c259e5ce3d967a983077f6996c4c23ab41d27f91be6698a4d1afb

See more details on using hashes here.

File details

Details for the file mjml_python-1.3.5-cp37-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for mjml_python-1.3.5-cp37-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ab7ae1196818a8961acc60bfeaf8e0dae59d1ac3ced0fade37ce7f1c8a34342a
MD5 8f6c59584a32ea10a28189ebc971acf9
BLAKE2b-256 c7987fe276c1843f04676e79d60b7fb034d403a7a4b55d3e2cc9aaa1cf1407c7

See more details on using hashes here.

Supported by

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