How many times do you wished to add some Python magic to your app ? Executing a Python script inside a Flutter app is no longer a problem. All you have to do is to write your Python code and make sure that it compile successfully then call the function execute finally receive the result that's it ! As shown below the function takes the file path as a parameter.
- About Python
- Installing
- Hello World App using Python plugin
- Add Python Modules
- Verifying File Existence
- Contact & Support
Python is a high-level, interpreted, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation. Python is dynamically-typed and garbage-collected.
Add Python to your pubspec.yaml file:
dependencies:
python:
Import python in files that it will be used:
import 'package:python/python.dart';
In order to access to Python class methods first you have to declare an instance from the class Python.
Python pythonScript = Python();
Then you should prepare where the result will be shown. As an example we will declare an empty String for the result.
String pythonCodeResult = "";
Now it's time to create the Python file that contains the code. We will give helloWorld.py as a file name. Make sure that the python file compiles successfully.
print("Hello World from Python inside Flutter")
Add a any kind of buttons to access to OnPressed event. As an example we will use TextButton.
TextButton(
child: const Text("Execute Python Code"),
onPressed: () async {
await pythonScript.execute('lib/helloWorld.py').then((result) {
setState(() {
pythonCodeResult = result;
});
});
},
),
Finally, you have the result inside pythonCodeResult variable all you have to do is to add it into Text widget in order to display it.
Text(pythonCodeResult)
And voila you have successfully executed Python code inside Flutter.
As usual, in order to access to Python plugin methods you need to create an instance from Python class.
Python pythonScript = Python();
Let's define the list of wanted modules. Note that every module requires two parameters. The first one is the name and the second one is the version.
List<Module> modules = [
Module("Flask", "2.1.2"),
Module("requests", "0.11.1"),
Module("numpy", "1.22.3"),
];
Adding a module is done by calling the method addModules and it takes a list of modules just like below.
pythonScript.addModules(modules);
And voila! You've successfully added the required Python modules.
Unlike the exists() method. The method fileExists() is provided by Python package that allows the developer to verify if the file exists or not. The main difference between these two functions is fileExists() is not asynchronous which means you can assign the result to a variable directly also you can use it as if statement conditions. To use this method you have to define an instance from the class Python
Python pythonScript = Python();
Then call the function and pass the path as a paramter
bool isExists = pythonScript('lib/helloWorld.py');
The method returns bool that's why we assigned the result to a bool variable.
Developer Contact: Nabil Krissane
You can support me by :
Bitcoin Wallet Address | 3Kty65Zi4i4QPPFgy9UqTYappcjbLsiDU7 |
---|---|
Eutherum Wallet Address | 0xb57Dd089D3339b1E36c555B9AAB582934dE76031 |
Patreon Account |