Skip to content

fable-compiler/Fable.Python

Repository files navigation

Fable Python

Build and Test Nuget

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

This Fable Python repository is a community driven project that contains the Python type bindings for Fable. The library will eventually contain Python (stdlib) bindings for Fable based on Python typeshed. It will also contain type binding for many other 3rd party libraries such as Flask, MicroBit and many more. Some bindings have already been added:

  • Python Standard Libray
  • Jupyter
  • Flask
  • CogniteSdk

Version

This library currently targets Python 3.10 or greater. Types bindings for other versions of Python should not be added to this library until we decide how to deal with Python version handling.

Installation

Prerequisite for compiling F# to Python using Fable:

> dotnet tool install --global fable --prerelease
> dotnet add package Fable.Core --prerelease

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

> dotnet add package 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:

> fable --lang Python MyProject.fsproj

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

Libraries that uses or works with Fable Python

Poetry

Fable.Python uses Poetry for package and dependency management. To handle dependencies when adding Fable Python compatible NuGet packages, you should use Femto.

Contributing

This project is community driven. If the type binding you are looking for is currently missing, then you need to add them to the relavant files (or add new ones). Open a PR to get them included.

The src/stdlib directory contains type bindings for modules in the Python 3 standard library. We also accept type bindings for 3rd party libraries as long as:

  • the package is publicly available on the Python Package Index;
  • the package supports any Python version supported by Fable Python; and
  • the package does not ship with its own stubs or type annotations

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

Road-map

  • Use a dedicated List.fs for Python. List.fs currently depends on Array.fs that is not an efficient list implementation for Python.

  • Compile Fable.Library as a published library (done)

  • Use poetry for Python references to Fable modules (done)

  • Update docs