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
This library currently targets Python 3.9. Types bindings for other versions of Python should not be added to this library until we decide how to deal with Python version handling.
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
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:
- Flask. References Feliz.ViewEngine as a nuget package.
- Timeflies, Cool demo using Tkinter and references FSharp.Control.AsyncRx as a nuget package.
- Femto
- AsyncRx
- Fable.Aether
- Fable.Giraffe, port of Giraffe to Fable.Python
- Fable.Logging, logging for Fable.Python
- Fable.Requests
- Fable.Jupyter, Jupyter Notebook using Fable.Python
- Fable.SimpleJson.Python
- Fable.Sedlex
- Feliz.ViewEngine
- FsToolkit.ErrorHandling
- TypedCssClasses
- Typed-BNF
- Zanaptak.TypedCssClasses
Fable.Python uses Poetry for package and dependency management. To handle dependencies when adding Fable Python compatible NuGet packages, you should use Femto.
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:
- https://fable.io/docs/communicate/js-from-fable.html
- https://medium.com/@zaid.naom/f-interop-with-javascript-in-fable-the-complete-guide-ccc5b896a59f
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.
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:
- Add bindings for Python
ast
module (in progress) - Use
ast
module to parse Python typeshed annotations - Generate F# bindings
-
Use a dedicated List.fs for Python. List.fs currently depends on Array.fs that is not an efficient list implmentation for Python.
-
Compile Fable.Library as a published library (done)
-
Use poetry for Python references to Fable modules (done)
-
Update docs