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.0-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e5719992eeed2a3bc98a50a84b63ee5fd959f87d15a269a2c659e1febea9c98 |
|
MD5 | 147c5f036d75cb525862327d1bee0097 |
|
BLAKE2b-256 | 92b5a12293ba8d2bbde281f305a9f18fa8aeb0845170ea46dcb7922a1d6178e0 |
Hashes for mjml_python-1.3.0-cp37-abi3-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b53c35dde112c86223441d930cfb7ebdd18918f13237ea442867e777c5cb93f9 |
|
MD5 | ddb6f3834d42dd3df478c2243f31f6c3 |
|
BLAKE2b-256 | 12cb5296a8c091ad938924668e04d8be6403ec06f8f632f6ff3d705f1cba5009 |
Hashes for mjml_python-1.3.0-cp37-abi3-musllinux_1_2_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52f16128be27b462eefd3fd99dd2462fce977509ae43485bc8f8060d86511ff8 |
|
MD5 | c619355b38dba103ab5ea4dee1d2ecdb |
|
BLAKE2b-256 | 4bd151e77fa9404d544753ae56f7b3c9d9213eeaed1174c70f6caaaf349cb9ad |
Hashes for mjml_python-1.3.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d923d77833e8b1f1db2197b22ac6bae7abcd9d68660b711765862f4ab4681b9 |
|
MD5 | 7672d024b8e079954258e92abd5eddd7 |
|
BLAKE2b-256 | e6040a2fee764666c0ca8806a521b1ab3d47ba62c45c72e52af7c69f5cc58e43 |
Hashes for mjml_python-1.3.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c943791cda2895258b1991cf16769b75279534d8b8a3b44353ae4c338ec36c8e |
|
MD5 | 9ee6c14cb32c92960ed49df22f16cb4e |
|
BLAKE2b-256 | b71dca0c7f25dd68d788d2fb2d994b75e68fa74a290835ed179487714128efba |
Hashes for mjml_python-1.3.0-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7acafd604c4de62f63b847f9dfacd5517be01c04d62e4443e9d622a28af5b28e |
|
MD5 | 1c9eba71065b81ff804eab1e4a86ae19 |
|
BLAKE2b-256 | e41122ae74096056d1dbbec2300f2bce214015a9509f04b7ef17a446d005b1d1 |