11

GitHub - fable-compiler/Fable.Python: Python bindings for Fable

 2 years ago
source link: https://github.com/fable-compiler/Fable.Python
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Fable Python

Fable is a compiler that translates F# source files to JavaScript and Python.

This Fable Python repository contains the Python bindings for Fable. This library will eventually contain Python (stdlib) bindings for Fable based on Python typeshed. It will also contain type binding for many other libraries as well such as Flask, MicroBit and many more. Some bindings have already been added:

  • Python Standard Libray
  • Jupyter
  • Flask
  • MicroBit
  • CogniteSdk

Installation

Prerequisite for compiling F# to Python using Fable:

> dotnet tool install --global fable-py --version 4.0.0-alpha-004
> dotnet add package Fable.Core.Experimental --version 4.0.0-alpha-005

To use the Fable.Python library in your Fable project:

> dotnet package add Fable.Python

Usage

open Fable.Python.Json

let object = {| A=10; B=20 |}
let result = json.dumps object

To compile an F# Fable project to Python run e.g:

> dotnet fable-py MyProject.fsproj

For more examples see the examples folder. It contains example code for using Fable Python with:

Contributing

If the type binding you are looking for is currently missing (it probably is), then add it to the relavant files (or add new ones). Open a PR to get them included. There's not much Python specific documentation yet, but the process of adding type bindings for Python is similar to JS:

Differences from JS

Note that import all is different from JS. E.g:

[<ImportAll("flask")>]
let flask: IExports = nativeOnly

This will generate import flask and not a wildcard import from flask import *. The latter version is discoraged anyways.

Auto-generation

Parts of this library could benefit from code-generation based on the type annotations in Python typeshed similar to ts2fable. Even so we should keep this library manually updated based on PRs to ensure the quality of the code.

Current plan:

  1. Add bindings for Python ast module (in progress)
  2. Use ast module to parse Python typeshed annotations
  3. Generate F# bindings

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK