Closed
Description
Description
This issue is a discussion about Python support for Fable. A POC has been made to show that this is possible. However, quite a lot of work is needed to:
- Add decent support for Python
- Make Fable more JS agnostic and support multiple target languages.
Use cases:
- F# running in Jupyter notebook with Python kernel, i.e similar to what is done with e.g Hy and Calysto Hy.
- F# scripting using Python for reduced friction
- More easily support embedded environments like the micro:bit and Rasberry PI.
- Sharing data model between F# and Python code
Things to discuss and decide:
- Should Python for F# be more Pythonic than .NET? There is already .NET support for Jupyter which we should not try to replace or compete with. Fable for Python should perhaps target Python instead of .NET (if possible) and be a more natural fit for Python developers looking to F#.
- Should we try to be compatible with Babel AST or should we diverge into our own Python AST. This is perhaps inevitable, but Babel gives us a good starting point.
- Should we for Fable (as with Peeble) or should we stay within Fable and have Fable support multiple languages. Having our own repo gives freedom and speed (by not needing to care about JS), but we will risk being left behind and obsoleted over time. My feeling is that we should become part of Fable.
- Python basic types is also different from F#. E.g
int
has an arbitrary length. Should we emulate .NET (machine) types to be compatible or should we expose the Pythonint
type as an F# int?
Installing the POC
The source code of the POC currently lives here:
- Install the latest Python 3.9 from https://www.python.org or
brew install python@3.9
on Mac. Note that python may be available on your system aspython
,python3
or both. - Clone both repo and switch to the
python
branch. - Build fable-library for Python:
dotnet fsi build.fsx library-py
- To run tests:
dotnet fsi build.fsx test-py
- In Fable, edit
QuickTest.fs
to some simple F# code. Then rundotnet fsi build.fsx quicktest-py
from the top directory.
Now you can edit both Fable and Expression code and the example code will recompile when needed. Note you need an additional save to QuickTest.fs
to trigger recompile after modifying Fable code. The python file will be available as quicktest.py
. A nice demo is to view both F#, JS, and Python in vscode at the same time. Then you will see how the code is being transformed when you save the F# file:
You can run the generated Python code in the terminal:
$ python3 quicktest.py
Links
A few relevant links:
- https://github.com/dbrattli/Fable/tree/python
- https://github.com/dbrattli/Fable.Jupyter
- https://github.com/cognitedata/Expression/tree/fable
- https://github.com/thinkbeforecoding/peeble
- https://docs.python.org/3/library/ast.html
- https://github.com/babel/babylon/blob/master/ast/spec.md
- https://fable.io/docs/dotnet/numbers.html
Feel free to contribute with discussions, comments, ideas, and code 😍
Metadata
Metadata
Assignees
Labels
No labels