11import 'dart:io' ;
22
3- import 'package:flutter/widgets.dart' ;
4- import 'package:path/path.dart' as p;
3+ import 'package:path/path.dart' as path;
54
65import 'src/serious_python_platform_interface.dart' ;
76import 'src/utils.dart' ;
87
8+ export 'src/utils.dart' ;
9+
910/// Provides cross-platform functionality for running Python programs.
1011class SeriousPython {
1112 SeriousPython ._();
@@ -40,17 +41,16 @@ class SeriousPython {
4041 List <String >? modulePaths,
4142 Map <String , String >? environmentVariables,
4243 bool ? sync }) async {
43- // unpack app
44- WidgetsFlutterBinding .ensureInitialized ();
44+ // unpack app from asset
4545 String appPath = "" ;
46- if (p .extension (assetPath) == ".zip" ) {
46+ if (path .extension (assetPath) == ".zip" ) {
4747 appPath = await extractAssetZip (assetPath);
4848 if (appFileName != null ) {
49- appPath = p .join (appPath, appFileName);
50- } else if (await File (p .join (appPath, "main.pyc" )).exists ()) {
51- appPath = p .join (appPath, "main.pyc" );
52- } else if (await File (p .join (appPath, "main.py" )).exists ()) {
53- appPath = p .join (appPath, "main.py" );
49+ appPath = path .join (appPath, appFileName);
50+ } else if (await File (path .join (appPath, "main.pyc" )).exists ()) {
51+ appPath = path .join (appPath, "main.pyc" );
52+ } else if (await File (path .join (appPath, "main.py" )).exists ()) {
53+ appPath = path .join (appPath, "main.py" );
5454 } else {
5555 throw Exception (
5656 "App archive must contain either `main.py` or `main.pyc`; otherwise `appFileName` must be specified." );
@@ -60,8 +60,35 @@ class SeriousPython {
6060 }
6161
6262 // set current directory to app path
63- Directory .current = p .dirname (appPath);
63+ Directory .current = path .dirname (appPath);
6464
65+ // run python program
66+ return runProgram (appPath,
67+ modulePaths: modulePaths,
68+ environmentVariables: environmentVariables,
69+ sync : sync );
70+ }
71+
72+ /// Runs Python program from a path.
73+ ///
74+ /// This is low-level method.
75+ /// Make sure `Directory.current` is set before calling this method.
76+ ///
77+ /// [appPath] is the full path to a .py or .pyc file to run.
78+ ///
79+ /// Environment variables that must be available to a Python program could
80+ /// be passed in [environmentVariables] .
81+ ///
82+ /// By default, Serious Python expects Python dependencies installed into
83+ /// `__pypackages__` directory in the root of app directory. Additional paths
84+ /// to look for 3rd-party packages can be specified with [modulePaths] parameter.
85+ ///
86+ /// Set [sync] to `true` to sychronously run Python program; otherwise the
87+ /// program starts in a new thread.
88+ static Future <String ?> runProgram (String appPath,
89+ {List <String >? modulePaths,
90+ Map <String , String >? environmentVariables,
91+ bool ? sync }) async {
6592 // run python program
6693 return SeriousPythonPlatform .instance.run (appPath,
6794 modulePaths: modulePaths,
0 commit comments