]> Softwares of Agnibho - medscript.git/blob - renderer.py
Creation
[medscript.git] / renderer.py
1 # MedScript
2 # Copyright (C) 2023 Dr. Agnibho Mondal
3 # This file is part of MedScript.
4 # MedScript is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
5 # MedScript is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
6 # You should have received a copy of the GNU General Public License along with MedScript. If not, see <https://www.gnu.org/licenses/>.
7
8 import os, shutil, tempfile, json, datetime
9 from jinja2 import Template
10 from config import config
11
12 class Renderer:
13
14 tempdir=None
15
16 def render(self, data_directory):
17 source=os.path.join(data_directory, "prescription.json")
18 target=os.path.join(data_directory, "template", "output.html")
19 template=os.path.join(data_directory, "template", "index.html")
20 if not os.path.exists(template):
21 shutil.copytree(config["template"], os.path.join(data_directory, "template"), dirs_exist_ok=True)
22 with open(source, "r") as source_file, open(target, "w") as target_file:
23 with open(template) as template_file:
24 template_data = Template(template_file.read())
25 data=json.loads(source_file.read())
26 try:
27 data["date"]=datetime.datetime.strptime(data["date"], "%Y-%m-%d %H:%M:%S")
28 except Exception as e:
29 print(e)
30 output=template_data.render(data)
31 target_file.write(output)
32 return(target)