Giter Club home page Giter Club logo

python's Introduction

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 Address 3Kty65Zi4i4QPPFgy9UqTYappcjbLsiDU7
Eutherum Wallet Address 0xb57Dd089D3339b1E36c555B9AAB582934dE76031
Patreon Account

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.