|
|
|
@ -5,18 +5,16 @@ import com.sztzjy.financial_bigdata.annotation.AnonymousAccess;
|
|
|
|
|
import com.sztzjy.financial_bigdata.util.ResultEntity;
|
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
|
import org.python.core.CompileMode;
|
|
|
|
|
import org.python.core.Py;
|
|
|
|
|
import org.python.core.PyCode;
|
|
|
|
|
import org.python.core.PyObject;
|
|
|
|
|
import org.python.util.PythonInterpreter;
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@Api(tags = "python接口")
|
|
|
|
@ -27,82 +25,104 @@ public class PythonController {
|
|
|
|
|
@ApiOperation("代码检验")
|
|
|
|
|
@AnonymousAccess
|
|
|
|
|
public ResultEntity validatePythonCode(@RequestBody JSONObject upCodeJson) {
|
|
|
|
|
String upCode = upCodeJson.getString("code");
|
|
|
|
|
// 对 Python 代码进行检验
|
|
|
|
|
String head="# -- coding: utf-8 --\n";
|
|
|
|
|
String code=head+upCode;
|
|
|
|
|
PythonInterpreter interpreter = new PythonInterpreter();
|
|
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
|
|
System.out.println(upCodeJson);
|
|
|
|
|
String pythonCode = upCodeJson.getString("code");
|
|
|
|
|
try {
|
|
|
|
|
InputStream stream = new ByteArrayInputStream(code.getBytes());
|
|
|
|
|
PyCode pyCode = Py.compile(stream, "<string>", CompileMode.exec);
|
|
|
|
|
// 创建一个新的进程来执行Python代码
|
|
|
|
|
// Process process = Runtime.getRuntime().exec("python");
|
|
|
|
|
Process process = Runtime.getRuntime().exec("/usr/bin/python3");
|
|
|
|
|
// 获取进程的输入流
|
|
|
|
|
BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
|
|
|
|
|
|
|
|
|
// 获取进程的输出流
|
|
|
|
|
BufferedReader errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
|
|
|
|
|
|
|
|
|
|
// 重定向输出到 outputStream
|
|
|
|
|
interpreter.setOut(outputStream);
|
|
|
|
|
// 向进程的输入流写入Python代码
|
|
|
|
|
process.getOutputStream().write(pythonCode.getBytes());
|
|
|
|
|
process.getOutputStream().flush();
|
|
|
|
|
process.getOutputStream().close();
|
|
|
|
|
|
|
|
|
|
// 读取Python代码的输出
|
|
|
|
|
String line;
|
|
|
|
|
StringBuilder output = new StringBuilder();
|
|
|
|
|
while ((line = inputStream.readLine()) != null) {
|
|
|
|
|
output.append(line).append("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interpreter.exec(pyCode);
|
|
|
|
|
// 读取Python代码的错误信息
|
|
|
|
|
StringBuilder errors = new StringBuilder();
|
|
|
|
|
while ((line = errorStream.readLine()) != null) {
|
|
|
|
|
errors.append(line).append("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取运行结果
|
|
|
|
|
String result = outputStream.toString();
|
|
|
|
|
// 等待进程执行完成
|
|
|
|
|
int exitCode = process.waitFor();
|
|
|
|
|
|
|
|
|
|
return new ResultEntity<>(HttpStatus.OK, result);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return new ResultEntity<>(HttpStatus.BAD_REQUEST, "语法有误", e.getMessage());
|
|
|
|
|
} finally {
|
|
|
|
|
interpreter.close();
|
|
|
|
|
if (exitCode == 0) {
|
|
|
|
|
// 执行成功,输出Python代码的结果
|
|
|
|
|
System.out.println("Python code output:\n" + output.toString());
|
|
|
|
|
return new ResultEntity(HttpStatus.OK,output.toString());
|
|
|
|
|
} else {
|
|
|
|
|
// 执行失败,输出错误信息
|
|
|
|
|
System.err.println("Error executing Python code:\n" + errors.toString());
|
|
|
|
|
return new ResultEntity(HttpStatus.BAD_REQUEST,errors.toString());
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException | InterruptedException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return new ResultEntity(HttpStatus.BAD_REQUEST,e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
String pythonCode = "num=100\na=2\nb=2\nc=a+b\nprint(c)\n";
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 构建Python命令和代码
|
|
|
|
|
String pythonCommand = "python";
|
|
|
|
|
String pythonCode = "#读取数据\n" +
|
|
|
|
|
"import pandas as pd\n" +
|
|
|
|
|
"df = pd.read_excel('IT行业收入表.xlsx')#读取excel表\n" +
|
|
|
|
|
"df.head()#使用函数,显示前几行数据\n" +
|
|
|
|
|
"\n" +
|
|
|
|
|
"# 此时的工龄为自变量,薪水为因变量,通过如下代码进行自变量、因变量选取\n" +
|
|
|
|
|
"X = df[['工龄']]\n" +
|
|
|
|
|
"Y = df['薪水']\n" +
|
|
|
|
|
"\n" +
|
|
|
|
|
"# 通过如下代码可以将此时的散点图绘制出来:\n" +
|
|
|
|
|
"from matplotlib import pyplot as plt\n" +
|
|
|
|
|
"%matplotlib inline\n" +
|
|
|
|
|
"plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签\n" +
|
|
|
|
|
"plt.scatter(X,Y)#绘制散点图\n" +
|
|
|
|
|
"plt.xlabel('工龄')#设置x轴标签\n" +
|
|
|
|
|
"plt.ylabel('薪水')#设置y轴标签\n" +
|
|
|
|
|
"plt.show()";
|
|
|
|
|
|
|
|
|
|
// 创建进程构建器
|
|
|
|
|
ProcessBuilder processBuilder = new ProcessBuilder(pythonCommand);
|
|
|
|
|
|
|
|
|
|
// 启动进程
|
|
|
|
|
Process process = processBuilder.start();
|
|
|
|
|
|
|
|
|
|
// 获取标准输入流
|
|
|
|
|
InputStream inputStream = process.getInputStream();
|
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
|
|
|
|
|
|
|
// 将Python代码写入标准输入流
|
|
|
|
|
// 创建一个新的进程来执行Python代码
|
|
|
|
|
Process process = Runtime.getRuntime().exec("python");
|
|
|
|
|
|
|
|
|
|
// 获取进程的输入流
|
|
|
|
|
BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
|
|
|
|
|
|
|
|
|
// 获取进程的输出流
|
|
|
|
|
BufferedReader errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
|
|
|
|
|
|
|
|
|
|
// 向进程的输入流写入Python代码
|
|
|
|
|
process.getOutputStream().write(pythonCode.getBytes());
|
|
|
|
|
process.getOutputStream().flush();
|
|
|
|
|
process.getOutputStream().close();
|
|
|
|
|
|
|
|
|
|
// 读取和打印Python解释器的输出
|
|
|
|
|
// 读取Python代码的输出
|
|
|
|
|
String line;
|
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
|
System.out.println(line);
|
|
|
|
|
StringBuilder output = new StringBuilder();
|
|
|
|
|
while ((line = inputStream.readLine()) != null) {
|
|
|
|
|
output.append(line).append("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 读取Python代码的错误信息
|
|
|
|
|
StringBuilder errors = new StringBuilder();
|
|
|
|
|
while ((line = errorStream.readLine()) != null) {
|
|
|
|
|
errors.append(line).append("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 等待进程执行完成
|
|
|
|
|
int exitCode = process.waitFor();
|
|
|
|
|
System.out.println("Exit Code: " + exitCode);
|
|
|
|
|
|
|
|
|
|
if (exitCode == 0) {
|
|
|
|
|
// 执行成功,输出Python代码的结果
|
|
|
|
|
System.out.println("Python code output:\n" + output.toString());
|
|
|
|
|
} else {
|
|
|
|
|
// 执行失败,输出错误信息
|
|
|
|
|
System.err.println("Error executing Python code:\n" + errors.toString());
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException | InterruptedException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|