找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 377|回复: 0

microbee-http调用python API

[复制链接]

9

主题

0

回帖

46

积分

新手上路

积分
46
发表于 2026-3-24 10:51:40 | 显示全部楼层 |阅读模式
本帖最后由 super_qjl 于 2026-4-1 17:47 编辑

框架支持java调用python代码,使用框架内工具类即可实现。

1.使用方法
1.1.虚拟环境配置

  通过server_conf.xml中的webRoot参数进行配置虚拟环境路径,程序将自动创建虚拟空间,在该目录下生成.python_embed_venv文件夹,python环境及第三方依赖存储在该文件夹下。

1.2.实例化

  实例化PythonEmbedCaller类,参数为:python文件地址/python代码段、python版本(python或python3,传空则默认使用python);

  1. 例:PythonEmbedCaller pythonEmbedCaller = new PythonEmbedCaller("/wwwdata/product/udso/dynamic/my_func.py", "python");
复制代码
1.3.调用1.3.1.方式1:无参函数 + 手动指定依赖
  1. // 调用无参方法。参数:python方法名、依赖集合
  2.     JsonNode result1 = pythonEmbedCaller.callPythonFunctionNoArgs("get_version", Collections.emptyList());
  3.     System.out.println(result1.toPrettyString());
复制代码
1.3.2.方式2:有参函数 + 手动指定依赖
  1. // 设置参数
  2.     Map input = new HashMap<>();
  3.     input.put("values", Arrays.asList(10.0, 20.0, 30.0));
  4.     // 显式声明依赖:numpy 和 pandas
  5.     List deps = Arrays.asList("numpy", "pandas");
  6.     // 调用有参方法。参数:python方法名、参数、依赖集合
  7.     JsonNode result2 = pythonEmbedCaller.callPythonFunctionWithArg("process", input, deps);
  8.     System.out.println(result2.toPrettyString());
复制代码
1.3.3.方式3:自动探测并安装缺失依赖
  1. // 设置参数
  2.     Map input = new HashMap<>();
  3.     input.put("values", Arrays.asList(1.5, 2.5, 3.5));
  4.     // 不传任何依赖列表!系统自动处理。参数:python方法名、参数
  5.     JsonNode result3 = pythonEmbedCaller.callPythonFunctionAutoDep("process", input);
  6.     System.out.println(result3.toPrettyString());
复制代码
1.3.4.方式4:执行code而非文件并自动探测依赖
  1. // 定义code
  2.     String code =
  3.           "def compute(data):n" +
  4.                "    import mathn" +
  5.                "    return {'sqrt': math.sqrt(data['x'])}n";
  6.     // 设置参数
  7.     Map input = new HashMap<>();
  8.     input.put("x", 16.0);
  9.     // 调用。参数:python代码段、python方法名、参数
  10.     JsonNode result4 = pythonEmbedCaller.callPythonCode(code, "compute", input);
  11.     System.out.println(result4.toPrettyString());
复制代码
2.python代码文件:my_func.py示例

注:.py文件需增加# -*- coding: utf-8 -*-指定utf-8

  1. # -*- coding: utf-8 -*-
  2.     import numpy as np
  3.     import pandas as pd  # 故意加一个可能未安装的包
  4.     import sys

  5.     def get_version():
  6.         return {"version": "2.0"}

  7.     def process(data):
  8.         arr = np.array(data["values"])
  9.         df = pd.DataFrame({"x": arr})
  10.         return {
  11.             "sum": float(arr.sum()),
  12.             "mean": float(arr.mean()),
  13.             "rows": len(df)
  14.         }

  15.     def hello_with_progress(n):
  16.         # 兼容 Python 2 和 Python 3
  17.         if sys.version_info[0] < 3:
  18.             print ">>> Python executable:", sys.executable
  19.             print ">>> sys.prefix:", sys.prefix
  20.         else:
  21.            print(">>> Python executable:", sys.executable)
  22.            print(">>> sys.prefix:", sys.prefix)
  23.    
  24.         import tqdm
  25.         for i in tqdm.tqdm(range(n)):
  26.             pass
  27.         return {"status": "completed", "count": n}
复制代码



您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|MICROBEE-BBS

GMT+8, 2026-8-26 15:13 , Processed in 0.048481 second(s), 20 queries .

Powered by Microbee-http

快速回复 返回顶部 返回列表