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
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
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:
- Flask. References Feliz.ViewEngine as a nuget package.
- MicroBit
- Timeflies, Cool demo using Tkinter and references FSharp.Control.AsyncRx as a nuget package.
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:
- 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