Flutter 集成 Python


This example shows how to run python script from flutter.

This example is based on starcore_for_flutter, which enables flutter calls other scripts.

Step by Step

1. Create flutter project

$ flutter create flutter_python 

2. Add dependency package “starflut” and assets folder

Edit “pubspec.yaml”

dependencies:   flutter:     sdk: flutter    starflut: any      ...      # To add assets to your application, add an assets section, like this:   assets:     - starfiles/   

3. Create “starfiles” folder at project root, and add file “python3.6.zip”

4. Create “assets” and “jniLibs”, and add python share libraries for android

5. dart code

  • Initialize starcore
StarCoreFactory starcore = await Starflut.getFactory(); StarServiceClass Service = await starcore.initSimple("test", "123", 0, 0, []); 
  • Register callback function
    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;     }); 

In callback function, we capture the output from python, and show it in text window.

  void ShowOutput(String Info) async{     if( Info == null || Info.length == 0)       return;     _outputString = _outputString + "\n" + Info;     setState((){      });   } 
  • 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 ){       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      } 

Initialize python

    if( await srvGroup.initRaw("python36", Service) == true ){       _outputString = "init starcore and python 3.6 successfully";       _isButtonDisabled = false;     }else{       _outputString = "init starcore and python 3.6 failed";     } 
  • Input and run python scrript
  void runScriptCode() async{     if( myController.text.length == 0 )       return;     await srvGroup.runScript("python", myController.text, null);      setState((){      });   } 

6. compile for ios

$ flutter clean $ export STARCORE_PATH='/Users/srplab/Desktop' $ export STARCORE_PYTHONVERSION='3.6' $ export STARCORE_PYTHONLIBRARY='star_python36,python3.6m' $ flutter build ios --no-codesign 

7. screenshot

Sample Download


没有评论 to “Flutter 集成 Python”


邮箱地址 (不会被公开) (必须):
评论 (必须)
XHTML: 您可以使用这些标记: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>