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 toNone
, 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
python -m venv env
source env/bin/activate
pip install -r requirements.txt
maturin develop
python -m unittest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for mjml_python-1.3.3-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 321ad94a7ef39902d69bce4f090ba5b4c1a9277ee759bdd3ce0d56bc6404d26d |
|
MD5 | 0f2a10ed79373f3b8eb7dda9e5f3b021 |
|
BLAKE2b-256 | 0970db35c0e602950874cc57f316556d03cf8eb974fb0d5205e71955ed7f6b33 |
Hashes for mjml_python-1.3.3-cp37-abi3-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99c34ce7138d2400ec889d5fc263add31a9b60ce9673790f4c6a2db4563b0dcb |
|
MD5 | 64f434c9d8f8116fe94e0223fa9592bf |
|
BLAKE2b-256 | a350b2ead262e56d874542c585c9f32af6ef65d28b4ae2b2cdc2c68e64061fb1 |
Hashes for mjml_python-1.3.3-cp37-abi3-musllinux_1_2_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24083860fd3d6d78bff37d73f1fcf8f1d60c2eeb348bea151c262e60e2d68d95 |
|
MD5 | 70a2e18fa4d56c7c0a19e78c2733141b |
|
BLAKE2b-256 | e72084eaf4400e4876663396ef9b75ddee7955255019111cbd8e0cc3055ee51a |
Hashes for mjml_python-1.3.3-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4fa9a4eca964889a3d3816750cba92a91abc852f17d8fd9cc3c63765abaf970 |
|
MD5 | c5e151d10afbac67c9cce26e079b2ee6 |
|
BLAKE2b-256 | bfbdee819cc12c54c68412c53a3f2cb1855fda576e9f0b86aba3b39bb48efab0 |
Hashes for mjml_python-1.3.3-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e8193ed7ac803bfc83b4452dc3dbf8adb017dbf3297dbcba232b4ee7011cb73 |
|
MD5 | 931966bde78c73effdb19c0263ae64f3 |
|
BLAKE2b-256 | 90e988d8b0b89d0cb8ee36abf227c5bb42546f70ec19602e83a740a056c4f729 |
Hashes for mjml_python-1.3.3-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3583a6d7cbc66edfd2e8a71a85f2452ee10f028ba660b1c61e7acd90cfe0e4c4 |
|
MD5 | 90d94c1a5fecc3d442d36033e5e37077 |
|
BLAKE2b-256 | 45e9f852ff0053865ec0c7a30131ca5e9ba6ff1f0628acf0715482cfd3a2b387 |