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 .
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.
And let’s invoke the fable-python compiler!!!! 🆒 🐍
Our Beautiful Python Code
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.
python3 -m pip install flask
I will install flask, and run it with
python3 -m flask run
And this is the output on the browser