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.6-cp37-abi3-win_amd64.whl (422.4 kB view details)

Uploaded CPython 3.7+Windows x86-64

mjml_python-1.3.6-cp37-abi3-musllinux_1_2_x86_64.whl (680.0 kB view details)

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

mjml_python-1.3.6-cp37-abi3-musllinux_1_2_aarch64.whl (762.7 kB view details)

Uploaded CPython 3.7+musllinux: musl 1.2+ ARM64

mjml_python-1.3.6-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (601.6 kB view details)

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

mjml_python-1.3.6-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (585.1 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ ARM64

mjml_python-1.3.6-cp37-abi3-macosx_11_0_arm64.whl (531.9 kB view details)

Uploaded CPython 3.7+macOS 11.0+ ARM64

mjml_python-1.3.6-cp37-abi3-macosx_10_12_x86_64.whl (556.7 kB view details)

Uploaded CPython 3.7+macOS 10.12+ x86-64

File details

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

File metadata

File hashes

Hashes for mjml_python-1.3.6-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 8c964224df9b71a5e42009a8a44d1082bcbf0d5ef89a62e78ff3e43c9834afc4
MD5 30a477c7d2aa3302fad2b2a94a7cbfb8
BLAKE2b-256 b72ba98d1258fa411c46e54b77b40a4da89b066f95bc7696a547da7e4d4e7f1e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mjml_python-1.3.6-cp37-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e9ea68a44741f04038dd1fe7d28b78afe8147b0ead07f19ac7b6cc631e6fa1b9
MD5 d7e8c7a87e79251aef041910be03fdf6
BLAKE2b-256 4f920e1c1e1fc250e6c1023899b503264be62ec7cd876a931cd0c89ebe563b33

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mjml_python-1.3.6-cp37-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 a0111425459d2495af3a2d178d4532043a488477423c1f90e2b71158f7d7857c
MD5 73be4a957693d723908146f18940b6af
BLAKE2b-256 943b9792631d4b73e5459fa7f0f14ab837396504a87ffcd064f52030f57b09b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mjml_python-1.3.6-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 36bf8fcee7354bf227a8a81ba90c795fc3631ee10f7c546022475b365fcfe646
MD5 bc9441d3314b4a5ba8df1ac85301edbe
BLAKE2b-256 d918e9bce030b54482f4284d9cf1e23df412d2b01d10f44f06e3770128ce2eec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mjml_python-1.3.6-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 31dfa2cba779bef42dc4ea42c41a63ad8e30ae4fbb9949f3ea91b89a99a86f49
MD5 a4d15e72f3c3b5b115147d5bad5c7b0d
BLAKE2b-256 b4fa4589093eadf54f3417c35af14ec14d8c262730e13e3b070a4e32fae28808

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mjml_python-1.3.6-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b5a9cf4ef39018f0b417a0976fa1ac306ff08eff6c0d98e989eda25aca3b94b2
MD5 81269186f59bdfb9d67708cc7a28ff76
BLAKE2b-256 dfe5d6f085c1410173ec26b921985054bb3b8bf7275bea9b74904de1c19a47ff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mjml_python-1.3.6-cp37-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 adf7e6e081fc3c243ea02b24c9b92f4410f698240091225a0aad60e9fd826e47
MD5 c64d83c03d9fb57a1c384b5cef7f03a2
BLAKE2b-256 dc9d2da7771b0e5f45d9bbeb554562859d3dcd042a100f3ad8d245d48a1ed8e5

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