python 0.0.2 icon indicating copy to clipboard operation
python: ^0.0.2 copied to clipboard

Python is a Flutter plugin that allows the developer to execute a Python script inside Flutter.

Flutter Python #

code example

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.

code example

Table of Contents #

About Python #

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.

Installing #

Add Python to your pubspec.yaml file:

dependencies:
  python:

Import python in files that it will be used:

import 'package:python/python.dart';

Hello World App using Python plugin #

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.

Add Python modules #

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.

Verifying file existence #

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 #


Support #

You can support me by :

Bitcoin Wallet Address3Kty65Zi4i4QPPFgy9UqTYappcjbLsiDU7
Eutherum Wallet Address0xb57Dd089D3339b1E36c555B9AAB582934dE76031
Patreon Account
51
likes
120
pub points
67%
popularity

Publisher

verified publisher iconnabilkrissane.cf

Python is a Flutter plugin that allows the developer to execute a Python script inside Flutter.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter, http

More

Packages that depend on python