Skip to content

Commit 3d9db1c

Browse files
committed
Split run() into two parts to use with Flet
Close #6
1 parent 116dcfb commit 3d9db1c

File tree

7 files changed

+59
-18
lines changed

7 files changed

+59
-18
lines changed

example/flet_example/ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ PODS:
4444
- SDWebImage/Core (5.15.8)
4545
- sensors_plus (0.0.1):
4646
- Flutter
47-
- serious_python (0.1.1):
47+
- serious_python (0.1.4):
4848
- Flutter
4949
- shared_preferences_foundation (0.0.1):
5050
- Flutter
@@ -97,7 +97,7 @@ SPEC CHECKSUMS:
9797
path_provider_foundation: eaf5b3e458fc0e5fbb9940fb09980e853fe058b8
9898
SDWebImage: cb032eba469c54e0000e78bcb0a13cdde0a52798
9999
sensors_plus: 5717760720f7e6acd96fdbd75b7428f5ad755ec2
100-
serious_python: 76a8038b2e4deae2bad1daee4040d7a49e60bc4f
100+
serious_python: 22931497cfcc8ea8531a1f65c8d317787b5786c8
101101
shared_preferences_foundation: e2dae3258e06f44cc55f49d42024fd8dd03c590c
102102
SwiftyGif: 93a1cc87bf3a51916001cf8f3d63835fb64c819f
103103
url_launcher_ios: 08a3dfac5fb39e8759aeb0abbd5d9480f30fc8b4

example/flet_example/lib/main.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import 'dart:io';
2+
13
import 'package:flet/flet.dart';
24
import 'package:flutter/foundation.dart';
35
import 'package:flutter/material.dart';
6+
import 'package:path/path.dart' as path;
47
import 'package:serious_python/serious_python.dart';
58

69
void main() async {
@@ -10,14 +13,20 @@ void main() async {
1013
} else if (defaultTargetPlatform == TargetPlatform.android) {
1114
fletPlatform = "Android";
1215
}
13-
SeriousPython.run("app/app.zip",
14-
appFileName: "counter.py",
16+
17+
// extract app from asset
18+
var appDir = await extractAssetZip("app/app.zip");
19+
20+
// set current directory to app path
21+
Directory.current = appDir;
22+
23+
SeriousPython.runProgram(path.join(appDir, "main.pyc"),
1524
environmentVariables: {
1625
"FLET_PLATFORM": fletPlatform,
1726
"FLET_SERVER_UDS_PATH": "flet.sock"
1827
});
19-
runApp(const FletApp(
28+
runApp(FletApp(
2029
pageUrl: "flet.sock",
21-
assetsDir: "",
30+
assetsDir: path.join(appDir, "assets"),
2231
));
2332
}

example/flet_example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ packages:
329329
source: hosted
330330
version: "1.9.1"
331331
path:
332-
dependency: transitive
332+
dependency: "direct main"
333333
description:
334334
name: path
335335
sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917"

example/flet_example/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ dependencies:
3535
path: ../../
3636

3737
flet: ^0.7.4
38+
39+
path: ^1.8.3
3840

3941
# The following adds the Cupertino Icons font to your application.
4042
# Use with the CupertinoIcons class for iOS style icons.

lib/serious_python.dart

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import '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

65
import 'src/serious_python_platform_interface.dart';
76
import 'src/utils.dart';
87

8+
export 'src/utils.dart';
9+
910
/// Provides cross-platform functionality for running Python programs.
1011
class 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,

lib/src/utils.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import 'package:archive/archive.dart';
44
import 'package:archive/archive_io.dart';
55
import 'package:flutter/foundation.dart';
66
import 'package:flutter/services.dart';
7+
import 'package:flutter/widgets.dart';
78
import 'package:path/path.dart' as p;
89
import 'package:path_provider/path_provider.dart';
910

1011
Future<String> extractAssetZip(String assetPath) async {
12+
WidgetsFlutterBinding.ensureInitialized();
1113
final documentsDir = await getApplicationDocumentsDirectory();
1214
final destDir = Directory(p.join(documentsDir.path, p.dirname(assetPath)));
1315

@@ -41,6 +43,7 @@ Future<String> extractAssetZip(String assetPath) async {
4143
}
4244

4345
Future<String> extractAsset(String assetPath) async {
46+
WidgetsFlutterBinding.ensureInitialized();
4447
Directory documentsDir = await getApplicationDocumentsDirectory();
4548

4649
// (re-)create destination directory

0 commit comments

Comments
 (0)