diff --git a/TeXmacs/plugins/python/bin/python.pex b/TeXmacs/plugins/python/bin/python.pex index c5af9ce22eddc1cc4246907e5f844e791e677a00..f82073502d9717dfebf1899850b1d7ccfe953e73 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 ed9f09f0a55e63574419d5d3309a580d1fb2fa8d..fcb3a0bb7de6a9424bdd24c183ac3044692dd333 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))