Orientação gerada a partir de informações públicas. Apenas para referência.
Chaquopy é um plugin do Flutter que permite executar scripts em Python dentro de aplicativos Android usando o SDK Chaquopy. Ele permite a integração sem problemas de código Python em apps Flutter, facilitando o acesso a bibliotecas e frameworks Python diretamente do Dart. Isso é particularmente útil para aproveitar modelos de aprendizado de máquina, tarefas de processamento de dados ou lógica existente baseada em Python em aplicativos móveis. O plugin simplifica a configuração e execução de código Python em dispositivos Android sem exigir serviços de backend separados.
Casos de uso
Executando modelos de aprendizado de máquina em aplicativos Flutter
Integrando bibliotecas Python para análise de dados
Executando scripts legados em aplicativos móveis
Realizando cálculos matemáticos complexos
Usando APIs baseadas em Python em aplicativos Android
Principais recursos
Integração nativa do Python com o Flutter
Suporte a pacotes populares do Python
Configuração fácil por meio do Flutter pub
Executa-se apenas no Android
Execução leve e eficiente
Indicado para
Desenvolvedores com expertise em Python
Aplicativos que precisam de processamento avançado de dados
aplicações móveis com inteligência artificial
Projetos reutilizando bases de código Python existentes
Desenvolvedores Flutter focados no Android
Considerações
Limitado à plataforma Android
Tamanho aumentado do aplicativo devido ao tempo de execução do Python
Sobrecarga de desempenho potencial para cálculos pesados
Requer conhecimento tanto de Dart quanto de Python
Não adequado para implantação no iOS
README original
Snapshot do README original do repositório-fonte vinculado. Consulte o repositório para o conteúdo mais recente.
Chaquopy Flutter plugin
This is an unofficial Chaquopy Flutter plugin to run Python code on Android. This is the simplest version, where you can write your code and run it.
Configuration Steps :
Add maven { url "https://chaquo.com/maven" } and add chaquopy dependency in dependencies section at project level gradle.build like following :
You can find the latest-version of chaquopyhere, also make sure to update the gradle, python and minimum SDK versions accordingly. You can find the compatible python, android gradle version and minimum SDK version based on chaquopy version here:
Then, in the module-level build.gradle file (usually in the app directory), apply the Chaquopy plugin at the top of the file, but after the Android plugin:
Now, in your android folder, you'll have additional python folder created. Now, Download script.py and put it in python directory. (Kindly note that this python file should not be renamed other than script.py and also if your code doesn't work, check the intendations of the downloaded file.)
Now in order to use the chaquopy plugin import the chaquopy package in your flutter app through declaring the package inside pubspec.yaml file.
Now you can run the code easily by calling Chaquopy.executeCode(code) function which returns a map.
The map returned contains two properties and it contains follwing structure:
{"textOutputOrError":"code output or error",}
you can easily access this code via result["textOutputOrError"] , where result is variable of type map and you can name it anything you want.
FAQs:
Why it shows the notification and also crashes the app after some time limit?
This plugin uses Chaquopy SDK, which uses a license for the unlimited use case. In order to remove the notification and timelimit, you need to contact here for the license and after you get your license, you need to follow add follwing two lines in your local.properties file.
chaquopy.license=your_license_key
chaquopy.applicationId=package name of the app
Can I use python packages?
You can use all the python packages by using following configuration.
defaultConfig {
python {
pip {
install "scipy"
install "numpy"// specify any other package to install.
}
}
}
Why this package doesn't support OpenCV, Matplotlib and NLTK packages?
I am writing reasons for individual packages here.
OpenCV and Matplotlib : OpenCV and Matplotlib Requires a special configuration, I am working on this issue to be able to integrate in the package itself.
NLTK and Spacy : NLTK and Spacy Packages can be installed and technically run on your device, but most of the NLTK and Spacy functionality relies on it's data that you will be downloading using nltk.download('all'). so It increases the size of the app significantly. I am also working on this feature.
App Size Reduction:
Using ABI Selection settings you can reduce the app size. The Python interpreter is a native component, so you must use the abiFilters setting to specify which ABIs you want the app to support. The currently available ABIs are:
armeabi-v7a, supported by virtually all Android devices.
arm64-v8a, supported by most recent Android devices.
x86, for the Android emulator.
x86_64, for the Android emulator.
During development you’ll probably want to enable them all, i.e.:
Some features require Python 3.5 or later to be available on the build machine. These features are indicated by a note in their documentation sections.
By default, Chaquopy will try to find Python on the PATH with the standard command for your operating system, first with a matching minor version, and then with a matching major version. For example, if Chaquopy’s own Python version is 3.8.x, then:
OnLinux and Mac it will try python3.8, then python3.
OnWindows, it will try py -3.8, then py -3.
If this doesn’t work for you, set your Python command using the buildPython setting. For example, on Windows you might use one of the following:
You might need to update dsitributionUrl for the gradle based on the version you're using, which you can do inside gradle-wrapper.properties. you can find the available gradle urls here:
distributionUrl=URL
Future Plans :
[ ] Add support for opencv and matplotlib
[ ] Add support for NLTK and Spacy
[ ] Add support for apple devices as well. If you want to help me out with it, kindly contact me,through mail, and I will be happy make this plugin better.
Demo :
Queries :
All the configurations of chaquopy will work the same way, it's mentioned on the chaquopy sdk home page. if you don't find any solution you can open issue in the repository and I am happy to help. :)