首页 > 编程语言 >mrml python 以及webassembly 实现简单说明

mrml python 以及webassembly 实现简单说明

时间:2023-12-28 13:22:07浏览次数:33  
标签:webassembly github bindgen python self wasm mrml

简单说明下mrml python 以及webassembly 的实现

python

python 是基于了pyo3,利用pyo3 提供的能力,暴露了python 模块

  • 参考处理
// 暴露的mrml 模块
#[pymodule]
#[pyo3(name = "mrml")]
fn register(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
    // mrml 方法的注册
    m.add_class::<NoopIncludeLoaderOptions>()?;
    m.add_class::<MemoryIncludeLoaderOptions>()?;
    m.add_class::<ParserOptions>()?;
    m.add_class::<RenderOptions>()?;
    m.add_function(wrap_pyfunction!(to_html, m)?)?;
    m.add_function(wrap_pyfunction!(noop_loader, m)?)?;
    m.add_function(wrap_pyfunction!(memory_loader, m)?)?;
    Ok(())
}
  • 基于maturinpip 的包构建
    基于了pyo3 提供的maturin 做为pip 的backend
    pyproject.toml
 
[build-system]
requires = ["maturin>=1,<2"]
build-backend = "maturin"
 
[project]
name = "mrml"
description = "A Python wrapper for MRML (Rust port of MJML)."
readme = "readme.md"
requires-python = ">=3.7"
classifiers = [
  "Programming Language :: Rust",
  "Programming Language :: Python :: Implementation :: CPython",
  "Programming Language :: Python :: Implementation :: PyPy",
]
 
[project.urls]
"Homepage" = "https://github.com/jdrouet/mrml"
"Bug Tracker" = "https://github.com/jdrouet/mrml/issues"

webassembly

webassembly 的构建使用了 wasm-bindgen,暴露了Engine 以及一些操作方法

  • 部分参考代码
#[derive(Debug, Default)]
#[wasm_bindgen]
pub struct Engine {
    parser: Rc<mrml::prelude::parser::ParserOptions>,
    #[cfg(feature = "async")]
    async_parser: Rc<mrml::prelude::parser::AsyncParserOptions>,
    render: mrml::prelude::render::RenderOptions,
}
 
#[wasm_bindgen]
impl Engine {
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        Self::default()
    }
 
    /// Defines the parsing options.
    #[allow(clippy::arc_with_non_send_sync)]
    #[wasm_bindgen(js_name = "setParserOptions")]
    pub fn set_parser_options(&mut self, value: ParserOptions) {
        self.parser = Rc::new(value.into());
    }
 
    /// Defines the async parsing options.
    #[cfg(feature = "async")]
    #[allow(clippy::arc_with_non_send_sync)]
    #[wasm_bindgen(js_name = "setAsyncParserOptions")]
    pub fn set_async_parser_options(&mut self, value: AsyncParserOptions) {
        self.async_parser = Rc::new(value.into());
    }
 
    /// Defines the rendering options.
    #[wasm_bindgen(js_name = "setRenderOptions")]
    pub fn set_render_options(&mut self, value: RenderOptions) {
        self.render = value.into();
    }
 
    /// Renders the mjml input into html.
    #[wasm_bindgen(js_name = "toHtml")]
    pub fn to_html(&self, input: &str) -> ToHtmlResult {
        match to_html(input, &self.parser, &self.render) {
            Ok(content) => ToHtmlResult::Success { content },
            Err(error) => ToHtmlResult::Error(error),
        }
    }
  • webassembly 的构建
    基于了wasm-pack
    参考命令
 
wasm-pack build --target nodejs --release

说明

mrml 对于python 以及webassembly 的支持都是基于了rust 的周边能力,集成起来还是比较方便的,对于mrml 构建相关的可以看看
github 代码中的github action workflow 配置

参考资料

https://github.com/jdrouet/mrml
https://pyo3.rs/
https://github.com/PyO3/pyo3
https://github.com/rustwasm/wasm-pack
https://rustwasm.github.io/wasm-pack/

标签:webassembly,github,bindgen,python,self,wasm,mrml
From: https://www.cnblogs.com/rongfengliang/p/17932490.html

相关文章

  • 简单记录下python视频提取语音,语音转文字(web版本)
    一、直接贴代码,有些离线文件需要下载,python依赖包也需要下载。#coding=utf-8fromflaskimportFlask,render_template_string,jsonify,requestfromflask_corsimportCORSfromtkinterimportfiledialogfrompydubimportAudioSegmentfromnoisereduceimportredu......
  • mrml mjml 的rust 实现
    mrml是mjml的rust实现,同时还提供了webassembly的支持,当然比较强大的是mrml同时也实现了pythonbindingwebassembly使用代码const{Engine}=require("mrml");constengine=newEngine();constresult=engine.toHtml("<mjml><mj-body>HelloWor......
  • vs code 运行python 项目问题
    1. 安装python、vscode;2. anaconda配置运行项目的虚拟环境;3. vscode打开运行项目文件夹;4 vscode安装python插件;   打开VScode编辑器,按下快捷键“Ctrl+Shift+P”,或者左下角图标 ,选择“CommandPalette”         调出全局设置搜索窗......
  • Boto3按名字搜索AWS Image并返回Image的相关参数 (Python)
    文章目录小结问题及解决参考小结本文记录使用Python脚本和Boto3按名字搜索AWSImage并返回AWSImage的相关参数。问题及解决记得操作之前拿到相应的权限:exportAWS_ACCESS_KEY_ID="xxxxxxxxxxxxxxxxxxxxxxxxxx"exportAWS_SECRET_ACCESS_KEY="yyyyyyyyyyyyyyyyyyyyyyyyyyyy"e......
  • 如何使用Python爬虫爬取电视剧数据
    要使用爬虫爬取电视剧数据,可以按照以下步骤进行:导入所需的库:使用Python的requests库进行网络请求,使用BeautifulSoup库进行HTML解析。importrequestsfrombs4importBeautifulSouphttp://www.jshk.com.cn/mb/reg.asp?kefu=xiaoding;//爬虫IP获取;发送网络请求并获取HTML页面:使用re......
  • python从网络摄像头获取rstp视频流并截取图片保存
    def get_img_from_camera_net(folder_path):    cap = cv2.VideoCapture("rtsp://admin:[email protected]/ch1/stream1")#获取网络摄像机        i = 1    while i<3:        ret,frame = cap.read()        cv2.imshow("capture......
  • Python消息队列之Huey
    缘起:之前在Python中使用最多的就是Celery,同样的在这次项目中使用了Celery+eventlet的方式,但是由于具体执行的逻辑是使用的异步编写的,当时就出现了一个问题,当使用httpx的AsyncClient发送一个网络请求的时候,发生了阻塞,导致整个程序无法完整执行.于是就找替代方案,于是......
  • 【python爬虫课程设计】实习僧——数据分析与可视化
    实习僧数据分析与可视化选题背景随着中国经济的不断发展,实习市场也变得日益重要。学生们在求学期间通过实习获取工作经验,而企业则通过实习生计划发现并培养潜在的人才。实习僧作为一家专注于实习和校园招聘的在线平台,收集了大量的实习相关数据。通过对实习僧的数据进行爬取和......
  • 【Python】键鼠操作、区域截图
    1.跟踪鼠标位置importtime,osimportpyautoguiaspagtry:whileTrue:print("按下Ctrl+C结束程序")x,y=pag.position()posStr="当前鼠标位置:"+str(x).rjust(4)+','+str(y).rjust(4)print(posStr)......
  • Python 虚拟环境工具及使用总结
    ​ 参考文档:Python虚拟环境工具及使用总结1、virtualenvvirtualenv是一个创建隔离的Python环境的工具。它可以创建一个包含指定版本Python解释器的环境,并可以安装独立的库和依赖。Python官方提供的虚拟环境工具。Virtualenv 的原理是基于Python的模块化机制,通过创建一......