Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate Flutter app with Python code [closed]

I want to make a Flutter app that uses a Python module. What are the options to integrate Python code, how can I marshal data between the 2 runtimes, can I make a standalone app that includes Python module that doesn't depend on local Python installation?

My context:

I have written Python code that can mark attendance using face recognition. It basically writes the (Name, Registration no. and entry time) to a .csv file of those whose faces match with the data(images of my friends and mine) that I've provided. I would like to create a desktop and/or mobile app that utilises that existing code.

like image 472
littleironical Avatar asked Apr 19 '26 08:04

littleironical


2 Answers

I suggest you to use or convert your Python code as a Back-end code and Flutter code as Front End code. After that, your Flutter application can call the API via HTTP Requests and get data that it wants.

Further reading about my suggestion:

  • Build a REST API with Python
  • HTTP Requests
  • Front-End Back-End : Introduction
like image 174
Harshana Serasinghe Avatar answered Apr 20 '26 23:04

Harshana Serasinghe


This worked for Python Barcode scanner integrated into Flutter.

Step 1: Import http package from this dependency and pub get( http: )

Step-2: In your flutter project create a new file, let’s say request.dart, enter the following lines in that file

import 'package:http/http.dart';
Future getData(url) async {
  Response response = await get(url);
  return response.body;
}

Step-3: Now, your flutter project is ready to connect Python. Go to PyCharm IDE, and create a new Flask Project.

Step-4: By default, you would have this code in your app.py file from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'


if __name__ == '__main__':
    app.run()

The above written code will be automatically generated when you will make a new Flask project. Do not attempt to delete any of this pre-written code.

You can rename the hello_world() function and implement whatever logic you want and return a string, number or an array.

When you would run your project, it will run on localhost. Normally, it runs on http://127.0.0.1:5000/ (local host, port 5000) Before configuring flutter, you can also type this address on your browser to see if your project is up and running!

http://127.0.0.1:5000/

Now for getting the output in flutter, instead of simply returning string, we need to return a json.

For that, from flask import Flask, jsonify

app = Flask(__name__)


@app.route('/')
def hello_world():
    json_file = {}
    json_file['query'] = 'hello_world'
    return jsonify(json_file)


if __name__ == '__main__':
    app.run()

Now in your main flutter file, where you want to access, add this code in the function you want to get your output by python script.

var data = await getData('http://10.0.2.2:5000/);
var decodedData = jsonDecode(data);
print(decodedData['query']);

That's it.

like image 25
Giridharan Venkatapathy Avatar answered Apr 20 '26 22:04

Giridharan Venkatapathy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!