修复添加plt.savefig没有缩进问题

master
whb 5 months ago
parent 14e34a8957
commit 8ca3203a69

@ -55,7 +55,6 @@ public class JupyterController {
} else {
pythonCode = pythonCodeUp;
}
System.out.println("替换dataResource之后的===================:"+pythonCode);
if (pythonCode.contains("show")) {
// 创建一个新的 JSONObject
@ -70,8 +69,6 @@ public class JupyterController {
// 写入临时Python文件
String tempPythonFile = "/usr/local/tianzeProject/financial_bigdata_total/py/code/" + s + ".py";
try (PrintWriter out = new PrintWriter(tempPythonFile)) {
out.println(pythonCode);
}
@ -110,7 +107,6 @@ public class JupyterController {
if (exitCode == 0) {
// 执行成功输出Python代码的结果
System.out.println("Python code output:\n" + output.toString());
return new ResultEntity(HttpStatus.OK, output.toString());
} else {
// 执行失败,输出错误信息
@ -130,7 +126,6 @@ public class JupyterController {
public ResultEntity validatePythonCodePhoto(@RequestBody JSONObject upCodeJson) {
System.out.println(upCodeJson);
String pythonCode = upCodeJson.getString("code");
@ -147,7 +142,6 @@ public class JupyterController {
// 输出流示例
PrintWriter out = null;
//============
if (pythonCode.contains("show")) {
// 将 pythonCode 按行拆分
String[] lines = pythonCode.split("\\r?\\n");
@ -164,33 +158,31 @@ public class JupyterController {
String imgPathv = IdUtil.simpleUUID();
String plotFile = pyPath + imgPathv + ".png";
// 获取当前行的缩进
String indent = lines[i].substring(0, lines[i].indexOf(lines[i].trim()));
// 处理最后一行的情况,确保不重复插入 plt.savefig()
if (i == length - 1 && lines[i].contains("plt.show()")) {
out.println("plt.savefig('" + plotFile + "')");
out.println(lines[i]);
out.println(indent + "plt.savefig('" + plotFile + "')"); // 插入保存语句并保持缩进
out.println(lines[i]); // 输出原有 plt.show()
stringList.add(pyPath + imgPathv + ".png");
System.out.println("保存图片路径:" + pyPath + imgPathv + ".png");
continue;
}
// 判断是否为 plt.show() 相关的语句
if (lines[i].contains("plt.show()")&&i != length - 1) {
out.println("plt.savefig('" + plotFile + "')");
if (lines[i].contains("plt.show()") && i != length - 1) {
out.println(indent + "plt.savefig('" + plotFile + "')"); // 保持缩进
stringList.add(pyPath + imgPathv + ".png");
System.out.println("保存图片路径:" + pyPath + imgPathv + ".png");
}
// 在 plt.savefig() 之后输出原来的 plt.show() 语句
out.println(lines[i]);
}
System.out.println("数据写入完成:"+stringList);
out.close();
}
}
String[] command = {"docker", "exec", "pyexe", "python", pyCode + s + ".py"};
// 创建一个新的进程来执行Python代码
@ -222,11 +214,9 @@ public class JupyterController {
// 等待进程执行完成
int exitCode = process.waitFor();
System.out.println("代码运行结束!!");
if (exitCode == 0) {
// 执行成功输出Python代码的结果
System.out.println("Python code output:\n" + output.toString());
String base64Image = "";
List<String> base64ImageList = new ArrayList<>();
@ -237,7 +227,6 @@ public class JupyterController {
// 读取图片文件并转换为Base64字符串
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.out.println("读取的stringList文件" + stringList.get(i));
try (FileInputStream fis = new FileInputStream(stringList.get(i))) {
byte[] buffer = new byte[1024];
@ -245,7 +234,7 @@ public class JupyterController {
while ((len = fis.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
System.out.println("图片读取成功!!!!");
}
base64Image = Base64.encodeBase64String(baos.toByteArray());
base64ImageList.add(base64Image);

Loading…
Cancel
Save