Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import python module in flutter using starflut

I am trying to develop a Flutter app for audio fingerprints processing. I am using Starflut for python integration. Here is simple example:

//python file for using from dart

def tt(a,b) :
    print (a, b)
    return 666,777
g1 = 123
def yy(a,b,z) :
    print(a,b,z)
    return {'jack': 4098, 'sape': 4139}

class Multiply :

    def __init__(self):
        pass

    def multiply(self, a,b):
        print("multiply....",a,b)
        return a * b

//dart code which uses python

void _initStarCore() async {
    StarCoreFactory starcore = await Starflut.getFactory();
    StarServiceClass service = await starcore.initSimple("test", "123", 0, 0, []);
    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 {
            Starflut.unzipFromAssets("lib-dynload-x86.zip", docPath, true);
        }
        await Starflut.copyFileFromAssets("python3.6.zip",
            "flutter_assets/starfiles", null);
    }
    await srvGroup.initRaw("python36", service);

    String resPath = await Starflut.getResourcePath();
    srvGroup.loadRawModule("python", "",
        resPath + "/flutter_assets/starfiles/" + "testpy.py", false);

    dynamic python = await service.importRawContext("python", "", false, "");

    StarObjectClass retobj = await python.call("tt", ["hello ", "world"]);
    print(await retobj[0]);
    print(await retobj[1]);

    print(await python["g1"]);

    StarObjectClass yy = await python.call("yy", ["hello ", "world", 123]);
    print(await yy.call("__len__",[]));

    StarObjectClass multiply = await service.importRawContext("python", "Multiply", true, "");
    StarObjectClass multiply_inst = await multiply.newObject(["", "", 33, 44]);
    print(await multiply_inst.getString());

    print(await multiply_inst.call("multiply", [11, 22]));

    await srvGroup.clearService();
    await starcore.moduleExit();
}

Now I need to import python library Dejavu for audio fingerprinting, but I do not know how to do that. There is nothing about it in starflut documentation or issues in their repository.

Has somebody faced same problem? Or maybe somebody has any suggestion how i can try to solve it?

Sorry for mistakes, hope the text is understandable:) English is not my native language.

like image 336
Danya Yatsenko Avatar asked Mar 28 '20 16:03

Danya Yatsenko


People also ask

How do I import a module in flutter?

Using the File > New > New Module… menu in Android Studio in your existing Android project, you can either create a new Flutter module to integrate, or select an existing Flutter module that was created previously. If you create a new module, you can use a wizard to select the module name, location, and so on.

What is Starflut?

A new flutter plugin project,which supports flutter to interact with other scripting languages such as python, java, ruby, golang, rust, etc. It is easy to use, supports android, ios, and desktop.

Can we use Python libraries in flutter?

Currently, it supports pure python packages. This opens new possibilities as now we can use already written modules of python in flutter apps.


1 Answers

Did you read the repo readme and installation readme file?

If not, try this:

In your command prompt:

pip install PyDejavu

In the module where you need to import Dejavu:

from dejavu import Dejavu  
like image 82
Tms91 Avatar answered Sep 22 '22 13:09

Tms91