Import python module in flutter using starflut

4,653

Solution 1

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  

Solution 2

Have you tried chaquopy plugin for flutter, as it supports 90% of the python packages that you can use and integrate inside your flutter app.

Share:
4,653
Danya Yatsenko
Author by

Danya Yatsenko

Updated on December 17, 2022

Comments

  • Danya Yatsenko
    Danya Yatsenko over 1 year

    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.

  • Danya Yatsenko
    Danya Yatsenko almost 4 years
    Thank you for answer. Where should I execute these commands? I get an error if I place pip install PyDejavu above from dejavu import Dejavu in my .py file. Sorry if the question is dumb, I am new in python.
  • Tms91
    Tms91 almost 4 years
    You have to type the first one in your command prompt (please search on google 'pip install command prompt') and the second in each module using dejavue.
  • Tms91
    Tms91 almost 4 years
    If you are new in python you should practice a bit with basic concepts before starting using modules not included in default django package. Or it is going to be hard...