gpt4 book ai didi

python - 如何使用 Starflut 在 Flutter 中导入 python 包?

转载 作者:行者123 更新时间:2023-12-04 13:54:34 26 4
gpt4 key购买 nike

我正在用 Flutter 构建一个应用程序,我需要使用一些 Python 代码。为此,我使用了 Starflut 插件 ( https://pub.dev/packages/starflut )。我成功运行了 Python 代码,这里没有问题,但我想导入额外的包。
为此,我在我的应用程序所在的位置创建了一个虚拟环境,然后通过 pip 安装程序 numpy、cv2 和 Pillow 下载,仍然没有问题,下载顺利。
但是,例如,当我尝试导入 numpy 时,我的 IDE(Visual Studio 代码)告诉我有一个错误:

Unable to import 'numpy' 
所以这是我的 main.dart 代码(唯一的 dart 文件,它是一个测试应用程序):
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:starflut/starflut.dart';

void main() => runApp(Main());

class Main extends StatefulWidget {
@override
_MainState createState() => _MainState();
}

class _MainState extends State<Main> {
var _outputString = '';
StarSrvGroupClass srvGroup;
dynamic python;

_MainState() {
_initStarCore();
}

void showOutput(String info) async {
if (info == null || info.length == 0) return;
_outputString = info;
setState(() {});
}

void _initStarCore() async {
StarCoreFactory starcore = await Starflut.getFactory();
StarServiceClass service =
await starcore.initSimple("test", "123", 0, 0, []);
String resPath = await Starflut.getResourcePath();
await starcore.regMsgCallBackP(
(int serviceGroupID, int uMsg, Object wParam, Object lParam) async {
if (uMsg == Starflut.MSG_DISPMSG || uMsg == Starflut.MSG_DISPLUAMSG) {
showOutput(wParam);
}
print("$serviceGroupID $uMsg $wParam $lParam");
return null;
});
srvGroup = await service["_ServiceGroup"];
bool isAndroid = await Starflut.isAndroid();
if (isAndroid == true) {
String libraryDir = await Starflut.getNativeLibraryDir();
String docPath = await Starflut.getDocumentPath();
if (libraryDir.indexOf("arm64") > 0) {
Starflut.unzipFromAssets("lib-dynload-arm64.zip", docPath, true);
} else if (libraryDir.indexOf("x86_64") > 0) {
Starflut.unzipFromAssets("lib-dynload-x86_64.zip", docPath, true);
} else if (libraryDir.indexOf("arm") > 0) {
Starflut.unzipFromAssets("lib-dynload-armeabi.zip", docPath, true);
} else {
//x86
Starflut.unzipFromAssets("lib-dynload-x86.zip", docPath, true);
}
await Starflut.copyFileFromAssets("python3.6.zip",
"flutter_assets/starfiles", null); //desRelatePath must be null
await Starflut.copyFileFromAssets(
"program.py", "flutter_assets/starfiles", "flutter_assets/starfiles");
}
if (await srvGroup.initRaw("python36", service) == true) {
_outputString = "init starcore and python 3.6 successfully";
} else {
_outputString = "init starcore and python 3.6 failed";
}

await srvGroup.loadRawModule("python", "",
resPath + "/flutter_assets/starfiles/" + "program.py", false);
python = await service.importRawContext("python", "", false, "");

setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
//_counter++;
});
}

void runScriptCode() async {
var result = await python.call("multiply", [5, 2]);
_outputString = result.toString();

setState(() {});
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: FlatButton(
color: Colors.red,
child: Text(_outputString),
onPressed: () {
runScriptCode();
},
),
),
),
);
}
}

和我的 Python 文件:
import numpy as np # (to test)

def multiply(a, b):
return a*b
如果有帮助,这是我的树结构:
The tree structure of my Flutter project
希望你能帮助我。
提前致谢 !

最佳答案

您可以使用chaquopy flutter 插件,如果你想在你的 android 应用程序中集成 python 包。

关于python - 如何使用 Starflut 在 Flutter 中导入 python 包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64688350/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com