Shreyas094 commited on
Commit
6dffd1c
·
verified ·
1 Parent(s): dd6cde0

Update backend/utils.py

Browse files
Files changed (1) hide show
  1. backend/utils.py +91 -91
backend/utils.py CHANGED
@@ -1,92 +1,92 @@
1
- import aiofiles
2
- import urllib
3
- import mistune
4
-
5
- async def write_to_file(filename: str, text: str) -> None:
6
- """Asynchronously write text to a file in UTF-8 encoding.
7
-
8
- Args:
9
- filename (str): The filename to write to.
10
- text (str): The text to write.
11
- """
12
- # Ensure text is a string
13
- if not isinstance(text, str):
14
- text = str(text)
15
-
16
- # Convert text to UTF-8, replacing any problematic characters
17
- text_utf8 = text.encode('utf-8', errors='replace').decode('utf-8')
18
-
19
- async with aiofiles.open(filename, "w", encoding='utf-8') as file:
20
- await file.write(text_utf8)
21
-
22
- async def write_text_to_md(text: str, filename: str = "") -> str:
23
- """Writes text to a Markdown file and returns the file path.
24
-
25
- Args:
26
- text (str): Text to write to the Markdown file.
27
-
28
- Returns:
29
- str: The file path of the generated Markdown file.
30
- """
31
- file_path = f"outputs/{filename[:60]}.md"
32
- await write_to_file(file_path, text)
33
- return urllib.parse.quote(file_path)
34
-
35
- async def write_md_to_pdf(text: str, filename: str = "") -> str:
36
- """Converts Markdown text to a PDF file and returns the file path.
37
-
38
- Args:
39
- text (str): Markdown text to convert.
40
-
41
- Returns:
42
- str: The encoded file path of the generated PDF.
43
- """
44
- file_path = f"outputs/{filename[:60]}.pdf"
45
-
46
- try:
47
- from md2pdf.core import md2pdf
48
- md2pdf(file_path,
49
- md_content=text,
50
- # md_file_path=f"{file_path}.md",
51
- css_file_path="./frontend/pdf_styles.css",
52
- base_url=None)
53
- print(f"Report written to {file_path}")
54
- except Exception as e:
55
- print(f"Error in converting Markdown to PDF: {e}")
56
- return ""
57
-
58
- encoded_file_path = urllib.parse.quote(file_path)
59
- return encoded_file_path
60
-
61
- async def write_md_to_word(text: str, filename: str = "") -> str:
62
- """Converts Markdown text to a DOCX file and returns the file path.
63
-
64
- Args:
65
- text (str): Markdown text to convert.
66
-
67
- Returns:
68
- str: The encoded file path of the generated DOCX.
69
- """
70
- file_path = f"outputs/{filename[:60]}.docx"
71
-
72
- try:
73
- from docx import Document
74
- from htmldocx import HtmlToDocx
75
- # Convert report markdown to HTML
76
- html = mistune.html(text)
77
- # Create a document object
78
- doc = Document()
79
- # Convert the html generated from the report to document format
80
- HtmlToDocx().add_html_to_document(html, doc)
81
-
82
- # Saving the docx document to file_path
83
- doc.save(file_path)
84
-
85
- print(f"Report written to {file_path}")
86
-
87
- encoded_file_path = urllib.parse.quote(file_path)
88
- return encoded_file_path
89
-
90
- except Exception as e:
91
- print(f"Error in converting Markdown to DOCX: {e}")
92
  return ""
 
1
+ import aiofiles
2
+ import urllib
3
+ import mistune
4
+
5
+ async def write_to_file(filename: str, text: str) -> None:
6
+ """Asynchronously write text to a file in UTF-8 encoding.
7
+
8
+ Args:
9
+ filename (str): The filename to write to.
10
+ text (str): The text to write.
11
+ """
12
+ # Ensure text is a string
13
+ if not isinstance(text, str):
14
+ text = str(text)
15
+
16
+ # Convert text to UTF-8, replacing any problematic characters
17
+ text_utf8 = text.encode('utf-8', errors='replace').decode('utf-8')
18
+
19
+ async with aiofiles.open(filename, "w", encoding='utf-8') as file:
20
+ await file.write(text_utf8)
21
+
22
+ async def write_text_to_md(text: str, filename: str = "") -> str:
23
+ """Writes text to a Markdown file and returns the file path.
24
+
25
+ Args:
26
+ text (str): Text to write to the Markdown file.
27
+
28
+ Returns:
29
+ str: The file path of the generated Markdown file.
30
+ """
31
+ file_path = f"/tmp/outputs/{filename[:60]}.md"
32
+ await write_to_file(file_path, text)
33
+ return urllib.parse.quote(file_path)
34
+
35
+ async def write_md_to_pdf(text: str, filename: str = "") -> str:
36
+ """Converts Markdown text to a PDF file and returns the file path.
37
+
38
+ Args:
39
+ text (str): Markdown text to convert.
40
+
41
+ Returns:
42
+ str: The encoded file path of the generated PDF.
43
+ """
44
+ file_path = f"outputs/{filename[:60]}.pdf"
45
+
46
+ try:
47
+ from md2pdf.core import md2pdf
48
+ md2pdf(file_path,
49
+ md_content=text,
50
+ # md_file_path=f"{file_path}.md",
51
+ css_file_path="./frontend/pdf_styles.css",
52
+ base_url=None)
53
+ print(f"Report written to {file_path}")
54
+ except Exception as e:
55
+ print(f"Error in converting Markdown to PDF: {e}")
56
+ return ""
57
+
58
+ encoded_file_path = urllib.parse.quote(file_path)
59
+ return encoded_file_path
60
+
61
+ async def write_md_to_word(text: str, filename: str = "") -> str:
62
+ """Converts Markdown text to a DOCX file and returns the file path.
63
+
64
+ Args:
65
+ text (str): Markdown text to convert.
66
+
67
+ Returns:
68
+ str: The encoded file path of the generated DOCX.
69
+ """
70
+ file_path = f"outputs/{filename[:60]}.docx"
71
+
72
+ try:
73
+ from docx import Document
74
+ from htmldocx import HtmlToDocx
75
+ # Convert report markdown to HTML
76
+ html = mistune.html(text)
77
+ # Create a document object
78
+ doc = Document()
79
+ # Convert the html generated from the report to document format
80
+ HtmlToDocx().add_html_to_document(html, doc)
81
+
82
+ # Saving the docx document to file_path
83
+ doc.save(file_path)
84
+
85
+ print(f"Report written to {file_path}")
86
+
87
+ encoded_file_path = urllib.parse.quote(file_path)
88
+ return encoded_file_path
89
+
90
+ except Exception as e:
91
+ print(f"Error in converting Markdown to DOCX: {e}")
92
  return ""