Giter VIP home page Giter VIP logo

dartpy's Introduction

Usage

Darpy is a library that allows dart to call out to python code by using the Python C-API embedding and dart:ffi. As such it is a fairly low level interface where you have to manage reference counts of python objects to help the python garbage collector.

However, there are a few high level functions that I've provided to automatically marshal the arguments to python and back and manage the reference counts for you.

Here are a few examples:

For a simple python script inline you can do the following: (Uses the low level C-API that is wrapped by dart:ffi).

void main(List<String> args) {
 dartpyc.Py_Initialize();
 final python = '''
 from time import time, ctime
 print("Today is", ctime(time()))
 ''';
 final pystring = python.toNativeUtf8();
 dartpyc.PyRun_SimpleString(pystring.cast<Int8>());
 malloc.free(pystring);
 print(dartpyc.Py_FinalizeEx());
}

For a more complex script that you don't want inline you can do this: (Uses a few higher level helper methods)

void main() {
  // initializes the python runtime
  pyStart();
  try {
    // imports a python module
    final pyModule = pyimport('multiply');
    // gets a function within that module
    final pFunc = pyModule.getFunction('multiply');
    // calls the function and gets the result
    final result = pFunc([1, 2]);
    print(result); // Should print 3
  } on DartPyException catch (e) {
    print(e);
    // Cleans up memory
    pyCleanup();
    exit(1);
  }
  // Cleans up memory
  pyCleanup();
  exit(0);
}

Feel free to contribute! This is a best effort project that I develop in my spare time.

Limitations

This library assumes that you have the python dynamic library in the following locations:

  • (MacOS) /usr/local/Frameworks/Python.framework/Versions/3.8/lib/libpython3.8.dylib
  • (Linux) /usr/lib/x86_64-linux-gnu/libpython3.8.so
  • (Windows)
          Map env = Platform.environment;
          String username =env["USERNAME"];
          
          pyLibLocation =
              'C:\\Users\\$username\\AppData\\Local\\Programs\\Python\\Python39\\python39.dll';
        
  • (android) wether can i use python from termux ,need to research;

To override this default location, put the following at the beginning of your script:

void main() {
  pyLibLocation = '/path/to/your/dynamic_library';
  // The rest of your code...
}

Eventually, maybe I will create a flutter package that bundles the dynamic library in the assets for each of the platforms, but currently I'm not doing that.

Not all objects are automatically marshalled back and forth, currently just supporting basic objects (int, double, String, num).

I'm working on an annotation and code generation library so that you can just annotate and call functions like so:

import 'package:dartpy/dartpy.dart';
import 'package:dartpy/dartpy_annotations.dart';
part 'gen_dartpy_example.g.dart';

@PyFunction(module: 'multiply')
int multiply(int a, int b) => pymultiply(a, b);

@PyFunction(module: 'multiply', name: 'multiply')
double mult_double(double a, double b) => pymultiplydouble(a, b);

@PyFunction(module: 'multiply', name: 'multiply')
num mult_num(num a, num b) => pymultiplynum(a, b);

void main() {
  try {
    print(multiply(6, 4));
    print(mult_double(6.13, 5.2));
    print(mult_num(6, 5.2));
    print(mult_num(6, 2));
  } on DartPyException catch (e) {
    if (pyErrOccurred()) {
      dartpyc.PyErr_Print();
    }
    print(e.message);
  }
}

"Python" is a registered trademark of the Python Software Foundation

dartpy's People

Contributors

timwhiting avatar b14cknc0d3 avatar

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.