基于texlive定制chemfig化学式转换Python服务镜像

基于texlive定制chemfig化学式转换Python服务镜像

chemfig

据别人介绍,在绘制平面分子式,乃至化学反应式、机理图时,大家使用的基本都是ChemDraw。当然ChemDraw是一款强大的软件,无论是平面的还是立体的分子结构式都能毫不费力地绘制出来。当然这份强大是要钱的,对于平面的分子式或反应式,不要钱而且还可行的方案大致也就LaTeX语言中的Chemfig宏包。
Chemfig是法国学者开发的宏包,εTeX,pdfLaTeX等TeX编译器都能正常使用,并且相对来说开发是比较活跃的。

texlive

TeX Live 是 TUG (TeX User Group) 发布并维护的的 TeX 系统,可以称得上是TeX的官方系统。对于任何阶段的TeX用户,都可以使用TeX Live, 以保持在跨操作系统、跨用户的TeX文件一致性。

texlive Docker镜像及服务化改造

texlive的安装B站有很多教程,目标是需要提供绘制chemfig化学方程式转换的服务,而texlive软件本身并不提供相关的api服务,需要对其进行服务化改造,因为考虑容器化部署,需要将texlive封装成docker镜像

话不多说,我们选择的基础镜像是 texlive:2020 ,使用Python对外提供服务,关于texlive相关的介绍可以参考博客:chemfig化学式转换为pdf

拉取镜像并运行

docker pull texlive:2020

docker run --name texlive -d texlive:2020

docker ps -a | grep  live
dda1561ae866  texlive:2020   "tail -f /dev/null" 8 seconds ago  Up 2  texlive

docker exec -it dda1561ae866 bash

安装python,制作texlive-python镜像

# texlive是基于Alpine Linux,目前主流
cat /etc/issue
Welcome to Alpine Linux 3.12

# 修改apk镜像源
vi etc/apk/repositories

替换文件内容为阿里源:
http://mirrors.aliyun.com/alpine/v3.8/main/
http://mirrors.aliyun.com/alpine/v3.8/community/

# 更新软件库
apk update
fetch http://mirrors.aliyun.com/alpine/v3.8/main/x86_64/APKINDEX.tar.gz
fetch http://mirrors.aliyun.com/alpine/v3.8/community/x86_64/APKINDEX.tar.gz
v3.8.5-67-gf94de196ca [http://mirrors.aliyun.com/alpine/v3.8/main/]
v3.8.5-66-gccbd6a8ae7 [http://mirrors.aliyun.com/alpine/v3.8/community/]
OK: 9578 distinct packages available

# 安装python3
apk add --no-cache python3 python3-dev py-pip

# 验证安装
python3 -V
Python 3.6.9
bash-4.4# pip3 -V
pip 18.1 from /usr/lib/python3.6/site-packages/pip (python 3.6)

保存镜像

docker commit dda1561ae866 textlive-python

至此,我们的拥有python环境texlive镜像就已经制作好了

服务化改造

python脚本

from flask import Flask, abort, request, jsonify
import os
import subprocess
import uuid
import base64

app = Flask(__name__)


@app.route("/texlive/translate/", methods=["POST"])
def translate():
    if not request.json or "chemfig" not in request.json:
        abort(400)
    chem_fig = request.json["chemfig"]
    # 由于不好测算化学方程式图形的大小,这里支持配置纸张大小,我们这里默认a5paper
    if "paper" in request.json:
        paper = request.json["paper"]
    else:
        paper = "a5paper"
    tempFile = open("template.tex")
    lines = tempFile.readlines()
    lines[2] = lines[2].replace("a5paper", paper)
    lines[15] = chem_fig + "
"
    uuidStr = str(uuid.uuid1())
    new_file_name = uuidStr + ".tex"
    newFile = open(new_file_name, "a+")
    for line in lines:
        newFile.write(line)
    newFile.flush()
    newFile.close()
    try:
        # 这里使用subprocess比较稳定,os.system经常会出现莫名问题,至少这个pdflatex命令用os.system会报错
        subprocess.run(["pdflatex", "-interaction=nonstopmode", new_file_name])
        pdf_string = open(uuidStr + ".pdf", "rb").read()
        encoded = base64.b64encode(pdf_string)
        pdf_link = "data:application/pdf;base64,{}".format(encoded)
        pdf_link = pdf_link.replace("b"", "").replace(""", "")
    except:
        remove_file(uuidStr)
    else:
        remove_file(uuidStr)

    return jsonify({"image": pdf_link})


def remove_file(uuid_str):
    os.system("rm " + uuid_str + ".tex")
    os.system("rm " + uuid_str + ".log")
    os.system("rm " + uuid_str + ".pdf")
    os.system("rm " + uuid_str + ".aux")


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8080, debug=False)

Latex模板

template.tex

documentclass{minimal}
usepackage{xcolor, mol2chemfig}
usepackage[a5paper, margin=10px, total={6in, 8in}]{geometry}

usepackage[helvet]{sfmath}
setcrambond{2.5pt}{0.4pt}{1.0pt}
setbondoffset{1pt}
setdoublesep{2pt}
setatomsep{%(atomsep)spt}
enewcommand{printatom}[1]{fontsize{8pt}{10pt}selectfont{ensuremath{mathsf{#1}}}}

setlength{parindent}{0pt}
setlength{fboxsep}{0pt}
egin{document}

chemfig{H_3C-[:30]N**6(-(=O)-(**5(-N(-CH_3)--N-))--N(-CH_3)-(=O)-)}

end{document}

mol2chemfig.sty

requirements.txt

Flask

startup.sh

python3 ./main.py

DockerFile

FROM texlive-python:2020
COPY main.py /home
COPY mol2chemfig.sty /home
COPY template.tex /home
COPY requirements.txt /home
COPY startup.sh /home
WORKDIR /home
EXPOSE 8080
RUN pip3 install -r requirements.txt && ls
CMD ["bash", "startup.sh"]

服务镜像制作

构建 texlive-python-api

# dockerfile构建
docker build -t texlive-python-api .
# 打tag
docker tag texlive-python-api xxx.xxx.com/xxx/texlive-python-api:2020
# 推送远程镜像仓库
docker push xxx.xxx.com/xxx/texlive-python-api:2020

跑一个试试

docker run -p 8080:8080 -d --name texlive-python-api texlive-python-api

请求body

{"paper":"a6paper",
"chemfig":"\chemfig{CH_3-[:108,,1]N-[:54](-[:180,0.85,,,draw=none]\mcfcringle{1.03})%
-[:126]N-[:198]-[:270](-[:342]\phantom{N})-[:210](=[:270]O)-[:150]N(%
-[:210,,,2]H_3C)-[:90](=[:150]O)-[:30]N(-[:330])-[:90,,,1]CH_3}

\bigskip

\chemfig{CH_3-[:108,,1]N-[:54](-[:180,0.85,,,draw=none]\mcfcringle{1.03})%
-[:126]N-[:198]-[:270](-[:342]\phantom{N})-[:210](=[:270]O)-[:150]N(%
-[:210,,,2]H_3C)-[:90](=[:150]O)-[:30]N(-[:330])-[:90,,,1]CH_3}"}

postman

拷贝出来放在浏览器就可以直接访问了,这个是a6纸张的渲染效果,可以根据图的大小动态的传入纸张规格

再来个大的

{"paper":"a3paper",
"chemfig":"\chemfig{CH_3-[:108,,1]N-[:54](-[:180,0.85,,,draw=none]\mcfcringle{1.03})%
-[:126]N-[:198]-[:270](-[:342]\phantom{N})-[:210](=[:270]O)-[:150]N(%
-[:210,,,2]H_3C)-[:90](=[:150]O)-[:30]N(-[:330])-[:90,,,1]CH_3}

\bigskip

\chemfig{
O%5
=[:270]%3
(
-[:210]%2
(
<[:150]%4
-[:90.4,2.094]%115
-[:18]%109
-[:330]%110
-[:30,,,,dlh]%111
-[:90]%112
-[:150,,,,dlh]%107
-[:210]%108
(
-[:162]\mcfabove{N}{H}%113
-[:234]%114
-[:306,,,,dbl={73}{73}]%->115
)
-[:270,,,,dlh]%->109
)
-[:270,,,2]HN%1
-[:330,,2]%93
(
-[:270]%95
-[:210]%96
-[:270]%97
-[:330]%98
-[:30]%99
-[:330]%100
-[:270]%101
-[:210]%102
-[:270]%103
)
=[:30]O%94
)
-[:330]\mcfbelow{N}{H}%6
-[:30]%7
(
<:[:90]%9
-[:30]%10
(
-[:330,,,1]NH_2%12
)
=[:90]O%11
)
-[:330]%8
(
=[:270]O%15
)
-[:30]\mcfabove{N}{H}%13
-[:330]%14
(
<:[:270]%16
-[:330]%19
(
=[:270]O%21
)
-[:30,,,1]OH%20
)
-[:30]%17
(
=[:90]O%22
)
-[:330]\mcfbelow{N}{H}%18
-[:30]%23
<:[:90]%24
(
=[:150]O%26
)
-[:30,,,1]NH%25
-[:90,,1]%27
-[:30]%28
(
=[:90]O%29
)
-[:330]\mcfbelow{N}{H}%30
-[:30]%31
(
-[:90]%33
-[:30]%34
-[:90]%35
-[:30,,,1]NH_2%36
)
-[:330]%32
(
=[:270]O%37
)
-[:30]\mcfabove{N}{H}%38
-[:330]%39
(
<:[:270]%43
-[:330]%44
(
-[:30,,,1]OH%46
)
=[:270]O%45
)
-[:30]%40
(
=[:90]O%42
)
-[:330]\mcfbelow{N}{H}%41
-[:30]%47
(
<:[:90]%49
)
-[:330]%48
(
=[:30]O%51
)
-[:270,,,2]HN%50
-[:330,,2]%52
(
<[:30]%54
-[:330]%55
(
-[:270,,,1]OH%57
)
=[:30]O%56
)
-[:270]%53
(
=[:330]O%58
)
-[:210,,,2]HN%59
-[:270,,2]%60
-[:210]%61
(
=[:270]O%62
)
-[:150]\mcfabove{N}{H}%63
-[:210]%64
(
<:[:270]%104
-[:330,,,1]OH%105
)
-[:180,1.732]%91
(
-[:150,,,,dlhs]O%92
)
-[:270,,,1]NH%90
-[:210,,1]%82
(
<[:270]%84
(
-[:330]%86
-[:270]%87
(
-[:210,,,2]HO%89
)
=[:330]O%88
)
-[:210]%85
)
-[:150]%81
(
=[:90]O%83
)
-[:210]\mcfbelow{N}{H}%71
-[:150]%69
(
-[:90]%68
(
=[:30]O%70
)
-[:150]O%67
-[:90]%65
(
<[:30]%66
)
-[:150]%->23
)
<[:210]%72
-[:270]%73
(
=[:330]O%75
)
-[:210]%74
-[:270,,,,drh]%80
(
-[:330,,,1]NH_2%106
)
-[:210]%79
-[:150,,,,drh]%78
-[:90]%77
-[:30,,,,drh]%76
(
-[:330]%->74
)
}
"}

PS:LaTeX是一种基于ΤΕΧ的排版系统,不光是化学方程式,其他latex语法都可以通过本镜像渲染,template.tex是模板文件,对latex排版有要求的可以自行定制

参考链接

chemfig化学式转换为pdf:https://www.cnblogs.com/xiaoqi/p/chemfig.html
mol2chemfig:http://chimpsky.uwaterloo.ca/mol2chemfig/index
Chemfig中文手册:https://chemfig.man.huzheyang.cn/index.html
用latex绘制有机化学分子式:http://static.latexstudio.net/wp-content/uploads/2016/09/chemfig.pdf

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » 基于texlive定制chemfig化学式转换Python服务镜像