From 5a731ca8e5faaa9ff9f7abc5300ecfbf1d7161c6 Mon Sep 17 00:00:00 2001 From: MoonLL Date: Mon, 1 Dec 2025 14:39:38 +0800 Subject: [PATCH] =?UTF-8?q?[61=5F2]=20Python=E6=8F=92=E4=BB=B6=EF=BC=9Adat?= =?UTF-8?q?aframe=E6=94=B9=E4=B8=BAhtml=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TeXmacs/plugins/python/bin/python.pex | 41 ++++++++++++++++++--- TeXmacs/plugins/python/bin/tmpy/protocol.py | 3 ++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/TeXmacs/plugins/python/bin/python.pex b/TeXmacs/plugins/python/bin/python.pex index c5af9ce22..f82073502 100644 --- a/TeXmacs/plugins/python/bin/python.pex +++ b/TeXmacs/plugins/python/bin/python.pex @@ -31,6 +31,7 @@ from tmpy.protocol import ( flush_scheme_u8, flush_verbatim, flush_latex, + flush_html, ) try: @@ -99,8 +100,10 @@ def flush_output(data): fig = convert_matplotlib_object_to_figure(data) flush_matplotlib_figure(fig) elif is_dataframe_object(data): - dfstr = convert_dataframe_to_scheme_str(data) - flush_scheme_u8(dfstr) + # dfstr = convert_dataframe_to_scheme_str(data) + # flush_scheme_u8(dfstr) + html = dataframe_to_html(data) + flush_html(html) elif is_sympy_object(data): flush_latex("\\rmfamily{" + latex(data) + "}") elif is_pil_object(data): @@ -175,16 +178,43 @@ def is_dataframe_object(obj): return False else: return False + +def dataframe_to_html(obj): + css = """ + + """ + html = css + obj.to_html(classes="my-table", border=0) + flush_verbatim(html) + return html + +def escape(s:str)->str: + """ 处理Python字符串中的反斜杠与引号 + """ + return s.replace("\\", "\\\\").replace('"', '\\"') def convert_dataframe_to_scheme_str(obj): """ 将dataframe中的数据取出,转换成scheme代码字符串 """ row_head = "(row " cell_head = "(cell " - result = "(document (tabular (tformat (cwith \\\"1\\\" \\\"1\\\" \\\"1\\\" \\\"-1\\\" \\\"cell-bborder\\\" \\\"1ln\\\") (table " + result = "(document (tabular (tformat (cwith \\\"1\\\" \\\"1\\\" \\\"1\\\" \\\"-1\\\" \\\"cell-bborder\\\" \\\"1ln\\\") (table " row_index = row_head + f"{cell_head}\\\"\\\" )" for column in obj.columns: - column_str = repr(str(column)) + column_str = str(column) column_str = column_str.replace('\\', '\\\\') if ' ' in column_str: column_str = column_str.replace(' ', '\\ ') @@ -195,13 +225,14 @@ def convert_dataframe_to_scheme_str(obj): for row in obj.itertuples(): row_str = row_head + f"{cell_head}\\\"{row.Index}\\\")"#(row (cell \"row.Index\") for i, column in enumerate(columns_list, 1): - value = repr(str(row[i])) + value = str(row[i]) value = value.replace('\\', '\\\\') if ' ' in value: value = value.replace(' ', '\\ ') row_str = row_str + f" {cell_head}\\\"{value}\\\")" #(row (cell \"row.Index\") (cell \"row.column[1]\")...(cell \"row.column[n]\") result = result + f" {row_str})" result = result + "))))" + flush_verbatim(result) return result def is_sympy_object(obj): diff --git a/TeXmacs/plugins/python/bin/tmpy/protocol.py b/TeXmacs/plugins/python/bin/tmpy/protocol.py index ed9f09f0a..fcb3a0bb7 100644 --- a/TeXmacs/plugins/python/bin/tmpy/protocol.py +++ b/TeXmacs/plugins/python/bin/tmpy/protocol.py @@ -83,6 +83,9 @@ def flush_file(path): def flush_ps(content): flush_any("ps:" + content) +def flush_html(html): + flush_any("html: " + html) + def flush_err(content): os.sys.stderr.write(chr(2)) -- Gitee