How to import python package using starflut in flutter?

574

If you want to integrate python packages inside your app I would strongly recommend using chaquopy plugin for flutter, as it's easy to use and also have python package support.

Share:
574
Maria Nabil
Author by

Maria Nabil

Updated on December 20, 2022

Comments

  • Maria Nabil
    Maria Nabil over 1 year

    I am trying to to integrate python script in my flutter app and I need to import cv2 in the python code but It did not find the package cv2 ,

    is there a way to import python package using starflut in flutter?

    Here is the code I used to link the Python code to my flutter app :

    dynamic python = null;
    String _outputString = "python 3.6";
    StarSrvGroupClass SrvGroup;
    String docPath;
    String resPath;
    StarServiceClass Service;
    
    Future<void> testCallPython() async {
      //Initialize starecore
      StarCoreFactory starcore = await Starflut.getFactory();
      Service = await starcore.initSimple("test", "123", 0, 0, []);
    
      //Register callback function
      await starcore.regMsgCallBackP(
          (int serviceGroupID, int uMsg, Object wParam, Object lParam) async {
        print("FIRST PRINT $serviceGroupID  $uMsg   $wParam   $lParam");
        return null;
      });
      SrvGroup = await Service["_ServiceGroup"];
    
      /*---script python--*/
      //Initialize python
      // for android, before run python, share libraries must be copied to app’s local folder.
      bool isAndroid = await Starflut.isAndroid();
      if (isAndroid == true) {
        await Starflut.copyFileFromAssets("Main_darts.py",
            "flutter_assets/starfiles", "flutter_assets/starfiles");
        await Starflut.copyFileFromAssets("python3.6.zip",
            "flutter_assets/starfiles", null); //desRelatePath must be null
        await Starflut.copyFileFromAssets("zlib.cpython-36m.so", null, null);
        await Starflut.copyFileFromAssets("unicodedata.cpython-36m.so", null, null);
        await Starflut.loadLibrary("libpython3.6m.so");
      }
    
      docPath = await Starflut.getDocumentPath();
      print("docPath = $docPath");
    
      resPath = await Starflut.getResourcePath();
      print("resPath = $resPath");
    
    // Initialize python
      dynamic rr1 = await SrvGroup.initRaw("python36", Service);
    
      print("initRaw = $rr1");
    
    // PYTHON SCRIPT
      var Result1 = await SrvGroup.loadRawModule("python", "",
          resPath + "/flutter_assets/starfiles/" + "Main_darts.py", false);
      print("loadRawModule = $Result1");
    
      python = await Service.importRawContext("python", "", false, "");
      print("python = " + await python.getString());
    }
    

    Thanks in advance .