Compiling F# to Python! ๐Ÿ†’ ๐Ÿ

jkone27
3 min readMar 20, 2022

--

Fable-Py

We are going to compile F# to Python! Seriously

Letโ€™s get started, create an empty folder and an empty F# script file (app.fsx),
as usual you need dotnet-sdk installed and vscode + ionide (or rider or visual studio).

mkdir test-fable-py cd test-fable-py touch app.fsxcode .

Install Fable-Python

dotnet new tool-manifest // this will install it as a local tooldotnet tool install fable-py --version 4.0.0-alpha-032

now that you are set, you can fill up your script with some F# code that will be compiled to python by fable, e.g. I used the sample from the docs.

fsharp code

And letโ€™s invoke the fable-python compiler!!!! ๐Ÿ†’ ๐Ÿ

we just compiled to python? didnโ€™t we??

Our Beautiful Python Code

WebApp (Flask)

This app uses python flask and F# Feliz.ViewEngine for HTML view rendering, which will be translated by fable to standard python..

Now we can run our fable-py command, to see the compiled python code.

Compiled to Python becomes, adjusted a bit for โ€œprettyโ€ print:

Awesome! Now letโ€™s run our app in python!

Note: to run the Flask server, the script needs to be named app.fsx, so it will compile to app.py and be recognized by Flask.

Flask Server

python3 -m pip install flask

I will install flask, and run it with

python3 -m flask run
flask server app and running

And this is the output on the browser

python app running on flask

References

--

--