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. |
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
. env/bin/activate
pip install -r requirements.txt
maturin develop
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.2.4-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 830e4bb070eecd86c91dfe6ad4fc95b1d4b0cf0b352c1ba37813973788a2bfa7 |
|
MD5 | e4a952c3f32bc28fc56b94e4336c241c |
|
BLAKE2b-256 | 32ee061b1ca919c18c576bb2d4e146187c247e7210c109ed22f843a917332dc8 |
Hashes for mjml_python-1.2.4-cp37-abi3-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75bb749d1f566c96e99df7adf9dc3440da7de90de891b6e5f74ebf13e60dfc56 |
|
MD5 | d9ef9fab6e09b6741ece96cc67cc1240 |
|
BLAKE2b-256 | 28c493111d570002cea471dcecb733445ba68ba29d7617ec7800a8c6ab9faf60 |
Hashes for mjml_python-1.2.4-cp37-abi3-musllinux_1_2_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68afb27f639b863e1003e16cffb0baae1cd51f0c754ffdec41a9124329c94433 |
|
MD5 | 4029fabbd30416822355244e81009928 |
|
BLAKE2b-256 | ec52d4b645e6c402e0e6c59234fe7f72092b4aa469f8c1cd0af0f7a7f351605f |
Hashes for mjml_python-1.2.4-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1882ac36e8473cffa5ad6cf5f41f54ab2292f7cedea39cfb37730566be6a55b6 |
|
MD5 | e38fd8e05a490d850e0ee950f8cc13a8 |
|
BLAKE2b-256 | b8dc336640ad24de1f37bcdc88df5bd52ca213f7347f8d302ac738906e8de87f |
Hashes for mjml_python-1.2.4-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | abc06b70cebab2459b59c9b96a6b05c3a0592aa1c635c19b0e3dd6375062c3a7 |
|
MD5 | ef082af68f924a0fc3137936e071bd40 |
|
BLAKE2b-256 | c0c21a543c53fe4c8aaf9cfdc332d4a54cc407432103b1c3d9f870289974bcb4 |
Hashes for mjml_python-1.2.4-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d609fa96702dd5b8acbc80f3b1e66b509aa971a1ad03bde7e189dbf4be543bbc |
|
MD5 | c88da6cfe5cb32604dc7d270099a32c8 |
|
BLAKE2b-256 | 4d57598f42fa6a8cafac39ecb71bf78e70f5db307787a16f1a1e9e7f1a96269a |