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.2-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6eed115c551af01df5c239ae2d8326cd9020c91d00d64bb0e82edde93a7724be |
|
MD5 | 973bd4cfa120999d027bc47bff4ebab7 |
|
BLAKE2b-256 | c6ea190b8b800b2e6066e6037cd54342f57787dbcb183d6c57f15a2e21c242bb |
Hashes for mjml_python-1.3.2-cp37-abi3-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8757675c3b4065a0aa13d993c645b1fdedf30f14559a6f6468eb69b572f2e02b |
|
MD5 | a268db19bdc257e3b8e4e00df4a1876f |
|
BLAKE2b-256 | a24484094791d455d5b1599943b4c54d41ccf0e3dda88cf595cc4b7565667553 |
Hashes for mjml_python-1.3.2-cp37-abi3-musllinux_1_2_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 511190d77dabfc60a8a815231c2691a9a0cf0b0a1ab8386195daaca976c54dca |
|
MD5 | c18299df5f0a7ab07c01e4a50f45d3aa |
|
BLAKE2b-256 | 98c39558a814c2b2f4dd930133626834a0b1bc2bd9f3841a4b7c661eb2be8704 |
Hashes for mjml_python-1.3.2-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b560b5637a379dd04fa08686cc0e25b39e1a8b9a87f9194b5340ef11ac9d1a0 |
|
MD5 | d8495067d40a17983fa54edc1a8c2573 |
|
BLAKE2b-256 | 72ce620b8447cea5cf479d3b668f3ad6d576dd242d3ce12cb9a45a2156e20f11 |
Hashes for mjml_python-1.3.2-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f56cc3188bbd5a30f8387d0149237acb907b0622fd1267f51b69c9990a0db8e |
|
MD5 | 250805792ac6f5cea1f21def118e03f5 |
|
BLAKE2b-256 | 49ef05184a36b216eacfb1e41eec70aa0dc0d083043b9349854b4977cd7235d1 |
Hashes for mjml_python-1.3.2-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b3d71e08b122aa50cd02d411c9583bb73355c90d8c4c699b4cbd6b97bc81c11 |
|
MD5 | 69af97605509e59c620446c957e1dc2e |
|
BLAKE2b-256 | 71d9a7afa4ca118258beb7f3f13260204528e639a2d37ed4aca9402a1875a608 |