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

Uploaded CPython 3.7+Windows x86-64

mjml_python-1.3.7-cp37-abi3-musllinux_1_2_x86_64.whl (682.8 kB view details)

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

mjml_python-1.3.7-cp37-abi3-musllinux_1_2_aarch64.whl (765.5 kB view details)

Uploaded CPython 3.7+musllinux: musl 1.2+ ARM64

mjml_python-1.3.7-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (603.0 kB view details)

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

mjml_python-1.3.7-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (585.6 kB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ ARM64

mjml_python-1.3.7-cp37-abi3-macosx_11_0_arm64.whl (534.3 kB view details)

Uploaded CPython 3.7+macOS 11.0+ ARM64

mjml_python-1.3.7-cp37-abi3-macosx_10_12_x86_64.whl (559.6 kB view details)

Uploaded CPython 3.7+macOS 10.12+ x86-64

File details

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

File metadata

File hashes

Hashes for mjml_python-1.3.7-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 c7308410451f86d535069e29a3179fd7f8daf19d663b565ffa4560e9f2081d93
MD5 de5b5679324615e578532688e655a132
BLAKE2b-256 aaa9d461e5be6e31bc71b1d3f2b0dbcc16d81a02ccabab6cf77ba83d60824439

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mjml_python-1.3.7-cp37-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ce2511cd93b61f82c2f005b49045517e2d989064a30dc93f3b973ab04b7e7c3c
MD5 fbf056211cfddcdf9ea4a6f7e8e82eb1
BLAKE2b-256 229d0a579cd5d79eee11b6df6daf90bc013cdc25d6cd41b675efafb67ea9c7c3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mjml_python-1.3.7-cp37-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 bbdf9b12d486bee1a4f723cd38acf2cd5113a1170322ede0fdaa59575f5ab69b
MD5 046ad76330ddb9402ea655512edbda1c
BLAKE2b-256 13990fb42d473d264dab23d74af74ff3d783c0ffe1da0c307097926d83b1b89c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mjml_python-1.3.7-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3e60789e2e428181c403145ee7d245b7fdd27b03217a71185dcd85701d5ff559
MD5 73f2a827bfffefe92eb9a85dc5efe5df
BLAKE2b-256 4bc313ef13298fb00b722ba462ed06423399de43e2771fd2e34c30dfa4424706

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mjml_python-1.3.7-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 f3aaa5703bf446336e1335c3c10b949c38e8b42fa801adb552c09ca922885e9e
MD5 9cba1daf9f51a55177e47ed5e037df2e
BLAKE2b-256 8747e4a157b144431a4c21b4e74051b60aa04ab4ea68f0c8880178d5efe8044b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mjml_python-1.3.7-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4b14863717463d4525e4ada60b9903ce8407e9406735ce6c1de0cb1295f91b2a
MD5 47cdd9555534384250b8731de4edf617
BLAKE2b-256 d9ce5651d7e491645c7c9ceb116ec9d6a4cb2f58430cb9bc2047095aaa3cbf1e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mjml_python-1.3.7-cp37-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3438176332bf18a3869dadba2dd2c5864a9f6c35ce37f91d514fb6b90b5bf078
MD5 a9e5634d953b30fca3ba8a2a65a02094
BLAKE2b-256 b99478e36181eab438754a86a84140325c13916e32195fcf9292f8a921d34547

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